Just wanted to know how others have layered their architecture. Say i have my layers as follows:
Domain Layer
--Product
--ProductService (Should the imp go into this layer?)
--IProductService
--IProductRepository
Infrastructure Layer
--ProductRepository (Imp of IProductRepository in my domain)
Now when a new product is created i have a requirement to assign a product id by calling into the ProductService.GetNextProductId() method.
Because the service has a dependency on the repository i set up the ProductService ctor with an interface of IProductRepository which can be injected later. something like this:
public class ProductService : IProductService
{
private IProductRepository _repository;
public ProductService(IProductRepository repository)
{
_repository = repository;
}
public long GetNextProductId()
{
return _repository.GetNextProductId();
}
}
My issue is that when i use the service in the Product Class i am making reference to the Repository in the ctor when instantiating a new ProductService class. In DDD its a big no no to have such a reference. I' am not even sure if my product domain class is being set up correctly to call the service, can someone pls advise:
public class Product : Entity
{
private ProductService _svc;
private IProductRepository _repository;
public Product(string name, Address address) //It doesnt seem right to put parm for IProductRepository in the ctor?
: base(_svc.GetNextProductId) // This is where i pass the id
{
// where to create an instance of IProductRepository?
}
}
How can i elegantly solve this design issue? I am open to suggestions from experienced DDD'ers
EDIT:
Thanks for you comments. I also doubted if the service should be called from the Product Class. I have not used a factory pattern (yet) as the construction of the object is still simple. I dont feel it warrants a factory method yet?
I' am confused...Putting the ProductId aside if my Product class needed some other data from a Service e.g GetSystemDateTime() (i know, bad example but trying to demonstrate a non db call) where would this service method be called?
Services in DDD are logic dumps where the logic is not natrual to the domain object, right? So How does it glue together?