Summary
For those who have followed my blog, I have a long-term project called the NHibernate Mapping Generator. This application generates ORM table mapping C# code for the Fluent NHibernate ORM. In this blog post I’m going to describe a few new features to this application.
ADO.Net Unit Testing
There is an ADO.Net Context that was created in the unit test helpers of this solution. I have added a condition where this can be called form a unit test causing the connection to override and use the SQLLocalDB database. If you use the ADODatabaseContext in your project, then you can connect to your normal database by passing a connection string. If you create unit tests and include the start and end code for the SQLLocalDB database, then this context will use that database whenever your code is executed from the unit test assembly. When using the context from a unit test, you will not need a connection string at all, but you might want to specify which database to default to. In order to do that, you can pass a blank connection string and pass in the database name, or you can pass the database name in the connection string and the constructor will copy the database into the new unit test context (see ADODatabaseContext.cs source for more details).
Session Wrappers
I have added two session wrappers to the project. To accommodate all the default methods and properties, I used partial classes with all the repeated code in the Wrappers directory. A method for each table is created inside the other half of the partial class for each database that is generated. This code is contained inside the database directory and Wrappers sub-directory. This will allow you to change your code from:
var query = (from d in db.Query<Department>() select d);
To:
var query = (from d in db.Department select d);
Future Enhancements Planned
One enhancement that will be necessary is the ability to access databases that do not show up in the drop-down list. The SQL Server Management Studio has the ability to type in the name of a SQL Server and I intend to mimic that feature.
Next, I need to store the SQL servers discovered in the registry so that the next time this utility is run, it just reads those entries. If the user desires, they will be able to refresh their list just like Management Studio does. This will cause the startup time to be quicker.
Next, I need to have the ability to use a user id and password instead of just integrated security. Management Studio has a drop-down to select one or the other, I’ll probably mimic this and only show the database list when the user clicks a button.
Next, I would like to have check boxes for the ability to generate code for non-NHibernate scenarios. That way the NHibernate mappings will not be created, but the views, stored procedures, table creation code and the constraint code will be created. That will make this utility handy for people who want to setup unit tests for projects that use EF, LINQ-to-SQL or just plain ADO.Net.
Where to Find the Code
You can download the entire solution including the unit test helpers, sample unit tests, sample projects and the NHibernate mapping generator at my GitHub account here.