Domain Driven Design and the role of the factory class

后端 未结 6 1795
温柔的废话
温柔的废话 2021-01-29 18:33

I\'am unclear as to what the roles and responsibility of the factory class is. I know enough that the factory class should be resposible for the creation of domain objects (agg

6条回答
  •  不思量自难忘°
    2021-01-29 19:04

    The difference between a repository and a factory is that a repository represents an abstract persistent storage, while a factory is responsible for building an object.

    So, for example, let's say I'm registering a user. I'll get my user object from a factory

    IUser user = userFactory.Create(name, email);
    

    Then pass it to the repository, which will be responsible for dealing with it.

    userRepository.Insert(user);
    

    Factories in DDD can be thought of as a way to hide new, an abstraction of the details of instantiation. It allows you to very effectively program to an interface rather than a concrete.

    In addition, this allows repositories to be focused on their entity type, and thus the use of generics becomes very powerful.

提交回复
热议问题