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