I’m trying to build a real-world app using this tutorial as a basis for the framework. I understand MVC, but am new to the whole IOC/NHibernate world. After reading a few Q&A here on SO, I am thinking of adding a Service layer between the controller and the repository as I’ll be adding some business rule validations down the line.
The source on github also has a ‘ServiceInstaller’ that proved really useful as it allows me to add any services to the application i.e.
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(AllTypes.FromThisAssembly().Pick()
.If(Component.IsInSameNamespaceAs<SectionService>())
.Configure(c => c.LifeStyle.Transient)
.WithService.DefaultInterface());
}
My question is specific to this tutorial, and its basically that I’m not sure if the ISession (which is the UoW) is passed from the Service layer to the Repository, or if there’s another approach.
Here’s what I have so far:
// 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
}
Is this correct way to implement this?
There’s a reason why the sample app is implemented that way. Well, actually two reasons.
First reason that it is relatively simple and there’s not enough logic to warrant a separate layer yet.
Second is, that this kind of controller –> service –> repository –> ISession abstractions are pointless and add nothing to the table. Only thing they do is increase the complexity of the app and amount of work you do for no benefit.
Ayende has a nice, recent, series of blogposts about it which I highly recommend. (here’s the first of them, followed by few others).
What sorts of real-world requirements do you have that would warrant those two additional layers?
In closing, YAGNI and KISS.