Learning some new tools

I an re-writing the London .NET User Group website which is old and long in the tooth. I like these personal projects because they form a great learning opportunity. I can explore new tools and frameworks without worrying about exposing an employer to ‘learning on the job‘.  I have a number of learning goals on this project:

  • ASP.NET MVC. I am using Monorail commercially and I am looking forward to getting a feel for the differences.
  • Spark View Engine. I prefer templates that do not obscure the structure of the html and I’m looking forward to taking this one out on the road.
  • SpecUnit. I have wanted to give an example-driven development tool a run out for a while now as part of efforts to pick up from the BDD guys.
  • Fluent NHibernate. I’m a Castle Active Record user, but I want to find out if Fluent NHibernate offers me anything more.

I’ll probably give some feedback on all of these over the weeks, including fluent NHibernate in my NHibernate series.

Trial and Error

One really interesting idea is Fluent NHibernate’s Automapping. Fluent NHibernate is a great example of what is possible with static reflection. However, I found myself a little bit cool on automapping at first. I found it a little hard at first not knowing what the mappings were. I’m used to using Castle Active Record whose close association between attribute and mapped property or class makes this fairly explicit. I have always been less concerned by friction from representing orthogonal concerns as attributes so it took me a while to before I started to enjoy Fluent NHibernate’s ClassMap. Automapping pushed my comfort out even further because it was even more ‘under the hood’. Getting more comfortable here was a multi-step process.

  • The first was using the explicit ClassMap files to become confident with the mapping, before tacking automap. Small steps.
  • The second step was to hook up log4net (a good step with NHibernate anyway). Maybe its just reflex but when I get a persistence error one of the first things I want to do is look at how I was mapping. This meant I could refer to the generated xml for the mapping when I set the logging level to DEBUG. I generate the Db from the domain model using a separate console project I just run from the command line, so this simple step meant I could recover the feedback I wanted as to the mappings from that. That helped me see my mistaken assumptions or confirm that the automap was doing what I wanted.
  • The last step is to use the built in support for integration testing that the Fluent NHibernate project provides. That gave me confidence that the mapping was what I wanted. James Gregory shows how here.

Finally, I suspect that you may need to be careful if your domain has a lot of exceptions. Exceptions may end up being confusing. I’m not sure that altering the conventions for the mappings that differ may not end up being less confusing than just explicitly mapping in the long run. I suspect there is a trade off here. If most of your classes conform to your conventions and you only have one or two exceptions this may be the way to go, but if you have a lot of variants then this may not be so helpful and you might be better off just using the explicit ClassMap<>.

My one oustanding concern is how people  supply automap with criteria for the classes to map from the assembly. Your domain assembly will contain a lot of types which will be transient and not persisted. So you have to provide some selection criteria to pick the types you want to map out of the assembly. The danger here is that to make my selection criteria you might adopt a strategy of deriving from a base class or interface which does not have any value in your domain but is just a ‘marker’ for persistence. This reduces my adherence to Persistence Ignorance principles because my entity ends up being designed to meet the needs of a mapping technology. I suggest preferring to use criteria such as namespaces here.

 

About Ian Cooper

Ian Cooper has over 18 years of experience delivering Microsoft platform solutions in government, healthcare, and finance. During that time he has worked for the DTi, Reuters, Sungard, Misys and Beazley delivering everything from bespoke enterpise solutions to 'shrink-wrapped' products to thousands of customers. Ian is a passionate exponent of the benefits of OO and Agile. He is test-infected and contagious. When he is not writing C# code he is also the and founder of the London .NET user group. http://www.dnug.org.uk
This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://blog.fohjin.com Mark Nijhof

    Fluent NHibernate offers you less ;) no persistence logic in your entities, a good thing imho. Looking forward to your coming posts.

  • Ian Cooper

    @Mark

    Sure though there are some trade-offs such as flipping between locations to see how something was mapped.

    But for sure there is an aesthetic pleasure to clean domain classes.

    I suspect the growth of static reflection will mean we start to attribute less going forward and use mapping files to handle the work.

    I guess right now I am comfortable because I use it a lot. It’s hard to get back to viewing attributes with fresh eyes.

    But from here I suspect the question will become ‘how can I use static reflection mapping files to solve this’ instead of ‘how can I use reflection and attributes’.

  • http://dualbotic.com/dasBlog Charlie Barker

    I have so far only used Castle / Active Record, I chose Nvelocity over Brail or AspView but I agree whole
    heartedly that a templating engine the obscures what your templating is not helpful.
    I’m anxious to read about your experience of MS MVC vs Monorail. We have some projects coming up that would be best tackled using an MVC pattern so we will have to pick one of the two.
    +1 to updating the DNUG site as well :) , I have just become a dad so attending meetings has been impossible but I hope things will settle down soon.

  • http://ywpyzjft.com/ Mogimcog

    6y1Yef