1

あなたの助けと注意を前もってありがとう!

私のプロジェクトは学習目的のみに専念しており、DDD と完全に混同しており、次のような状況にあります。

ユーザーとドキュメントが存在する私のドメインには、どこにでもある言語があります。次のように述べています。

- A user can create a document. One of the main purpose of my project is to provide users an ability to create different documents. I mean that the documents cannot exist without the users. So,I think that the process of a document creation belongs to my domain.
- A user can send a document for approval. It's one more thing that belongs to the domain. An approval process is one of the most important part of the project. It has its steps that other users must confirm.
- A user can approve a step of approval process.
- A user can reject a step of approval process.

私の質問を理解し、答えるにはこれで十分です。

User に CreateDocument(params)、SendDocumentForApproval(docId)、ApproveApprovalStepOfDocument(stepId) などのメソッドを含めることができるのは正常ですか?

コードが少し奇妙に見えるので、私は混乱しています。

たとえば、ドキュメント作成プロセスについては、次のようなものがあります。

    public async Task<bool> CreateDocumentCommandHandler(CreateDocumentCommand command)
{

    //We have our injected repositories
    User user = await _userRepository.UserOfId(command.UserId);

    Document document = User.CreateDocoment(command.*[Params for the document]);

    _documentRepostiory.Add(document);

     // It raises event before it makes a commit to the database
     // It gets event from an entity. The entity keeps it as readonly collection.
     // Here it raises DocumentCreatedEvent. This event contains logic which concerns
     // creation some additional entities for the document and log actions.
    await _documentRepository.UnitOfWork.SaveEntitiesAsync();
}

承認プロセス:

//The first try out to model this process:
public async Task<bool> SendDocumentForApprovalCommandHandler(SendDocumentForApprovalCommand command)
{
    //We have our injected repositories

    User user = await _userRepository.UserOfId(command.UserId);

    //Here I have some problems.
    //Is it okay that the method returns the document?
    //The method that is placed inside the User has this logic:
    //public Document SendDocumentForApproval(int docId)
    //{
    //   Document document = this.GetDocument(docId);
    //   
    //   //Inside this method ChangedStatusToApproving is created
    //   document.SetStatusToApproving();
    //   return document;
    //}
    Document document = User.SendDocumentForApproval(command.DocId);

    _documentRepostiory.Upadate(document);

     // It raises event before it makes a commit to the database
     // It gets event from an entity. The entity keeps it as readonly collection.
     // Here it raises ChangedStatusToApproving. This event contains logic which concerns
     // creation some additional entities for the document and log actions.
    await _documentRepository.UnitOfWork.SaveEntitiesAsync();
}
//Is it okay to do something like the command handler above?

//The second one:
public async Task<bool> SendDocumentForApprovalCommandHandler(SendDocumentForApprovalCommand command)
{
    //We have our injected repositories
    User user = await _userRepository.UserOfId(command.UserId);

    //The same one as we have in the previous method.
    //But here I don't want to put the logic about the changing status of the doucnent inside it.
    Document document = User.SendDocumentForApproval(command.DocId);
    //I see that it breaks the method above (SendDocumentForApproval)
    //Now It doesn't mean anything for our domain, does it?
    //It is only getter like User.GetDocument or we can even do it
    //by using repository - documentRepository.DocumentOfId(docId)
    document.SetStatusToApproving();

    _documentRepostiory.Upadate(document);

    await _documentRepository.UnitOfWork.SaveEntitiesAsync();
}

// So, I think the first one is better, isn't it? It follows the ubiquitous language. 

//And here is the final question: Why can't I do it like this:
public async Task<bool> SendDocumentForApprovalCommandHandler(SendDocumentForApprovalCommand command)
{
    //Here we don't want to use the userRepository. We don't need at all
    //Here as a consequence we also don't need a user entity
    //Everything what we need is:
    Document document = _documentRepository.DocOfId(command.DocId);
    document.ForApproval();

    _documentRepostiory.Upadate(document);

    await _documentRepository.UnitOfWork.SaveEntitiesAsync();
}
//I think that the last approach breaks the ubiquitous language and we're about to having an anemic model.
//But here we have only two queries to the database because we don't need a user.
//Which of the approaches is better? Why? How can I do it more correctly if I want to apply DDD?

私の考えをもっと詳しく説明したいと思います。ユーザーを見てみましょう。彼らは文書を管理します。ドキュメントはユーザーなしでは存在できません。ユーザーは、その集計を作成、更新、削除する必要があるため、集計ルートであることを意味しますか。

また、承認プロセスが含まれているため、ドキュメントは集約ルートでもあります。ApprovalProcess は、ドキュメントなしでは存在できません。

次のようなことをする必要があるということですか?

public async Task<bool> SendDocumentForApprovalCommandHandler(SendDocumentForApprovalCommand command)
{
    Document document = _documentRepository.DocumentOfId(command.DocId);

    document.SendForApproval();

    _documentRepository.SaveChangesAsync();//Raise a domain event - SentDocumentForApprovalEvent
}

// Here we have a handler for the event SentDocumentForApprovalEvent
public async Task SentDocumentForApprovalEventHandler(SentDocumentForApprovalEvent sentDocumentForApprovalEvent)
{
    //Now I want to create an approval process for the document
    //Can I do the next thing:
    ApprovalProcess process = new ApprovalProcess(sentDocumentForApprovalEvent.DocId);

    _approvalProcessRepository.Add(process);

    _approvalProcessRepository.SaveEntitiesAsync();//Raise a domain event - InitiatedApprovalProcessEvent

    //Or Should I create the approval process through Document?

    //Looks terrible due to we need to call the repostiory amd
    ApprovalProcess process = Document.InitiateApprovalProcess(sentDocumentForApprovalEvent.DocID);//Static method

    _approvalProcessRepository.Add(process);

    _approvalProcessRepository.SaveEntitiesAsync();

    //log
}

// Here we have a handler for the event InitiatedApprovalProcessEvent
public async Task InitiatedApprovalProcesEventHandler(SentDocumentForApprovalEvent sentDocumentForApprovalEvent)
{
    //The same question as we have with handler above.
    //Should I create steps trough the approval process or just with the help of constructor of the step?

    //log
}

どうもありがとう、そして私のひどい英語でごめんなさい!よろしくお願いします

4

2 に答える 2