DDD - Dependencies between domain model, services and repositories

前端 未结 6 2060
旧时难觅i
旧时难觅i 2020-12-23 10:32

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

6条回答
  •  渐次进展
    2020-12-23 11:10

    In agreement with Marcelo, you should probably first determine if the product ID is really a domain model concept. If the business users never use or have any idea of the product ID and usually refer to the product by name, number, SKU or a natural product ID made up of name + dimensions, then this is what the domain model should be aware of.

    That being said, here is how I structure my DDD projects assuming that the product ID is an auto-number field in the database:

    Project.Business (Domain Model)

    No references and therefore, no dependencies upon anything.

    public class Product : Entity
    {
        private Product(string name, Address address)
        {
            //set values.
        }
    
        //Factory method, even for simple ctor is used for encapsulation so we don't have 
        //to publically expose the constructor.  What if we needed more than just a couple           
        //of value objects?
        public static CreateNewProduct(string name, Address address) 
        {
            return new Product(name, address);
        }
    
        public static GetAddress(string address, string city, string state, string zip) { }
    }
    
    public interface IProductRepository : IEnumerable
    {
        void Add(Product product);
        //The following methods are extraneous, but included for completion sake.
        int IndexOf(Product product);
        Product this[int index] { get; set; }
    }
    

    Project.Implementation

    public SqlProductRepository : List, IProductRepository
    {
        public SqlProductRepository(string sqlConnectionString) { }
    
        public void Add(Product product)
        {
            //Get new Id and save the product to the db.
        }
    
        public int IndexOf(Product product)
        {
            //Get the index of the base class and convert to business object.
        }
    
        public Product this[int index]
        {
            get { //find instance based on index and return; }
            set { //find product ID based on index and save the passed in Business object to the database under that ID. }
        }
    }
    

    Project.ApplicationName (Presentation Layer)

    public class Application
    {
        IProductRepository repository = new SqlProductRepository(SqlConnectionString);
    
        protected void Save_Click(object sender, EventArgs e)
        {
            Product newProduct = Product.CreateNewProduct(name, Product.GetAddress(address,city,state,zip));
            repository.Add(newProduct);
        }
    }    
    

    If necessary, you may have:

    Project.Services (Application Services Layer that uses DTOs between itself and the Presentation Layer)

提交回复
热议问题