Quantcast
Channel: Tagged with .NET
Viewing all articles
Browse latest Browse all 9

nopCommerce with NuoDB – Part 2

$
0
0
Jiri Cincura

In the first installment of this two part series we talked briefly about the benefits of running NuoDB as a database backend for nopCommerce. If you wanted more detail, or just to see it in action, I recommended watching the on-demand webinar “Enabling Scalability & Redundancy in eCommerce.” Today, I’d like to introduce you to some of the details for how nopCommerce was enhanced to run on NuoDB, and to provide a peek under the covers of the “Next-Next-Next” installation.

The port itself is basically a change in the nopCommerce database layer and in the install page (because you need to select a new type of database). Thus, if you’d like to take this port into new versions of nopCommerce it shouldn’t take long.

The easiest way to get started is to download the NuoDB sources from GitHub https://github.com/nuodb/nopCommerce. If you’re eager to try what you have seen in the webinar, I strongly recommend reading the Deploying.Readme.txt file first. Once you build the solution in Visual Studio – and remember, you need to have NuoDB ADO.NET driver (NuoDb.Data.Client.dll) on that system – and have prepared your NuoDB database, you can proceed with the installation.

The installation does not differ from vanilla nopCommerce installation. You don’t need to perform any hacking. Everything is done, as usual, via the web.

Internally, NopCommerce is using Entity Framework - even for the installation. Entity Framework is able to scaffold the model you’re working with and, together with mapping specifications (either explicit or thanks to conventions), create a SQL script with some cooperation with a provider. That’s what the installation is doing as a first step. The script is then executed one statement at a time creating tables, columns, etc. All the main logic is in <a href=”<a href=”https://github.com/nuodb/nopCommerce/blob/9fa92f1e0fd300a28797f9a735b9a4cdb6215e1c/Libraries/Nop.Data/Initializers/NuoDbDatabaseInitializer.cs”>NuoDbDatabaseInitializer</a> class”>https://github.com/nuodb/nopCommerce/blob/9fa92f1e0fd300a28797f9a735b9a4….

namespace Nop.Data.Initializers
{
	public class NuoDbDatabaseInitializer : IDatabaseInitializer where TContext : DbContext
	{
		public void InitializeDatabase(TContext context)
		{
			if (context == null)
				throw new ArgumentNullException("context");

			using (var ts = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions() { Timeout = TimeSpan.FromHours(1) }))
			{
				if (IsInitialized(context))
					return;

				var script = ((IObjectContextAdapter)context).ObjectContext.CreateDatabaseScript();
				foreach (var item in script.Split(';').Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => x.Trim()))
				{
					context.Database.ExecuteSqlCommand(item);
				}
				context.Database.ExecuteSqlCommand("create table _nopCommerce_NuoDB (dummy int)");
				Seed(context);
				context.SaveChanges();
				ts.Complete();
			}
		}

		static bool IsInitialized(TContext context)
		{
			try
			{
				context.Database.ExecuteSqlCommand("select dummy from _nopCommerce_NuoDB fetch first 0");
				return true;
			}
			catch
			{
				return false;
			}
		}

		protected virtual void Seed(TContext context)
		{ }
	}
}

 

Then – depending on your selection – the sample data is created. This is in fact the standard process of using Entity Framework. The installation creates new instances of entities and saves these into the database. All this “magic” happens thanks to NuoDbDataProvider class that’s plugged into nopCommerce and all the recent improvements in the .NET driver [Entity Framework supportConnection Pooling, .NET]. The driver was improved a lot and hence the upper layers cannot see really what’s beneath the Entity Framework layer. This might look like obvious or expected behavior – especially for people using it only with Microsoft SQL Server – but that’s the trick really. Believe me, to see it running for the first time, with only few modifications in nopCommerce (and heavy work in the driver), was electric for me.

After you finish your installation (it’s going to take a minute or two, so grab a delicious beverage and relax), you can start configuring nopCommerce and playing with it. You can check the database structure later to see why it took so long, there are some impressive tables.

And that’s it. You not only installed nopCommerce on NuoDB, but you should also now have an understanding of where most of the important NuoDB-ish magic comes from.

Happy e-commercing with NuoDB.

If you have any questions, feel free to leave a comment and I’ll try to answer all.

Old nid for redirect: 
231

Viewing all articles
Browse latest Browse all 9

Trending Articles