0

I have this problem... I have a VS solution with these projects: Persistance, Domain, Services, UI. Now my problem is that I must reference nhibernate in all project that uses something of nhibernate. Is it possible that I reference nhibernate only in Persistence project and that any project that have reference to Persistence project can use nhibernate too?

I am using StructureMap as DI container. And I have setup dependency injection through constructor for ISession. So I must reference nhibernate in every layer (not UI) that passes ISession. What I want is not have nhibernate.dll and all its dependency dll (bytecode.linfu...) referenced in nearly all my projects, but only in persistence. Is this somehow possible?

Thanks

Sly
  • 15,046
  • 12
  • 60
  • 89
Luka
  • 4,075
  • 3
  • 35
  • 61

1 Answers1

1

In your Domain projects, you define the interfaces for your data acces objects. Your NHibernate persistence project can reference the Domain project and provide implementation for the data access objects.

Your service project might reference Domain and not Persistence. Your service objects depend on the data access interfaces in Domain. You use your DI container to wire your service objects to the NHibernate implementations in Persistence.

Changing the dependency Domain -> Persistence to Persistence -> Domain is an example of inversion of control.

I can imagine you now have the following service:

using Persistence;
using Domain;

public class UserService
{
  private Persistence.NHibernateUserRepository _repository;
  public UserService (ISession session)
  { 
     _repository = new Persistence.NHibernateUserRepository(session);
    // ...
  }

  // some service methods
}

I suggest to change this to:

using Domain;  // no longer using Persistence package

public class UserService
{
  private Domain.IUserRepository _repository;
  public UserService (Domain.IUserRepository repo)
  { 
    _repository = repo;
    // ...
  }

  // some service methods
}

In your StructureMap configuration, you configure an NHibernate Session, which you wire to your Persistence.NHibernateUserRepository. Then you wire your UserService to this Persistence.NHibernateUserRepository. I'm not familiar with StructureMap, so I can't help you with the mechanics. You might want to read:

Community
  • 1
  • 1
Marijn
  • 10,367
  • 5
  • 59
  • 80
  • +1 This interface is often called [IRepository](http://stackoverflow.com/questions/2347337/database-abstraction-layer-design-using-irepository-the-right-way) – k3b Mar 06 '11 at 12:11
  • Ok, I am using Repositories that are located in Persistence layer, in domain layer are ma POCO entities and DTOs, Problem is in the service layer, i must reference nhibernate because of this: the service layer uses repositories to get entities. but repositories needs ISession injected in constructor, and so I need nhibernate in services. My question was is it possible to somehow propagate dependeny from one project to another without directly add references into project. Somthing like Project2, uses project1, that uses nhibernate. So project 2 can use nhibernate too. Is this possible? – Luka Mar 06 '11 at 12:24
  • "but repositories needs ISession injected in constructor, and so I need nhibernate in services" - I don't think so. Your service needs a repository and should not care whether the repo is implemented using NHibernate or plain ADO.NET, for instance. – Marijn Mar 06 '11 at 13:49
  • Ok, I see your point, Now I have UserService with constructor that take a IRepository. but repositories are in my persistence. But I will refactor my solution to your example of inversion of control – Luka Mar 06 '11 at 13:55