Choosing the right mapping method

Knowing more than one way of declaring NHibernate mapping may sound good but it may also be confusing. It is important to have knowledge of different methods of mapping but it is equally important to be able to choose the right method that best fits the bill, given a particular job. So how do you go about choosing the right mapping method?

If I am working on a green-field project, then my default stand is to use FNH. This is mainly because there is no legacy database or legacy domain model that I need to work with. So basically, I get all the freedom to define the best domain model that suits my business requirements and then use NHibernate and FNH to drive the design of database from there. This works quite nicely though FNH lacks few mapping features. FNH is most effective when used with auto-mapping feature on. In this mode, I do not have to declare any mapping and FNH would intelligently decide the best mapping for me looking at the domain model. I have worked in teams where there are rules around designing databases. So auto-mapping may not work there. In that case, I would either use FNH conventions to tweak the mappings that FNH generates automatically or resort to FNH's explicitly declared mapping. No matter which route I take, I always find myself most productive with FNH.

But there are times when FNH may not be the ideal solution. One situation that readily comes to my mind is when you are dealing with a legacy domain model and you need to map private/protected fields/properties. While FNH has ways of mapping private/protected fields/properties, it is not the most elegant. I can go down a non-elegant route if I have handful of such properties but for a large number of these, I would probably stick to XML.

XML has the biggest advantage that it supports every mapping feature that NHibernate has. But it is usually difficult to know from the beginning of the project whether you would need those features or not. Still, it is useful to understand XML mappings as I have stated earlier. Knowledge of XML mappings has benefited me immensely in situations when I had mapping defects in my projects. Knowledge of XML mappings has also benefited me in understanding more clearly how NHibernate mappings work.

Lastly, mapping by code – It is wise of NHibernate team to implement a code alternative to XML. If I was not able to use FNH for any reason, I would use mapping by code after considering two things. One, mapping by code cannot map private/protected fields/properties. Second, mapping by code is freshly baked and may fall short of a finished product, so I would be wary of deploying it to production.

There you go! I have not answered the question for you but I have given you enough parameters to consider when choosing the right mapping method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.181.57