tags:

views:

37

answers:

1

I am working on my first NHibernate project, and have the following setup/requirement.

I have 2 projects xx.Data, and xx.Business. In the Data project, this is where my domain classes, mapping files are located. What I am doing is creating an online application, whereby I have a SQL table that stores the ID, modified date, and an XML field which stores the application form details. This is mapped to an EnrolmentApplication domain class.

The xml is deserialised into an ApplicationForm class, which at the moment, is in the xx.Business project. The business project references the Data project.

I started out with my code creating a new ApplicationForm, serialising to xml, then setting the EnrolmentApplicaiton property to the xml string, and saving, which I think is a terrible way of doing things.

I next then thought that I should have a property in the EnrolmentApplication class that has get/set properties to serialise/deserialise the xml property for me, however, I would either have to move the ApplicationForm class into the Data project, or create another class that inherits from EnrolmentApplication, i.e.

using xx.Data;
namespace xx.Business
{
public class EForm : EnrolmentApplication
{
    public EForm(Guid applicationID) : base(applicationID)
    {}
}
}

My issue with this, is in loading up an EnrolmentApplication from the database. I do not want to do the following:

EnrolmentApplication form = NHibernateSession.Linq<EnrolmentApplication>().SingleOrDefault(x => x.appID);
this.ApplicationXML = form.ApplicationXML;
this.DateModified = form.DatModified;
...

I could make this simple by moving the ApplicationForm class into the Data project, but I would like to keep them separate, as the business class is where the rules for an applicaton form should really reside.

A: 

Why have the EnrolmentApplication class in the Data project?

NHibernate is about mapping your domain layer (with corresponding business objects) to your database.

So you should have two projects:

  • Domain Project - business enitities
  • Data Project - Mapping files and repositories

Your data project should reference the domain project, not the other way round.

After that you could have a service layer, or application layer or UI layer whatever you want, which would obviously reference the domain project and at some point fire off the data projects mappings.

If you want to have your ApplicationForm class serialized into XML, I would create an IUserType to do this (either a generic IXmlSerializedUserType or less generic IApplicationFormUserType will do the trick).

You can then have the ApplicationForm (in unserialized form) as a property of your EnrolmentApplication class, which means the fact that it is serialized to XML is hidden from your application layer and is only the concern of the data layer.

cbp