Anyone who has worked with me or knows me will understand I have very little patience! This is probably my worst personality trait. I started to think about my day to day frustrations and what I can change and what I can’t.
Slow pc’s are an issue, wasting time waiting for a PC to perform an action is one thing that frustrates me enormously ! But I can change this! Buy a faster pc! (To be honest the above is just a justification to make me feel better about spending money on a new dev machine!)
I started to think about which programs I am running concurrently on a daily basis :- Visual Studio 2008 Pro, Re-Sharper, Visual SVN, SQL Server 2008 Standard, IIS 7, NHProf, Tweet Deck, Outlook, Word, Excel, VMware as well as others. Some of these programs are resource hungry and running them concurrently slows my current pc’s to a halt!
I need POWER. I need BIG BIG POWER! I need PORTABILITY! So without further adieu…….
Alienware M15x : Base Cosmic Black
Processor : Intel i7 720QM(1.60Ghz,6MB) Mobile CPU
Display : 15.6in Wide Full HD (1920 x 1080) WLED
Memory : 4096MB (2x2048) 1333MHz DDR3 Dual Channel
Hard Drive : 256GB Solid State Hard Drive
Optical Drive : Slot Load DVD+/-RW (DVD, CD read and write)
Graphics : 512 MB NVIDIA GeForce GT240M
Operating System : English Genuine Windows 7 Professional (64 BIT)
Please don’t take this as boastful! I am just really excited about my new dev machine and look forward to seeing how productive I can be with more power!
Please comment on the spec or even post a comment with the spec of your own machine for comparison. I would love this post to be a place for developers to visit to get an idea of the machine spec’s developers are using when developing on top of the .net framework.
Happy new year everyone!
Last month I wrote a post explaining how to test your data access layer by using Fluent NHibernate to generate an in-memory SQLite database from an applications domain model and fluent mappings. You can then run unit tests against the SQLite database rather than mocking objects.
Over the last couple of weeks I have become less fond of Fluent NHibernate (long story! Curse & Swear!) and wanted to recreate the same functionality using NHibernates standard mappings and features. So without further adieu…..
Firstly you will need to build up the schema for the SQLite database. This is created automatically based upon the applications domain model and mapping files.
public abstract class InMemoryDatabase : IDisposable
{
private static Configuration _configuration;
protected static ISessionFactory _sessionFactory;
protected static ISession _session;
protected InMemoryDatabase()
{
_sessionFactory = CreateSessionFactory();
_session = _sessionFactory.OpenSession();
BuildSchema(_session);
}
private static ISessionFactory CreateSessionFactory()
{
//NHibernateProfiler.Initialize();
Dictionary<string, string> properties = new Dictionary<string, string>
{
{"connection.driver_class","NHibernate.Driver.SQLite20Driver"},
{"dialect", "NHibernate.Dialect.SQLiteDialect"},
{"connection.provider","NHibernate.Connection.DriverConnectionProvider"},
{"query.substitutions", "true=1;false=0"},
//{"show_sql", "true"},
{"connection.connection_string","Data Source=MyTestDb.db;Version=3;New=True;"},
{"proxyfactory.factory_class","NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"},
{"cache.provider_class", "NHibernate.Cache.HashtableCacheProvider"},
{"cache.use_second_level_cache", "true"},
{"cache.use_query_cache", "true"}
};
_configuration = new Configuration()
.SetProperties(properties)
.AddAssembly(typeof(Person).Assembly);
return _configuration.BuildSessionFactory();
}
private static void BuildSchema(ISession session)
{
SchemaExport export = new SchemaExport(_configuration);
export.Execute(true, true, false, session.Connection, null);
}
public void Dispose()
{
_sessionFactory.Dispose();
}
}
You will then need to create a class that derives from InMemoryDatabase, this is where the static data is setup. When calling Session.Flush() the data is persisted to the SQLite database.
public abstract class InMemoryData : InMemoryDatabase
{
protected InMemoryData()
{
Account accountOne = new Account(1, "AC001");
Account accountTwo = new Account(2, "AC002");
_session.Save(accountOne);
_session.Save(accountTwo);
_session.Flush();
_session.Save(new Person(1, "Dan", "Watson", accountOne));
_session.Save(new Person(1, "James", "May", accountTwo));
_session.Save(new Person(1, "Andrew", "Plum", accountOne));
_session.Flush();
}
}
As long as your unit test classes then derive from InMemoryData you will have access to the ISession object that is associated to the SQLite database and will be able to execute unit tests against the static data.
I usually create a base class that all my data access tests derive from. I setup my data access repository’s to accept an ISession object via a constructor argument (this is also really useful for dependency injection which is outside the scope of this post) and in this instance pass in the ISession object associated with the SQLite database.
[TestFixture]
public abstract class TestBase : InMemoryData
{
protected IPersonData PersonData { get; set; }
protected IAccountData AccountData { get; set; }
[SetUp]
public void Init()
{
PersonData = new PersonData(_session);
AccountData = new AccountData(_session);
}
[TestFixtureTearDown]
public void Die()
{
Dispose();
}
}
Here is an example of a unit test class. The class is derived from TestBase which then allows my data tests to make assertions on the data access methods but with those methods executing against the SQLite database and static data.
public class PersonDataTest : TestBase
{
[Test]
public void Select_All_People_Should_Return_3()
{
Assert.AreEqual(3, PersonData.SelectAllPeople().Count());
}
}
As always here’s the code for you to
download.