[英]My TODO APP solutions is consistent with OOP?
I would like to have your opinions by contribution to these two implementations of a TODO List application as to the respect of the principles of object-oriented programming.我想通过对 TODO 列表应用程序的这两个实现的贡献来了解您对面向对象编程原则的看法。
Solution 1: geting corresponding todoList Objet via TodoListRepository and Add TodoItem via the getting Object方案一:通过TodoListRepository获取对应的todoList对象,通过获取Object添加TodoItem
class TodoItem{
private readonly string _libele;
private readonly int _todoListId;
public string Libele => _libele;
public TodoItem(int todoListId, string libele)
{
_libele = libele;
_todoListId = todoListId;
}
}
class TodoList{
private readonly ICollection<TodoItem> _items;
private readonly int _id;
public IReadOnlyList<TodoItem> Items => _items.ToList();
public void AddItem(string libele)
{
_items.Add(new TodoItem(_id, libele));
}
}
class TodoListService {
private readonly ITodoListRepository _repository;
private readonly IAppUnitOfWork _unitOfWork;
public TodoListService(ITodoListRepository repository, IAppUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
_repository = repository;
}
void AddItemToTodoList(int todoListId, string lebele)
{
var todList = _repository.GetTodoListById(todoListId);
todList.AddItem(lebele);
_unitOfWork.Commit();
}
}
Solution 2 Add todoItem via TodoItemRepository witout passing by TodoList Object解决方案 2 通过 TodoItemRepository 添加 todoItem witout 通过 TodoList Object
class TodoItem {
private readonly string _libele;
private readonly int _todoListId;
public string Libele => _libele;
public TodoItem(int todoListId, string libele)
{
_libele = libele;
_todoListId = todoListId;
}
}
class TodoList{
private readonly ICollection<TodoItem> _items;
private readonly int _id;
public IReadOnlyList<TodoItem> Items => _items.ToList();
}
class TodoItemService {
private readonly ITodoItemRepository _repository;
private readonly IAppUnitOfWork _unitOfWork;
public TodoItemService(ITodoItemRepository repository, IAppUnitOfWork unitOfWork){
_unitOfWork = unitOfWork;
_repository = repository;
}
void AddItemToTodoList(int todoListId, string lebele){
var todoItem = new TodoItem(todoListId, lebele);
_repository.Add(todoItem);
_unitOfWork.Commit();
}
}
Is solution 1 consistent with OOP. What are the opinions between the two solutions?方案1与OOP是否一致,两种方案有何意见? Thanks in advance
提前致谢
Your classes TodoItem
and TodoList
are fairly simple and have one responsibility ( Single responsibility Principle ).您的类
TodoItem
和TodoList
相当简单并且只有一个职责( 单一职责原则)。 And these classes do not have any code that can pollute their single responsibility.而且这些类没有任何代码可以污染它们的单一职责。 I mean there is no logic for logging or other responsibilities in these classes.
我的意思是这些类中没有日志记录或其他职责的逻辑。 So it is okay.
所以没关系。 Read another great post about Single Responsibility Principle.
阅读另一篇关于单一职责原则的精彩文章。
But it looks like your code in solution 1 will not save any items as your item will be added to IReadOnlyList<TodoItem> Items
.但看起来您在解决方案 1 中的代码不会保存任何项目,因为您的项目将被添加到
IReadOnlyList<TodoItem> Items
。
I like that repository has great separation of concerns.我喜欢该存储库具有很好的关注点分离。 I mean that
repository
is here like simple collection of items.我的意思是
repository
在这里就像简单的项目集合。 We can make analogy with List<T>
, that List<T>
does not have Save()
method.我们可以类比
List<T>
,即List<T>
没有Save()
方法。 Responsibility of saving item is delegated to _unitOfWork
.保存项目的责任委托给
_unitOfWork
。 And this is also great separation of concerns.这也是很好的关注点分离。
void AddItemToTodoList(int todoListId, string lebele)
{
var todoItem = new TodoItem(todoListId, lebele);
_repository.Add(todoItem);
_unitOfWork.Commit();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.