「タスク」という名前のエンティティがあります。このエンティティに対して、「コメント」と呼ばれる複数のエンティティを作成できます。「CreateComment」という名前のメソッドも必要です。Domain Driven Design では、"Task" クラスのインスタンスを作成しないとエンティティ "Comment" は存在できません。私の質問は、このメソッドをどこに配置する必要があるかです: Task クラスまたは Comment クラスですか? Comment.CreateComment または Task.CreateComment のようにする必要があります。このメソッドを Task クラスに配置すると、単一責任の原則違反になりますか?
2 に答える
1
メソッドはTask
エンティティにある必要があると思います。しかし、そうは言っても、コメントを作成するのはオブジェクトの責任であるとは思わないので、メソッドはそうすべきではありCreate
ません。代わりに、私はこのようなものを使用します.Add
Task
タスククラス、かなり自明です
public class Task
{
private readonly IList<Comment> Comments = new List<Comment>();
public void AddComment(ICommentBuilderFinalization commentBuilder)
{
Comments.Add(commentBuilder.MakeComment());
}
}
コメントクラス、かなり自明です
public class Comment
{
public string Text { get; set; }
public string PostedBy { get; set; }
public DateTime PostedAt { get; set; }
}
オブジェクトビルダーとプログレッシブ流暢なインターフェース
// First progressive interface
public interface ICommentBuilder
{
ICommentBuilderPostBy PostWasMadeNow();
ICommentBuilderPostBy PostWasMadeSpecificallyAt(DateTime postedAt);
}
// Second progressive interface
public interface ICommentBuilderPostBy
{
ICommentBuilderPostMessage By(string postedBy);
}
// Third progressive interfacve
public interface ICommentBuilderPostMessage
{
ICommentBuilderFinalization About(string message);
}
// Final
public interface ICommentBuilderFinalization
{
Comment MakeComment();
}
// implementation of the various interfaces
public class CommentBuilder : ICommentBuilder, ICommentBuilderPostBy, ICommentBuilderPostMessage, ICommentBuilderFinalization
{
private Comment InnerComment = new Comment();
public Comment MakeComment()
{
return InnerComment;
}
public ICommentBuilderFinalization About(string message)
{
InnerComment.Text = message;
return this;
}
public ICommentBuilderPostMessage By(string postedBy)
{
InnerComment.PostedBy = postedBy;
return this;
}
public ICommentBuilderPostBy PostWasMadeNow()
{
InnerComment.PostedAt = DateTime.Now;
return this;
}
public ICommentBuilderPostBy PostWasMadeSpecificallyAt(DateTime postedAt)
{
InnerComment.PostedAt = postedAt;
return this;
}
}
すべてを一緒に入れて
var task = new Task();
var commentBuilder = new CommentBuilder().PostWasMadeNow().By("Some User").About("Some Comment");
task.AddComment(commentBuilder);
わかりました。先ほど述べたように、この例はほとんどの状況に対して過剰に設計されています。しかし、単一責任の原則に忠実であり続けるために何ができるかについてのアイデアが得られるはずです。
于 2013-07-01T13:25:56.013 に答える