New .Net App – Just add water

I’ve been meaning to post this for a while, only I was planning on covering it with more depth. Life has been a bit busy, so I’m going to post it in it’s current form.

Using FluentMigrator, Fluent-NHibernate, and NHibernate, one can whip up an app fairly quickly. Let’s walk through it in brief.

First, grab the source for FluentMigrator and Fluent-NHibernate from github. Second, build each and put the binaries somewhere practical for your application. Use the NHib binaries from Fluent-NHiberate; otherwise dll versions can be a problem. Now open Visual Studio and create an application. To keep things easy, I’ll make it a WinForm application. Source will be in our OSS project, title InstantApp.

CropperCapture[1]

Now add the references to FluentNH, FluentMigrator, and NH. For the count, I added FluentMigrator.dll, FluentMigrator.Runner.dll, Castle.Core.dll, Castle.DynamicProxy2.dll, log4net.dll, and System.Data.SQLite.dll to use sqlite as the database. Don’t worry, the sqlite dll is in the sample code repository. (Update: please refer to the sample for the added dlls. I added others such as NHibernate.linq.)

CropperCapture[2]

Now, we need some schema. I recommend keeping schema codefiles in a seperate folder to keep things easy to maintain; so I added a “Migrations” folder in the project. Then added a “first.cs” schema class. Because I am a bit particular, I’m also adding a folder called “Models” to contain the object model classes. In order to wire-up NHib with Fluent-NH, each model will need a mapping class. I’ve added those to the folder “Mappings” under the “Models” folder. Then I’m adding “Tests.cs” because I want to test a couple things.

CropperCapture[3]

So far, so good… now we need some code, and because this is fast & dirty, I’m going to whip out the migration code. Let’s keep it to just one table. And add some data.

   1:  public override void Up()
   2:  {
   3:    //users table
   4:    Create.Table("Users")
   5:    .WithColumn("Id").AsInt32().NotNullable().PrimaryKey()
   6:    .WithColumn("Email").AsString(50).NotNullable()
   7:    .WithColumn("Name").AsString(50).NotNullable()
   8:    .WithColumn("Password").AsString(100).NotNullable();
   9:   
  10:    for(int loop = 0; loop < 10; loop++)
  11:    {
  12:      Insert.IntoTable("Users").Row(new { Id = loop, Email = string.Format("email{0}@mail.com", loop),
  13:                                                  Name = string.Format("test name{0}", loop),
  14:                                                  Password = string.Format("password{0}", loop)
  15:      });
  16:    }
  17:  }
  18:   
  19:  public override void Down()
  20:  {
  21:    Delete.Table("Users");
  22:  }

Let’s run this on startup so the table gets created right away. So I added this to Program.cs and called it in Main():

   1:  static void MigrateDatabase()
   2:  {
   3:   //make sure the sqlite db file exists
   4:   if (!File.Exists(DbFile))
   5:   System.Data.SQLite.SQLiteConnection.CreateFile(DbFile);
   6:   
   7:   //run migration
   8:   using (SQLiteConnection connection = new SQLiteConnection(ConnectionString))
   9:   {
  10:    var conventions = new MigrationConventions();
  11:    connection.Open();
  12:    var processor = new FluentMigrator.Runner.Processors.Sqlite.SqliteProcessor(connection, new SqliteGenerator());
  13:    var runner = new MigrationVersionRunner(conventions, processor, new MigrationLoader(conventions));
  14:   
  15:   //upgrade to latest
  16:   runner.UpgradeToLatest(false);
  17:   }
  18:  }

 

Aaaanndd now.. {drum roll} ..let’s get a list of all the users as take from one of the tests:

   1:  var session = CreateSessionFactory().OpenSession();
   2:  var list = from c in session.Linq<User>()
   3:    select c;
   4:  Assert.True(list.ToList().Count == 10);

Yeah! It passes. So now, just add a data grid view to the WinForm, and the right code.  Wha-bam! You get data in your grid. Pretty nice for less than an hour’s work.

   1:  var session = CreateSessionFactory().OpenSession();
   2:  var list = from c in session.Linq<User>()
   3:   select c;
   4:   
   5:  dataGridView1.AutoGenerateColumns = true;
   6:  dataGridView1.DataSource = list.ToList();

 

CropperCapture[9]

 

That’s the short intro to building an app with NH, FNH, FluentMigr, and a dash of Linq. See the OSS code for better details.

 
September 28, 2009 07:00 by josh
E-mail | Permalink
blog comments powered by Disqus

Sept 11 2001 In Memoriam

  Few events left more an indelible mark on me before Sept 11 2001, than watching, in school, the shuttle Challenger explode. But in comparison to 9-11, it doesn’t even register. For the families of the fallen, for the heroes and the brave, for those that have served this country, and for those that are currently serving, I pray.

Let us remain vigilant, lest we forget and evil befall us again.

dont_tread_on_me4

 
September 11, 2009 00:01 by josh
E-mail | Permalink
blog comments powered by Disqus

Random Thought

  I just realized that I should credit Fluent-NHibernate for putting me over the edge to build my first NHibernate app. I’ve been wanting to get to it for a while, but I don’t have any paid projects that are using it. FNH made it easy to set up NHibernate and get going. I doubt I’m the only one to whom this applies.  It also led me to find FluentMigrator on which I’m now a contributor. Lastly, I also finally chose to use ASP.Net MVC on the same project I’m using FNH, FluentMigrator, and NHibernate. It’s funny how things build momentum like that.

 
September 9, 2009 23:43 by josh
E-mail | Permalink
blog comments powered by Disqus


about josh

another programmer blogging about his misadventures in writing code.

Contact

contact us for website & software consulting

Decide

decide on pragmatic solutions

Develop

develop your product together

Succeed

achieve your goals with our services