nhibernate in solution
I have 开发者_Go百科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
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:
- Injecting ISession Into My Repositories Using Structuremap in a Asp.Net MVC Application
- http://www.bengtbe.com/blog/post/2009/02/27/Using-StructureMap-with-the-ASPNET-MVC-framework.aspx - this is a post that discusses exactly your problem, including StructureMap, NHibernate and asp.net mvc.
精彩评论