Maritim
Maritim

Reputation: 2159

The placement of utility classes in DDD

I'm trying to figure out where to put my utility classes in a DDD based project. The case is a follows:

I have a class called CookieAwareWebClient which is required for the core to work. Form what I've read online this seems like a class that belongs in the Infrastructure tier, however one is not supposed to refer to the Infrastructure tier from the Core tier. This means that I cannot place functionality that the Core tier depends on in the Infrastructure tier. Where then should this CookieAwareWebClient class be placed?

Upvotes: 2

Views: 3369

Answers (1)

Chris Simmons
Chris Simmons

Reputation: 7266

Without understanding exactly what you need to do, I believe that @plalx sums it up in the comments:

  • Establish an interface which provides the functionality that your Core tier requires
  • Implement this interface in CookieAwareWebClient
  • Use Dependency Inversion to allow the Core to use CookieAwareWebClient

Here's some code (C# in this case) with constructor injection as an example:

The interface:

namespace Core
{
  public interface IBlah
  {
    int SomethingCoreNeeds();
  }
}

The implementation by CookieAwareWebClient:

namespace Services
{
  public class CookieAwareWebClient : IBlah
  {
    // ... rest of class 
    private int _somethingCookieAwareWebClientHasThatCoreNeeds;

    public int SomethingCoreNeeds()
    {
      return _somethingCookieAwareWebClientHasThatCoreNeeds;
    }
    // ... rest of class 
  }
}

The consuming service in your Core:

namespace Core
{
  public class DomainService
  {
    private readonly IBlah _blah;

    public DomainService(IBlah blah)
    {
      _blah = blah;
    }

    public void DoSomething(DomainEntity entity)
    {
      entity.NeededValue = _blah.SomethingCoreNeeds();
    }
  }
}

Upvotes: 1

Related Questions