简体   繁体   English

具有业务逻辑的存储库模式

[英]Repository pattern with business logic

I just have the intention to write demo application based on DDD. 我只是想编写基于DDD的演示应用程序。 My repository uses Entity Framework ORM an everything is great. 我的存储库使用Entity Framework ORM,一切都很棒。 MVC and Windows Forms applications calls repository methods and that works. MVC和Windows Forms应用程序调用存储库方法,并且该方法有效。 But What If I decide to replace Entity Framework with Dapper or NHibernate or my data comes through web service? 但是,如果我决定用Dapper或NHibernate替换Entity Framework或我的数据通过Web服务来处理呢? I konw that I need to rewrite repostiory implementation, but what with my bussines logic. 我知道我需要重写后实现,但是我的业务逻辑又如何。 Business logic now is placed in Repository. 现在,业务逻辑已放置在存储库中。 Some examples have business logic in controller but I have more than one client. 一些示例在控制器中具有业务逻辑,但是我有多个客户端。 Do I need to put some layer above Repository? 我是否需要在存储库上放置一些图层? What is the name of that layer in concept of DDD. DDD概念中该层的名称是什么。

I konw that I need to rewrite repostiory implementation, but what with my bussines logic. 我知道我需要重写后实现,但是我的业务逻辑又如何。 Business logic now is placed in Repository. 现在,业务逻辑已放置在存储库中。

This kind of worries me. 这种让我担心的。 If your business logic is placed inside of your repository, then you aren't practicing Domain Driven Design at all. 如果您的业务逻辑放在存储库中,那么您根本就不会练习“域驱动设计”。 In DDD, your business logic is found in your domain entities. 在DDD中,可以在域实体中找到您的业务逻辑。 The purpose of a repository is simply to return populated domain objects. 存储库的目的仅仅是返回已填充的域对象。 The Domain Objects shouldn't have any idea how they're saved (whether it's in a database, an xml file, or just created dynamically). 域对象不应该知道如何保存它们(无论是在数据库,xml文件中还是只是动态创建)。

The whole purpose of Domain Driven design is to free you from being tied to any specific infrastructure. 域驱动设计的全部目的是使您摆脱与任何特定基础结构的联系。 Your Domain Objects should be able to work, regardless of whether you're using Entity Framework or Dapper. 无论您使用的是Entity Framework还是Dapper,您的域对象都应该能够工作。

Typically, you can create a set of classes that represents your domain model. 通常,您可以创建一组代表您的域模型的类。 Then you will have a set of classes for the repository that will take the domain model class for read/write. 然后,您将为存储库提供一组类,这些类将使用域模型类进行读/写。 This allows you to change one independent of the other. 这使您可以独立于另一个进行更改。

For example, domain model: 例如,领域模型:

class Vehicle
{
    string id; // or you can use an appropriate key
    int noOfWheels;
    int topSpeed;
    // etc.
}

class Car : Vehicle
{
    int passengerCapacity;
    int trunkCapacity;
    // etc.
    string Serialize();
    void Deserialize(string representation);
}

And your repository: 和您的存储库:

class VehicleRepository
{
    void Store(Vehicle v);
    Vehicle GetVehicle(string id);
    void Delete(string id);
    // etc.
}

I have avoided adding interfaces for code brevity. 我避免添加接口以简化代码。 But, in general, this kind of a structure will allow you to modify your domain objects regardless of the repository and vice versa. 但是,总的来说,这种结构将允许您修改域对象,而不管存储库如何,反之亦然。 Thus you can have a repository that will put your objects in NOSQL store (or file system for local testing) by serializing/deserializing objects, but another repository that will CRUD objects from a SQL store. 因此,您可以拥有一个存储库,该存储库通过对对象进行序列化/反序列化将对象放入NOSQL存储库(或用于本地测试的文件系统)中,而另一个存储库将从SQL存储库中删除对象。

I have included serialization/deserialization methods on the object, which works in most cases. 我在对象上包括了序列化/反序列化方法,该方法在大多数情况下都有效。 However, sometimes this has to be separated out in separate classes and hidden behind the repository for version control with the physical storage medium (ie your physical representation may change, but your domain objects should not be affected, and vice versa). 但是,有时必须将其分离到单独的类中,并隐藏在资源库的后面,以便使用物理存储介质进行版本控制(即,您的物理表示形式可能会发生变化,但是域对象不会受到影响,反之亦然)。

Do I need to put some layer above Repository? 我是否需要在存储库上放置一些图层?

Yes. 是。 The goal of a Repository is to simply remove knowledge of how data is persisted from the Domain layer. 存储库的目标是简单地从域层中删除有关数据持久化的知识。 This is to enforce a separation of concerns and keep the Domain 'pure'. 这是为了使关注点分离并保持“纯”域。 A Repository does this by providing an abstraction for your Domain objects whereby it presents them as nothing more than an in-memory collection. 存储库通过为您的Domain对象提供抽象来做到这一点,从而将它们呈现为内存中的集合。

Note however that the Repository's interface should be defined in the Domain, because the Repository is there to serve the Domain and only the Domain can define its contract with its data. 但是请注意,应该在域中定义存储库的接口,因为存储库在那里可以为域提供服务,并且只有域可以使用其数据定义其合同。

What is the name of that layer in concept of DDD? DDD概念中的该层的名称是什么?

Repositories belong in the Infrastructure layer. 存储库属于基础结构层。 The layer above this is the Domain layer, where all business logic lives. 在此之上的层是域层,所有业务逻辑都存在于此。

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

相关问题 存储库模式和/或/ vs业务逻辑层 - Repository pattern and/or/vs business logic layer 通用存储库模式以及数据层与业务逻辑的分离 - Generic repository pattern & separation of data layer from business logic 将存储库模式中的服务层用于“标准”业务逻辑/实用程序方法? - Using service layer in Repository Pattern for “Standard” business logic / Utility methods? 使用MVC + Repository Pattern,Business Logic应该在哪里? - Using MVC + Repository Pattern, where Business Logic should be? 我需要分开业务对象和业务逻辑吗? C#中具有存储库模式的Asp.net MVC - Do I need to Separate Business Objects and Business Logic? Asp.net MVC with Repository Pattern in C# 存储库中的嵌套类和业务逻辑? - Nested classes and business logic in a repository? 混合逻辑的存储库模式 - Repository Pattern with mixed Logic 存储库模式,POCO和业务实体 - Repository Pattern, POCO, and Business Entities 如何将通用存储库(或业务逻辑层)注入 ViewModel - How to Inject Generic Repository(Or Business Logic Layer) Into ViewModel 向ASP> NET MVC 3 C#中的存储库添加简单的业务逻辑 - Add simple business logic to repository in ASP>NET MVC 3 C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM