This project is read-only.

Project: DataAccess


This example shows a comparison between ADO.NET, a custom Data Access Layer, EntityFramework and NHibernate. All the CRUD are explained.

Here the T-SQL code to create the database for test usage:
private const string CreateDataBaseSql =
	"if exists (select name FROM sys.databases WHERE name = N'{0}')" + "\n" +
	"	alter database [{0}]" + "\n" +
	"	set single_user" + "\n" +
	"	with rollback immediate" + "\n" +
	"	use Master" + "\n" +
	"if exists (select name FROM sys.databases WHERE name = N'{0}')" + "\n" +
	"	drop database [{0}]" + "\n" +
	"create database [{0}]";

The CREATE method using the ADO.NET Provider Model:
[TestMethod]
public override void Insert()
{
	CreateSchema();

	using (var connection = factory.CreateConnection())
	{
		connection.ConnectionString = ConnectionString;

		using (var command = connection.CreateCommand())
		{
			////insert into [Customer] ([Id], [Name], [Birthday]) values (@Id, @Name, @Birthday)
			command.CommandText = KnownQueries.Insert;
			command.AddParameter("@Id", KnownCustomer.Id);
			command.AddParameter("@Name", KnownCustomer.Name);
			command.AddParameter("@Birthday", KnownCustomer.Birthday);

			connection.Open();
			command.ExecuteNonQuery();
		}
	}
}

The CREATE method using EntityFramework:
[TestMethod]
public override void Insert()
{
	using (var dbContext = new CustomerDbContext(factory, ConnectionString))
	{
		dbContext
			.CustomerRepository
			.Add(	new Customer {	Id = KnownCustomer.Id, Name = KnownCustomer.Name, BirthDay = KnownCustomer.Birthday });

		dbContext.SaveChanges();
	}
}

The CREATE method using NHibernate:
[TestMethod]
public override void Insert()
{
	using (var session = factory.OpenSession())
	{
		using (var transaction = session.BeginTransaction())
		{
			session.Save(new Customer { Id = KnownCustomer.Id, Name = KnownCustomer.Name, BirthDay = KnownCustomer.Birthday });
			transaction.Commit();
		}
	}
}

Last edited Aug 19, 2011 at 8:16 AM by teoarch, version 7

Comments

No comments yet.