DDD - Problems in the Designer and Object Design Methods

I have a problem with entity design. As I read, when you create a DDD object, the constructor should contain the values ​​necessary for the entity to exist. For example, in the domain I'm working on, a class object cannot exist without a section and level:

public class Class
{
    public Class(short id, string section, Level level)
    {
        ID = id;
        Section = section;
        Level = level;
    }
    //
    // Properties
    //
    public short ID { get; private set; }
    public string Section { get; private set; }
    public Level Level { get; private set; }
    //
    // Methods
    //
    public static IList<Class> GetClassesByTeacher(short teacherID)
    {
        List<Class> classes = new List<Class>();
        classes.Add(new Class(1, "a", null));
        classes.Add(new Class(2, "b", null));
        classes.Add(new Class(3, "c", null));
        return classes;
    }
}

Here Level is also an entity. Since I am not finished in design, the Level contructor may also contain the SchoolYear entity. I am concerned about calling the GetClassesByTeacher method, I need to create an instance of the class along with other objects (Level, as well as SchoolYear, necessary in the level constructor).

? , , . ? , , , . , CQRS , , , , - , CQRS, , DDD, ? ?

+4
2

GetClassesByTeacher . Class . (, ) - . A ClassRepository Class .

, Class Level. . DDD . :

EDIT:

, ?

, , A B, , A B.

, , ( Class, Level, , , ..) (). , ?

.

  • ?. . , , .
  • ? , , . .

. , DDD, !

, , , Entity . - Entity , DAL?

( - , ). - "" . , . , , , , , .

+3

, , - .

(SRP), . , , "", "" , , , ... "" , "" .

, . - , .

:

  • 'Class'

:

public class Class
{
    public Class(short id, string section, Level level)
    {
        ID = id;
        Section = section;
        Level = level;
    }
    //
    // Properties
    //
    public short ID { get; private set; }
    public string Section { get; private set; }
    public Level Level { get; private set; }
}



public class ClassRepository
{
    private IList<Class> contents;

    //
    // Methods
    //
    public IList<Class> GetClassesByTeacher(short teacherID)
    {
        List<Class> classes = new List<Class>();
        for (Class elem: contents) {
            if (elem.getTeacher().equals(teacherID) {
                classes.Add(elem);
            }
        }
        return classes;
    }
}

repository = new ClassRepository;
level1 = new Level();
repository.Save(new Class(1, "a", level1));
repository.Save(new Class(2, "b", level1));
repository.Save(new Class(3, "c", level1));

result = repository.GetClassesByTeacher(1);

, ClassRepositoryInterface InMemoryClassRepository, , , .. ...

Java, , , .

+4

Source: https://habr.com/ru/post/1544003/


All Articles