using Repositories from Domain Services

Feb 10 at 7:56 AM
Edited Feb 10 at 8:25 AM

In DDD Document it (Domain Layer) it states

Regarding using Repositories from Domain Services, there are, of course, exceptions. That is the case when we need to obtain data depending on Domain logic states. In that case we will need to invoke Repositories from the Domain Services. But that use will usually be just for querying data.

This is exactly what I am trying to do.

How can I do that? Because DataLayer is not referenced in the Domain layer. Even if I try to do that it will become a Circular dependency.

1) Passing Repository from ApplicationLayer to DomainLayer is that right thing to do?

  Eg:  Method in Domain service which will accept CustomerRepository as parameter

    public void PerformApproval(bool Approved,ICustomerRepository ACustomerRepository)

2) Should I use new third layer which will have reference to both Domain and Datalayer

3)  Or can I use Dependency injection and somehow get the values if so how can I do that?

I can't find any code to access the Repository from the domain layer if you could point me to the right direction that would help me.

 

 

Coordinator
Feb 10 at 10:15 PM

You can call to Repositories from the Domain-Model Layer (from Domain-Services, and only for queries, usually), there's no problem about that.

Why would yo need to add a reference to the Infrastructure-Persistence Layer from the Domain Layer?. You shouldn't do that.

Take into account that if you are using DI (Dependency Injection), the only reference you will have is to the Repositories' Contracts/Interfaces. And those Repositories' interfaces are already within the Domain-Model Layer. Therefore, you don't need to add any reference to the Infrastructure-Persistence Layer (And you shouldn't even if you could, the Domain-Model Layer must be isolated from the Infrastructure Layers). So, NO CIRCULAR DEPENDENCY!! :-)

The procedure is easy, just add the Repository contract/interface to the Domain-Service constructor. Then, the IoC container will inject the Repository object for you, like any other object in the constructor.

Here you have a simple test:

... //using to Repositories' Interfaces/Contracts within the Domain
using Microsoft.Samples.NLayerApp.Domain.MainBoundedContext.ERPModule.Aggregates.CustomerAgg;
...
    public class SimpleDomainService : ISimpleDomainService
    {
        readonly ICustomerRepository _customerRepository;

        public SimpleDomainService(ICustomerRepository customerRepository)
        {
            if (customerRepository == null)
                throw new ArgumentNullException("customerRepository");

            _customerRepository = customerRepository;
        }



        public void CallingRepository(Guid customerId)
        {
            //(CDLTLL - Repository Test)
            var associatedCustomer = _customerRepository.Get(customerId);

            //(Other Domain-Logic)...
        }
    }

Of course, you need to add the mapping into the UNITY container, too:

Container.cs (WCF Project)

_currentContainer.RegisterType<ISimpleDomainServiceSimpleDomainService>();

But, usually I would use Repositories within the Domain-Model Layer, only for querying (if you need to do different actions depending on querys' values, etc.).

On the other hand, all the Transactions and UoW usage should be within the Application Layer, like in our BankTransfer example.

Cheers,

Cesar.

Feb 13 at 12:44 PM

Thanks a lot for that detail explanation.  SPOT ON . First thing to learn from you is your Commitment, you replied at  Fri at 11:15 PM. Hats off even if you are replying form different time zone.

I am still very new as for as DDD is concerned

1) Should I have all the entity business logic in the entity class itself or may I point to different class. (I know it’s silly question).If I want to call different module for a business logic what is the best place to call? Application service/Domain Service/Entities

2) What sought of logic should I have in Domain service compared with the entities itself. Is Domain service mainly a service to call different entities within same module and connecting repository when needed?

4) Is it advisable to call the repositories inside the Entity rather than domain Services because business logic is very much dependent on the Data.

5) I don't understand how the Value object can be helpful.

Because the value object is saved in the same table back in the database of its root entity table

Eg: address is value object and customer is entity. In the database we will only have one table called customer with all the address columns inside it.

 

Thanking you in advance

Regards

Nathan