views:

173

answers:

1

Hi all,

I think I am at a impasse here. I have an application I built from scratch using FluentNHibernate (ORM) / SQLite (file db). I have decided to implement the Unit of Work and Repository Design pattern. I am at a point where I need to think about the end game, which will start as a WPF windows app (using MVVM) and eventually implement web services / ASP.Net as UI.

Now I already created domain objects (entities) for ORM. And now I don't know how should I use it outside of ORM. Questions about it include:

  • Should I use ORM entity objects directly as models in MVVM? If yes, do I put business logic (such as certain values must be positive and be greater than another Property) in those entity objects? It is certainly the simpler approach, and one I am leaning right now. However, will there be gotchas that would trash this plan?
  • If the answer above is no, do I then create a new set of classes to implement business logic and use those as Models in MVVM? How would I deal with the transition between model objects and entity objects? I guess a type converter implementation would work well here.
+2  A: 

To answer the first part of your question, yes your business logic and validation should go in your entities. The point of NHibernate is to let you design your entities to be persistence ignorant. That means that you should, whenever possible, be designing your entities as you would if you didn't care about persistence. This isn't entirely feasible as you'll soon find out (you'll need to make your properties virtual in order to support lazy loading and if you want to use NHibernate Validator you'll be decorating your properties with validation attributes), but for the most part NHibernate does a good job of staying out of your way.

As for whether to use your entities as the models, you'll get mixed reviews on that. Ideally, you would create separate viewmodel classes and map from your entities to the viewmodel so that your views will only access to the bare minimum of information they need. This also goes a long way in preventing N+1 access issues. However, doing so is often a huge pain. Granted, there are tools like AutoMapper that will make it easier from transposing your entity properties to a viewmodel.

Kevin Pang
I didn't know about AutoMapper, thanks! With it, it makes perfect sense (and efficient) to create separate ViewModel classes and use AutoMapper to map their properties to Entity class properties. A simple flatten will likely work (or minor projection).The 2nd part I know is trickly, and requires someone with solid NHibernate / Unit of Work design pattern knowledge. If you want to take a stab at that I'd appreciate it!
Echiban
Yeah, I'm not versed enough with NHibernate to tell you how best to set it up. For what it's worth, I've seen a ton of different ways to set up the ISessionFactory, ISession, and ITransaction. Personally, I like working directly with those and passing in an ISession into my Repository classes that need it using an Inversion of Control container. As far as setting it up, I'd go off of the FluentNHibernate documentation as it's pretty lean and doesn't introduce complexities with adding a unit of work layer.
Kevin Pang
I recommend you the CodeCampServer (http://code.google.com/p/codecampserver/). There are a vergy good implementations using NHibernate, AutoMapper and IoC (StructureMap)
Rookian
If you're interested in how I've personally set up Fluent NHibernate, take a look at my post here: http://www.kevinwilliampang.com/2010/04/06/setting-up-asp-net-mvc-with-fluent-nhibernate-and-structuremap/
Kevin Pang