Search code examples
nhibernatearchitecturefluent-nhibernatedatasourcedata-access-layer

Using nHibernate as ORM for multiple sources?


I've been using Fluent nHibernate with success on many web<->db applications and like it a lot. Using only a database as a source is pretty straightforward with only challenges in defining your domain model, create conventions, mappings etc.

Now however I face a different approach and would like some input before concidering using nHibernate.

Now there is three or more different datasources in the persistence layer and sometimes you will write/read to one of them or sometimes to all of them. I suppose a unit of work pattern is good to solve this.

  • 0..1 Database
  • 0..* files on the file system
  • 0..* hardware

And so there would be entities coming from different sources, some of them created manually, other mapped from the DB.

Any good ideas of how to approach this scenario, and would it still make sense to use repositories with nHibernate, working towards different sources?

My own thoughts:

Separate the db/nhib part with the other sources and build a repository or facade upon that (to expose to BL). This knows how to work with the different sources. Probably need to create a new definition of entities as they might contain data from different sources and presented as "one". This way I could still use nHib for the db access and write some other DAL code for the other parts and hide the implementation from the BL.

Would that make sense, or could it be solved in a better way using nHib?


Solution

  • Different Entities have different sources

    a facade like Repositories, UnitOfWorkClass or a custom SessionWrapper implementing ISession would be enough. the tricky part would be transactions on sources like files

    When Entities have some properties from different sources

    i would use IUserTypes like here

    Entities are mixed from different sources

    i would introduce Wrapperclasses with inner objects holding the state for each datasource maybe with the corresponding unitofwork-object