【发布时间】: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