views:

270

answers:

4

Hi,

I am starting with DDD. I am a bit confused with the interaction between the several layers involved in a DDD application.

Can I call my repositories from my presentation layer? If not do I have to replicate the CRUD functionality provided by the repositories in my service layer (which ofcourse will in turn use the repository for these functions)? What would be the best way to do this?

A: 

Well I usually use a service layer but there is no harm in using your repository

Yassir
A: 

Absolutely, you can call a repository from the presentation layer. My one piece of advice is to have your presentation layer depend on an abstraction of a repository as opposed to the implementation, i.e. UserSession depends on an IPersonRepository interface instead of a PersonRepository class. Not only is it a good separation of concerns, but it can make testing easier.

Go for it!

Kevin Swiber
I would add that this also makes it easy to inject dependencies, and use an IoC framework like Ninject, which in turn makes it easy to write tests.
mgroves
I didn't make it clear in my question but I was referring to the code behind in ASP.NET page as the presentation layer in this case. Would u still say its OK to call up the repository (i.e. through an interface ofcourse)?
Ali Kazmi
@Ali: My advice is to choose a presentation pattern that suits your needs. Using a controller or presenter, you would never need to call a Repository directly from a code-behind. Dino Esposito has a great MSDN Magazine article on ASP.NET presentation patterns. (http://msdn.microsoft.com/en-us/magazine/dd252940.aspx) You may benefit most from using the MVP pattern with Web forms. Good luck!
Kevin Swiber
+1  A: 

What exactly do you mean by presentation layer? If you mean the Controller/Presenter, then it's perfectly fine. The rule of thumb that I've used is if the controller action is 4 lines of code or more I should look at refactoring to an application service class but still - both are at the application level and you can certainly use repositories there.

The way you phrased your question - and I could certainly be misinterpreting this - sounded suspiciously like you were thinking of referencing the repository from your view or codebehind. In that case I would say no! no! no!

George Mauer
Ya that's the case. I was talking of referencing it from my codebehind
Ali Kazmi
Ok then yes, that is bad. The codebehind is not the presentation layer, the codebehind is actually a part of your view. In standard web terms the codebehind should be looked at in the same way as you look at very simple javascript. Move any logic into a Controller or Presenter class and inject your respositories into it via the constructor.
George Mauer
So to clarify - what you're saying is bad because you're referencing the repository in the View layer, not the presentation layer
George Mauer
A: 

As Kevin says, going with a controller or presenter gives you many benefits.

These includes clear separation of concerns as well as testability.

Personally, in an ASP.NET setting, I'd go for a «passive view» Fowler.

You simply design your page contracts as interfaces which have concrete implementations as web forms, and then you just stub/mock those when testing your presenters.

The only drawback that comes to mind is that you end up with more code than in a "traditional" web form solution.

Martin R-L