简体   繁体   English

存储库模式 vs DTO 模式方法

[英]Repository pattern vs DTO pattern approach

We can have these two approaches to send data to Data Access Layer or any other source:我们可以使用这两种方法将数据发送到数据访问层或任何其他来源:

Approach 1 : Repository way:方法1 :存储库方式:

public class User
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
}

public class UserRepository
{
    public static void Add(User user)
    {
        // Add user logic
    }
    public static void Delete(User user)
    {
        // Delete user logic
    }
    public static User Get(int userid)
    {
        // Get user logic
    }
}

Usage:用法:

var user = new User
    {
        FirstName = "FirstName",
        LastName = "LastName",
        Age = 20
    };
UserRepository.Add(user);

Above, you have seen that I have kept the User class simple.在上面,您已经看到我使User类保持简单。 It is not having any behavior.The behavior is added in a separate class UserRepository .它没有任何行为。行为被添加到一个单独的类UserRepository 中

Second approach : Keeping Add/Delete/Get etc in User.cs only:第二种方法:仅在 User.cs 中保留 Add/Delete/Get 等:

   public class User
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }

        public void Add()
        {
            // Add user logic
        }
        public void Delete()
        {
            // Delete user logic
        }
        public User Get()
        {
            // Get user logic
        }
    }

Usage:用法:

var user = new User
    {
        FirstName = "FirstName",
        LastName = "LastName",
        Age = 20
    };
user.Add();

Above I have kept the behavior in User.cs only.以上我只保留了 User.cs 中的行为。 Both of the two approaches is serving the purpose of adding, deleting etc. the user.这两种方式都是为了增加、删除用户等。 Can you let me know你能告诉我吗

  1. Which approach is better?哪种方法更好?

  2. When to decide which of the above two approach we have to opt?什么时候决定我们必须选择上述两种方法中的哪一种?

  3. If I have to add other methods too like FindAllUsers , FindUserByUserId , DeleteUserByUserId which approach should I go for?如果我也必须添加其他方法,例如FindAllUsersFindUserByUserIdDeleteUserByUserId ,我应该采用哪种方法?

The first approach is far better as you are separating concerns ie the domain entity User and persistence to the database.第一种方法要好得多,因为您正在分离关注点,即域实体 User 和数据库的持久性。

One of the most important things that is often talked about in Domain Driven Design is "persistence ignorance" See What are the benefits of Persistence Ignorance?领域驱动设计中经常谈论的最重要的事情之一是“持久无知”,请参阅持久无知的好处是什么?

By using the repository pattern, the way you save /get your entity is kept out of the entity code, ie your domain keeping it cleaner and in essence achieving persistence ignorance (or going a long way towards it anyway)通过使用存储库模式,您保存/获取实体的方式将被排除在实体代码之外,即您的域使其保持清洁,并在本质上实现了持久性无知(或者无论如何都朝着它走很长的路)

So answers:所以回答:

  1. The repository approch is much better存储库方法要好得多
  2. Always go for option 1总是选择选项 1
  3. Add these methods to the repository class将这些方法添加到存储库类

It strictly depends on the work you need to get done and on the size of your app.这完全取决于您需要完成的工作以及您的应用程序的大小。 If you want something developed fast and less scalable you don't need to use a n-tier type architecture(i mean separate your data interactions to your Data acces layer).如果您想要快速开发且可扩展性较低的东西,则不需要使用 n 层类型架构(我的意思是将您的数据交互与数据访问层分开)。

However if you are looking for something that need to be highly scalable, editable, modifiable and know that it will get future features then clearly you must separate your consceurns to make your work easier for a longer period of time.但是,如果您正在寻找需要高度可扩展、可编辑、可修改的东西,并且知道它将获得未来的功能,那么很明显,您必须将您的责任分开,以使您的工作在更长的时间内更轻松。

In the end as i said each approach serves a purpose, just know what your purpose is before getting to work.最后,正如我所说的,每种方法都有一个目的,在开始工作之前要知道你的目的是什么。

Cheers.干杯。

If you follow DDD guidelines , then, entities should NOT have dependency on infrastructure:如果您遵循DDD 指南,那么实体不应依赖于基础设施:

Nikola's 1st law of IoC尼古拉的国际奥委会第一定律

Store in IoC container only services.仅存储在 IoC 容器中的服务。 Do not store any entities.不要存储任何实体。

Reason: By including infrastructure logic into your entity, you are moving away from Single Responsibility Principle .原因:通过将基础架构逻辑包含到您的实体中,您正在远离单一职责原则 See Mark Seemann's explanation .参见 Mark Seemann 的解释

The problem with your second approach is that User Entity has dependency on Infrastructure.你的第二种方法的问题是User实体依赖于基础设施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM