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.
Solution 1 : geting corresponding todoList Objet via TodoListRepository and Add TodoItem via the getting 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();
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
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? Thanks in advance
CodePudding user response:
Your classes TodoItem
and TodoList
are fairly simple and have one responsibility (Single responsibility Principle).
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
.
I like that repository has great separation of concerns. I mean that repository
is here like simple collection of items. We can make analogy with List<T>
, that
List<T>
does not have Save()
method. Responsibility of saving item is delegated to _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();
}