Click here to Skip to main content
Click here to Skip to main content
Articles » Database » Database » Utilities » Downloads
 

Myotragus Tuples - Automatizing NHibernate Composite Keys

By , 17 Jan 2013
 
Execution_Times.zip
Execution Times.xlsx
Myotragus.Data.Tupples.zip
Libs
Antlr3.Runtime.dll
Castle.Core.dll
Iesi.Collections.dll
NHibernate.ByteCode.Castle.dll
NHibernate.dll
NLipsum.Core.dll
nunit.framework.dll
Remotion.Data.Linq.dll
Myotragus.Data.Tupples.Tests
bin
Debug
Antlr3.Runtime.dll
Castle.Core.dll
Data
Domain.sdf
Iesi.Collections.dll
Myotragus.Data.Tupples.dll
Myotragus.Data.Tupples.Tests.dll
NHibernate.ByteCode.Castle.dll
NHibernate.dll
NLipsum.Core.dll
nunit.framework.dll
Remotion.Data.Linq.dll
System.Data.SqlServerCe.dll
Data
Domain.sdf
Domain
nTests.nunit
obj
Debug
Myotragus.Data.Tupples.Tests.dll
ResolveAssemblyReference.cache
Properties
Myotragus.Data.Tupples
bin
Debug
Myotragus.Data.Tupples.dll
obj
Debug
Myotragus.Data.Tupples.dll
Properties
using System.Collections.Generic;
using Myotragus.Data.Tupples.Tests.Domain;
using NHibernate;
using NUnit.Framework;

namespace Myotragus.Data.Tupples.Tests
{
	/*
	 Execute only to prepara the database. Methods here would create data
	 */
	[TestFixture]
	public class PrepareDataBase
	{
		[Test]
		public void AssignCategories()
		{
			using (var session = Core.OpenSession())
			{
				var products = session.CreateCriteria<Product>().List<Product>();
				var categories = session.CreateCriteria<Category>().List<Category>();

				foreach (var p in products)
				{
					for (var i = 0; i < Core.GetRandom(5); i++)
						AssignCategory(session, p, categories);
				}

				session.Flush();
			}
		}

		[Test]
		public void ClearCategoryAssignment()
		{
			using (var session = Core.OpenSession())
			{
				var associations = session.CreateCriteria<CategoryProducts>().List<CategoryProducts>();
				foreach (var assoc in associations)
					session.Delete(assoc);

				session.Flush();
			}
		}

		private static void AssignCategory(ISession session, Product product, IList<Category> categories)
		{
			var c = categories[Core.GetRandom(categories.Count)];
			using (var transaction = session.BeginTransaction())
			{
				var categoryProduct = new CategoryProducts
				{
					ProductId = product.Id,
					CategoryId = c.Id,
					CustomDescription = Core.GenerateSentence()
				};

				if (session.Get<CategoryProducts>(categoryProduct.Key) == null)
					session.Save(categoryProduct);

				transaction.Commit();
			}
		}

		[Test]
		public void InsertProducts()
		{
			using (var session = Core.OpenSession())
			{
				for (var i = 0; i < 100; i++)
				{
					var p = new Product { Name = Core.GenerateSentence(3) };
					using (var transaction = session.BeginTransaction())
					{
						session.Save(p);
						transaction.Commit();
					}
				}

				session.Flush();
			}
		}

		[Test]
		public void InsertCategories()
		{
			using (var session = Core.OpenSession())
			{
				for (var i = 0; i < 10; i++)
				{
					var p = new Category { Name = Core.GenerateSentence(1) };
					using (var transaction = session.BeginTransaction())
					{
						session.Save(p);
						transaction.Commit();
					}
				}

				session.Flush();
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of use and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Erich Ledesma
Software Developer Babtec Informationssysteme GmbH
Spain Spain
Member
I Received a Bachelor's Degree in Computer Science at the Mathematics and Computer Science Faculty, University of Havana, Cuba.
 
Programming Languages I use:
C#, C++, Javascript, Delphi, Java, PHP, and others (less frequently).

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 17 Jan 2013
Article Copyright 2011 by Erich Ledesma
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid