views:

1285

answers:

3

Does it make sense to group all Interfaces of your Domain Layer (Modules, Models, Entities, Domain Services, etc) all within the Infrastructure layer? If not, does it make sense to create a "shared" project/component that groups all of these into a shared library? After all, the definition of "Infrastructure Layer" includes "shared libraries for Domain, Application, and UI layers".

I am thinking of designing my codebase around the DDD layers: UI, Application, Domain, Infrastructure. This would create 4 projects respectfully. My point is, you reference the Infrastructure Layer from the Domain Layer. But if you define the interfaces in the Domain Layer project, say for IPost, then you'll have a circulur reference when you have to reference the Domain Layer project from the Infrastructure project when you are defining the IPostRepository.Save(IPost post) method. Hence, the idea of "define all Interfaces in the Shared library".

Perhaps the repositories should not expect an object to save (IPostRepository.Save(IPost post); but instead, expect the params of the object (that could be a long set of params in the Save() though). Given, this could be an ideal situation that shows when an object is getting overly complex, and additional Value Objects should be looked into for it.

Thoughts?

+2  A: 

Hi Eric,

I’m quiet new in DDD so don’t hesitate to comment if you disagree, as you I’m here to learn.

Personally I don’t understand why you should reference the infrastructure layer from your domain. In my opinion the domain shouldn’t be dependent on the infrastructure. The Domain objects should be completely ignorant on which database they are running on or which type of mail server is used to send mails. By abstracting the domain from the infrastructure it is easier to reuse; because the domain don’t know on which infrastructure its running.

What I do in my code is reference the domain layer from my infrastructure layer (but not the opposite). Repositories know the domain objects because their role is to preserve state for the domain. My repositories contains my basic CRUD operations for my root aggregates (get(id), getall(), save(object), delete(object) and are called from within my controllers.

What I did on my last project (my approach isn’t purely DDD but it worked pretty well) is that I abstracted my Repositories with interfaces. The root aggregates had to be instantiated by passing a concrete type of a Repository:

The root aggregate had to be instantiated through a repository by using the Get(ID) or a Create() method of the repository. The concrete Repository constructing the object passed itself so that the aggregate could preserve his state and the state of his child objects but without knowing anything of the concrete implementation of the repository. e.g.:

public class PostRepository:IPostRepository
{
     ...
    public Post Create()
    {
     Post post=new Post(this);
     dbContext.PostTable.Insert(post);
     return post;
     }
     public  Save(post)
     {
         Post exitingPost=GetPost(post.ID);
         existingPost = post;
         dbContext.SubmitChanges();
     }
}

public class Post
{

     private IPostRepository _repository
     internal  Post(IPostRepository repository)
     {
        _repository = repository;
     }
     ...
    Public Save()
    {
     _repository.Save(this);
     }

}
Geo
I know where you are going with this. It's just that in my DDD book, it's mentioned that the repositories are in the Infrastructure layer. But conflicting info I find on the net puts the repositories in the Domain layer.
eduncan911
It is also advocated that the UI-Layer access the "Application, DOmain, and Infrastructure", the Application-Layer access the "Domain and Infrastructure", and finally the Domain-Layer access the "Infrastructure". This is from the book "DOmain Driven Design Quickly". Hence, the reason for this ?
eduncan911
This is hard information to nail down. What I *think* I've figured out is that the *interfaces* for repositories go in the Domain layer since they define a "seam" for the domain. The *implementations" of the repositories go in the Infrastructure layer. Imagine deciding to completely extract your Domain from it's current application context and build a whole new one. You'd want the interfaces of the repositories, but not necessarily the implementations.
jlembke
+3  A: 

I would advise you to consider Onion architecture. It fits with DDD very nicely. The idea is that your repository interfaces sit in a layer just outside Domain and reference Entities directly:

IPostRepository.Save(Post post)

Domain does not need to know about repositories at all.

Infrastructure layer is not referenced by Domain, or anybody else and contains concrete implementations of repositories among other I/O-related stuff. The common library with various helpers is called Application Core in this case, and it can be referenced by anyone.

Alexander Abramov
+2  A: 

Eric, I was away for a couple, so forgive me for answering so late. Concerning where to put the repositories, personally I always put the repositories in a dedicated infrastructure layer (e.g . MyApp.Data.Oracle) but declare the interfaces to which the repositories have to conform to in the domain layer.
In my projects the Application Layer has to access the Domain and Infrastructure layer because it’s responsible to configure the domain and infrastructure layer.
The application layer is responsible to inject the proper infrastructure into the domain. The domain doesn’t know to which infrastructure it’s talking to, it only knows how to call it. Of course I use IOC containers like Structuremap to inject the dependencies into the domain. Again I do not state that this is the way DDD recommends to structure your projects, it’s just the way, I architecture my apps. Cheers.

Geo
Excellent Geobarteam. That just gave me a "duh" moment. Yes, define interfaces in the Domain, repositories should be implemented in seperate assemblies (MySqlProviver, MsSqlProvider, XmlProvider, etc). And some type of IOC Container (Castle Windsor I love) used to wire it up at App layer. Perfect.
eduncan911
In the case of ASP.NET MVC, it's actually easy to inject the repository into the Controller (UI layer) via Castle Windosr. Steven Sanderson had a good example of this in ASP.NET MVC Framework Preview. The Domain-Driven Design Quickly book I have says that UI, App, and Domain can all use the Infra.
eduncan911
The only issue I have with this is that my book says that Infrastructure never references anything. UI->App, Domain, and Infra. App->Domain and Infra. And, Domain->Infra. I know I know, it is all suppose to be guidelines anyhow.
eduncan911