如何实现与NHibernate一个3层模型? [英] How do you implement a 3 layer model with NHibernate?

查看:121
本文介绍了如何实现与NHibernate一个3层模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以传统的3层的方法(不一定是3层):

Taking the traditional 3 layer approach (not necessarily 3 tiers):

UI BLL DAL

UI BLL DAL

如何NHibernate的契合?在大多数情况下,我看到人们让NHibernate的直接填充域对象/实体,这显然需要从NHibernate的引用这些实体。如果域实体是BLL的一部分,这似乎需要从DAL(如NHibernate的所在)的引用,BLL(其中域对象所在)。

How does NHibernate fit? In most cases I see people allowing NHibernate to directly populate domain objects/entities, which obviously requires a reference from NHibernate to these entities. If domain entities are part of the BLL, this seems to require a reference from the DAL (where NHibernate resides) to the BLL (where domain objects reside).

这难道不是违背分离每一层的典型思维,每一层只依赖于它下面的人吗?我缺少的是在这里吗?

Doesn't this go against the typical thinking of separating each layer, with each layer only being dependent on the one below it? What am I missing here?

推荐答案

我可以给你举个例子,我一般是如何与NHibernate层A N层体系结构:

I can give you an example, how I generally layer with NHibernate for a n-tier architecture:

数据访问层

实例映射:

public class CategoryMap : ClassMap<Domain.Entities.Category>
{
    public CategoryMap()
    {
        ...
    }
}

业务层

  • 资料库:BaseRepository

实例的存储库:

public class CategoryRepository : BaseRepository<Domain.Entities.Category>,
    Domain.DataInterfaces.Business.Repositories.ICategoryRepository
{
    public CategoryRepository(ISession session)
        : base(session)
    {
    }
}

  • BaseRepository(CRUD,GetById,GETALL)
    • BaseRepository (CRUD, GetById, GetAll)
    • 实例基础信息库:

      public class BaseRepository<T> : IBaseRepository<T>
      {
          public ISession Session { get; set; }
      
          public BaseRepository(ISession session)
          {
              Session = session;
          }
       }
      

      领域层

      • DataInterfaces(IRepository,IBaseRepository)
      • 实体:BaseEntity
      • 持久性(IEntity,IBaseEnity)
      • DataInterfaces (IRepository, IBaseRepository)
      • Entities : BaseEntity
      • Persistance (IEntity, IBaseEnity)

      所以,这是引用的NHibernate的唯一层实际上是数据层和业务( NHibernate.ISession )。域名层的所有层之间共享,不知道NHibernate的。对于simplicy你可以融合业务和数据层为一层。我一般倾向于它们分开,但是这取决于项目规模。

      So, the only layer which is referencing NHibernate is actually the data layer and the business (NHibernate.ISession). The domain layer is shared between all layers and does not know about NHibernate. For simplicy you could merge the business and data layer into one layer. I generally tend to seperate them, but it depends on the project size.

      如果你真的要分离,我也建议你看看依赖注入减少之间不同的相关性层。

      If you really want seperation, I also suggest you to have a look at dependency injection to reduce the dependencies between different layers.

      希望有所帮助你。

      这篇关于如何实现与NHibernate一个3层模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