鼎鼎知识库
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

7、Hi,建造者模式.md 970B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. ```
  2. public class ToDoItemBuilder
  3. {
  4. private ToDoItem _todo = new ToDoItem();
  5. public ToDoItemBuilder Id(int id)
  6. {
  7. _todo.Id = id;
  8. return this;
  9. }
  10. public ToDoItemBuilder Title(string title)
  11. {
  12. _todo.Title = title;
  13. return this;
  14. }
  15. public ToDoItemBuilder Description(string description)
  16. {
  17. _todo.Description = description;
  18. return this;
  19. }
  20. public ToDoItemBuilder WithDefaultValues()
  21. {
  22. _todo = new ToDoItem { Id = 1, Title = "title", Description = "desc" };
  23. return this;
  24. }
  25. public ToDoItem Build() => _todo;
  26. }
  27. ```
  28. ```
  29. public class ToDoItem : BaseEntity, IAggregateRoot
  30. {
  31. public string Title { get; set; } = string.Empty;
  32. public string Description { get; set; }
  33. public bool IsDone { get; private set; }
  34. ......
  35. }
  36. ```