Brian Hartsock's Blog

Software

NHibernate O/R Mapper: Part 2

by bhartsock on Feb.10, 2008, under .NET, ASP.NET, Software

In a previous post I talked about my first impressions of NHibernate. Not a whole lot has changed since then, except I have learned a lot more. But, I said I would discuss what I didn’t like about NHibernate so here it goes.

Session Management

The biggest thing that I don’t like is the session management aspect. Although I have come to understand why NHibernate sessions are so valuable and critical to its functionality, it doesn’t stop the fact that persistent objects are tied to a single database. For a project that follows the patterns and practices set forth in ivory towers, this works great. All business entities are stored in a database in 6th normal form and everyone is happy. Well, when working with legacy and enterprise systems, that usually isn’t the case. There are many different databases that are used and objects are stored across a variety of them.

In order to get my system to use NHibernate, the first challenge was incorporating multiple sessions. A session is basically a connection to a single database that manages persistent objects. This article describes a really complex way to accomplish it, but all you really need to pay attention to is the NHibernateSessionModule and NHibernateSessionManager. Using the outline described in the article, I successfully got multiple sessions to work fairly painlessly.

The second challenge was storing a persistent object in multiple database. Since persistent objects in NHibernate can’t span multiple sessions, some data massaging has to be performed. As I see it, there are two possibilities for storing an object in multiple database.

  1. Different objects for each database with type conversion operators
  2. The same object for all databases with extra session logic

The latter is obviously easier from a domain model perspective, but not from an implementation perspective. For a persistent object from one session to be stored in another session, all references must be removed from the session. This means unproxying the object and unpersisting any collections contained within the object. Looking through the NHibernate source code, I was able to accomplish this with only a few lines of code.

Setup & Documentation

Most of the getting started guides for NHibernate are no where near complete or up-to-date. It took a couple days to get my environment up and running. This isn’t really a problem with the library itself, but is still a problem for first time users. In all reality, there seems to be a lack of documentation in general. Three example mappings in the documentation is no where near enough for what is marketed as an enterprise ORM solution.

Conclusion

NHibernate is awesome. Even with these minor issues, it is one of the greatest tools for web development I have ever used. I hope that the development of this library continues to improve, and hopefully some of these issues will be resolved.

Post to Twitter Post to Digg Post to Facebook Post to Reddit

1 Comment more...

Data Access Application Blocks

by bhartsock on Sep.18, 2007, under .NET, ASP.NET, Programming, Software

Last week I discussed using ADO.NET in enterprise applications. A good part of the article was explaining the negatives of using the DataSet class. Although I stand by that conclusion, I also want to say that I like ADO.net overall. Some of the tools it provides are good for enterprise applications, while others are better for smaller applications.

ADO.NET provides common interfaces and base classes that can be used with different database providers. While some companies use everything Microsoft, we do not. In all honesty, I would choose MySQL over SQL Server for almost any project but that is an argument for another day. Like I was saying, ADO.NET is good, but can have some very repetitive code. Managing connections, creating commands, adding parameters, and executing readers is pretty annoying. But, the worst thing about ADO.NET is having to create database provider specific code. To create a connection to an Odbc database, you have to actually load the OdbcConnection class.

Luckily, Microsoft realized that ADO.NET had these short comings and created Data Access Application blocks, which are part of their Enterprise Library. I have been using them for a couple weeks and am definitely a fan. The greatest thing about it is the ability to completely abstract out the database provider. For example (Copied from the Microsoft website):

Database db = DatabaseFactory.CreateDatabase();
DbCommand dbCommand = db.GetStoredProcCommand("GetProductsByCategory"); 

// Retrieve products from category 7.
db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, 7);
DataSet productDataSet = db.ExecuteDataSet(dbCommand);

If you didn’t notice, there was no mention about an Ole, SQL, Oracle, or Odbc provider. Also, I think it is a really clean and logical way to abstract the database. I definitely recommend checking out Data Access Application blocks, as well as the entire Enterprise library.

Post to Twitter Post to Digg Post to Facebook Post to Reddit

Leave a Comment more...

ADO.NET

by bhartsock on Sep.06, 2007, under .NET, ASP.NET, Design Patterns, Programming, Software

