Is it possible to have no references of NHibernate in my Services project?
I have a Data project that has all my nhibernate mappings, and my Repositories like:
public class Repository<T> : IRepository<T>
{
private readonly ISessionFactory _sessionFactory;
public Repository()
{
}
public Repository(ISessionFactory sessionFactory)
{
this._sessionFactory = sessionFactory;
}
}
I then have specif开发者_如何学编程ic repositories that derive from this class like UserRepository etc.
public class UserRepository : Repository<IUser>, IUserRepository
{
}
I'm using StructureMap to wireup the dependancies.
In my Services project, I don't want to reference anything nhibernate related (if that's possible), to make it ORM independent.
The problem is, I have to pass down ISessionFactory in my services class which the Repositories will use correct?
In my web application (MVC), I ONLY want to reference my services project, not my nhibernate Data project.
Is this setup possible?
Here is an interesting blog post from the nHibernate guru Ayende Rahien: "In short, I am completely opposed for even trying doing something like that."
The problem is, I have to pass down ISessionFactory in my services class which the Repositories will use correct?
That shouldn't be the case. The service layer shouldn't depend on any ISessionFactory. It should depend only on your repositories (the abstractions more precisely). It is your repositories that will require an ISessionFactory or more precisely the NHibernate implementation of those repositories.
So no need to reference any NHibernate specific stuff in the service layer.
In my web application (MVC), I ONLY want to reference my services project, not my nhibernate Data project.
That doesn't make any sense. In yuor ASP.NET MVC project you have to reference everything, otherwise if the implementation assembly is not referenced it won't be in the bin folder and your application won't work.
So reference it in the ASP.NET MVC application but the only place where you will actually use it is the configuration of your DI container, nowhere else. You may consider the cnofiguration of the DI container as infrastructure code. All controllers and services will still work with abstractions and never know about NHibernate specific classes.
精彩评论