简体   繁体   English

我的TODO APP解法和OOP一致吗?

[英]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 ).您的类TodoItemTodoList相当简单并且只有一个职责( 单一职责原则)。 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.

相关问题 Todo.App 在 Todo.UWP.MainPage 中使用,但谁调用了 Todo.UWP.App? - Todo.App is used in Todo.UWP.MainPage but who invokes Todo.UWP.App? 我的OOP设计正确吗? - Is my OOP design correct? 多个解决方案之间共享的app.xaml - Shared app.xaml between multiple solutions 为什么我的代码中的 SQL 命令在运行时被“TODO: FUNC”替换? - Why is a SQL command in my code replaced by "TODO: FUNC" at runtime? 在我的情况下,将接口方法标记为async是正确的事情 - Is marking an interface method as async the right thing todo in my case XAML (WPF) 设计器无法以一致或有意义的方式解析应用程序资源 - XAML (WPF) designer does not resolve app resources in a consistent or meaningful way 是否可以从不同解决方案的单独项目访问App_Data - Is it possible access App_Data from a seperate projects in different solutions C# Solutions Explorer 中的多个自定义 app.config 文件 - C# multiple custom app.config files in Solutions Explorer 比我的.NET组装解决方案更好的数据库访问层解决方案? - Better solutions for a Database Access Layer than my .NET assembly solution? 如何将.htaccess文件添加到我的Visual Studio解决方案 - How to add .htaccess file to my Visual Studio solutions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM