【问题标题】:ISession/UnitofWork in Service Layer within Windsor tutorialWindsor 教程中服务层中的 ISession/UnitofWork
【发布时间】:2011-04-25 21:02:25
【问题描述】:

我正在尝试使用this tutorial 作为框架的基础来构建一个真实的应用程序。我了解 MVC,但对整个 IOC/NHibernate 世界还是陌生的。在阅读了关于 SO 的一些问答之后,我正在考虑在控制器和存储库之间添加一个服务层,因为我将添加一些业务规则验证。

github 上的源代码也有一个“ServiceInstaller”,它被证明非常有用,因为它允许我向应用程序添加任何服务,即

public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(AllTypes.FromThisAssembly().Pick()
                            .If(Component.IsInSameNamespaceAs<SectionService>())
                            .Configure(c => c.LifeStyle.Transient)
                            .WithService.DefaultInterface());
    }

我的问题是针对本教程的,基本上我不确定 ISession(即 UoW)是否从服务层传递到存储库,或者是否有其他方法。

这是我目前所拥有的:

// Controller

    public class SectionsController : Controller
    {
        public ILogger Logger { get; set; }
        private readonly ISectionService sectionService;

        public SectionsController(ISectionService sectionService)
        {
            this.sectionService = sectionService;
        }

        public ActionResult Index()
        {
            return View(sectionService.FindAll());
        }

    // other action methods
    }

// Service Layer

    public class SectionService : ISectionService
    {
        private ISectionRepository repository;

        public SectionService(ISession session)
        {
            this.repository = new SectionRepository(session);
        }

        public IQueryable<Section> FindAll()
        {
            return repository.FindAll();
        }

    // other methods
    }

// Repository
    public class SectionRepository : ISectionRepository
    {
        private readonly ISession session;

        public SectionRepository(ISession session)
        {
            this.session = session;
        }

        public IQueryable<Section> FindAll()
        {
        return session.QueryOver<Section>().List().AsQueryable();
        }

    // other CRUD methods

    }

这是正确的实现方式吗?

【问题讨论】:

    标签: asp.net-mvc nhibernate castle-windsor


    【解决方案1】:

    示例应用以这种方式实现是有原因的。嗯,实际上有两个原因。

    第一个原因是它相对简单,而且还没有足够的逻辑来保证单独的层。

    其次,这种控制器--> 服务--> 存储库--> ISession 抽象是没有意义的,不会向表中添加任何内容。他们所做的唯一一件事就是增加应用程序的复杂性和您无益的工作量。

    Ayende 有一个不错的、最近的一系列关于它的博文,我强烈推荐。 (here's the first of them,其次是其他几个)。

    你有什么样的现实世界要求来保证这两个额外的层?

    最后,YAGNI 和 KISS。

    【讨论】:

    • 答案是“我想不到”。我读过 Ayende 的帖子(包括this one),并不反对。然而,作为 NHibernate/IOC 的新手,人们倾向于寻找“最佳实践”,并且绝大多数帖子和代码示例都使用存储库/服务层。后续问题:我知道您非常忙,但是您是否有机会在不久的将来为您出色(且简单)的教程添加其他部分(并且可能会取消示例 EventsRepository)?
    • @seekay,我没有计划在最近的将来进行这项工作。但是,我们对想法持开放态度。此外,@Hadi Eskandari(推特上的@heskandari)是另一位致力于本教程的人,所以如果您有一些好的想法,他可能会有一些时间将它们付诸实践。
    猜你喜欢
    • 2017-01-07
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多