I recently started a new project here at Webmail.us which will be a .NET based web application. One of the core pieces of .NET web apps is, not surprisingly, ADO.NET. It is Microsoft’s upgrade from ADO, but it is more of a rewrite. Almost everything has been refactored, with many new abstractions and paradigms for how to access data. When I first read about ADO.NET, I was very skeptical. I didn’t understand the need for these complex, hard to understand abstractions like DataTable’s and DataRelation’s and DataSet’s. It all seemed foreign to me for a multitude of reasons. Unfortunately, Microsoft seems to be pushing DataSet’s so loudly, its hard to see what else is out there.

After a few days of digging though, I found a few great articles (Choosing Data Containers for .NET, On the Way to Mastering ASP.NET: Introducing Custom Entity Classes) on why not to use DataSets, as well as why you should. DataSets were designed for rapid application development, and seems to be Microsoft’s answer to the Rails ActiveRecord pattern. It is a quick and dirty way to build the data access layer for web applications. To be more specific, by quick I mean it allows version 1.0 of a product to be released quickly, because a data abstraction already exists. By dirty, I mean it isn’t an abstraction created for your application, it is a data abstraction created for any application.

Pro’s of ADO.net

  • Fast development time
  • In place architecture.
    Developing custom data access solutions can be very powerful, and very risky. A bad design can lead to bad performance and is inherently error prone
  • Feature rich
    • Disconnected data
    • Concurrency
    • Many more…
  • Easily serializable

Con’s of DataSets

  • Large memory footprint
  • Not object oriented.
    DataSet’s represent relation data, just like a database. But I don’t want an in-memory representation of the Employee table, I want Employee objects. Scott Hanselman sums it up pretty nicely:

    A DataSet is an object, right? But it’s not a Domain Object, it’s not an “Apple” or “Orange” – it’s an object of type “DataSet.” A DataSet is a bowl (one that knows about the backing Data Store). A DataSet is an object that knows how to HOLD Rows and Columns. It’s an object that knows a LOT about the Database. But I don’t want to return bowls. I want to return Domain Objects, like “Apples.”

  • Not abstracted.
    Directly connected with database data and structure. So, any change to the database schema, could trickle down to the presentation layer.

Conclusion

ADO.NET is a huge improvement from old ADO, but don’t buy into the entire framework. Microsoft is trying to push DataSets but they might not always be the best solution. For enterprise applications, I don’t think DataSet’s are the answer. On the other hand, for smaller web application, it might be a perfect fit.

Post to Twitter Post to Digg Post to Facebook Post to Reddit

Leave a Comment more...

K2 WordPress theme

by bhartsock on Sep.04, 2007, under Software, Uncategorized

For the past few weeks, I log into my blog and am disgusted at the theme I was using. It was pretty awful, but I couldn’t find anything I liked better.

My requirements for a theme are simple:

  • Widget ready
  • At least 1 sidebar, maybe 2
  • Simple

Being a web developer, I hoped for a little more though. I wanted an Ajax theme. It took a while, but I finally found K2, the second version of the default Kubrick theme. So far I am super happy with it, but I need to tweak the Now Reading plugin to not suck at HTML.

Maybe I can get back to blogging about important things soon since this one annoyance is gone …

Post to Twitter Post to Digg Post to Facebook Post to Reddit

Leave a Comment more...

Zend Framework 1.0

by bhartsock on Jul.13, 2007, under Design Patterns, Software

A while back, I posted a review of some existing Active Record implementations. One of those happened to be the Zend frameworks DB portion. I believe that was about the time of version 0.7, and I recently looked on their website and discovered 1.0 had been released.

I initially had low expectations, expecting 1.0 to just clean up 0.7 code, with minimal feature additions. After I downloaded it, I found out how wrong I was. The 1.0 release fixed every negative comment I had about 0.7. Although it’s not perfect, it’s a lot better than it used to be.

The Good..

  • Variables for defining which row and rowset classes to use in Zend_Db_Table. This enables custom row and rowset classes which I believe is a must-have.
  • Support for compound primary keys
  • Ability to cache the table meta data and prevent unnecessary database access

The Bad…

  • Application level support for foreign keys, namely cascading deletes. This feature is awesome since a lot of the tables I work with are myisam and don’t support cascading deletes. Too bad it doesn’t truly cascade. If you delete a single record, it will cascade that delete to all related tables, but no cascading after that. Basically, a worthless feature that causes more confusion than it helps. Just use MySQL’s Innodb storage engine and clean up the code
  • Complexity. Lots of classes and lots going on.

Post to Twitter Post to Digg Post to Facebook Post to Reddit

Leave a Comment more...