DDD “View Objects”?

后端 未结 4 691
情歌与酒
情歌与酒 2021-02-14 18:01

Given an application that involves, say, Companies, I might have a Company class. I will have a data access layer that populates a List . However, there will be

4条回答
  •  闹比i
    闹比i (楼主)
    2021-02-14 18:48

    I use a crumble of the entity attributes. For example:

    // interface for "ID" attribute of Company entity
    public interface ICompany_ID {
        Guid CompanyID{get;set;}
    }
    // interface for "Name" attribute of Company entity
    public interace ICompany_Name {
        string Name{get;set;}
    }
    // interface for "Logo" attribute of Company entity
    public interface ICompany_Logo {
        byte[] Logo{get;set;}
    }
    
    // interface for all attributes of Company entity
    public interface ICompany : ICompany_ID, ICompany_Name, ICompany_Logo { }
    
    // base class for classes based on Company entity
    public abstract class CompanyBase : ICompany_ID {
        // implementation of ICompany_ID interface
    }
    
    // class for all attributes of Company entity
    public class Company : ICompany {
        // implementation of ICompany interface (all attributes)
    }
    
    // class for Company name lookup
    public class CompanyNameLookup : CompanyBase, ICompany_Name {
       // implementation of ICompany_Name interfade
    }
    

    This crumble allow me to work with different attributes of different entities and all is type-safe. however, your data-layer must support this scenario.

    The next way is dynamic creation of lookup classes, but it is much more complex. On the other side, it is much more flexible.

    EDIT: Then the selection can be for example:

    var companies = (from c in db.Table()
                     order by c.Name
                     select new CompanyNameLookup { ID = c.ID, Name = c.Name }
                    ).ToList();
    

    or for danymicly created types:

    var companies = (from c in db.Table()
                     order by c.Name
                     select DynamicTypeFactory.New( c.Id ).And( c.Name ).Create()
                    ).ToList();
    

    The DynamicTypeFactory is class with static method New and fluent interface for danymic creation classes at run-time.

提交回复
热议问题