简体   繁体   English

具有部分加载对象的域模型

[英]Domain Model with partially loaded objects

Let's say I have an application which consists of both client and server. 假设我有一个由客户端和服务器组成的应用程序。 Client is using MVVM pattern (with WPF) and server is simply a WCF service which fetches some data from database and returns data as DTO-objects to client. 客户端使用MVVM模式(使用WPF),服务器只是一个WCF服务,它从数据库中获取一些数据并将数据作为DTO对象返回给客户端。 In client, DataAccess layer converts these DTOs to domain objects and passes them to Model. 在客户端中,DataAccess层将这些DTO转换为域对象并将它们传递给Model。 ViewModel uses Model to fetch data (Domain Object) and populates itself with it. ViewModel使用Model来获取数据(Domain Object)并用它填充自己。

在此输入图像描述

To optimize database performance, each ViewModel is given only the data it really needs and nothing more (as recommended by many sources). 为了优化数据库性能,每个ViewModel仅提供它真正需要的数据,而不是更多(根据许多来源的建议)。 For example, let's say there is an entity called DbCustomer which has 30 properties, and there are also 3 different Views related to customers: CustomerProfileView , CustomersListView and CustomerThirdView . 例如,假设有一个名为DbCustomer的实体,它有30个属性,还有3个与客户相关的不同视图: CustomerProfileViewCustomersListViewCustomerThirdView Every view needs different portion of data: CustomerProfileView uses 20 properties, CustomersListView uses 10 properties and CustomerThirdView uses only 4 properties. 每个视图都需要不同的数据部分: CustomerProfileView使用20个属性, CustomersListView使用10个属性,而CustomerThirdView仅使用4个属性。 For each View, only required properties are fetched from database and delivered to ViewModel. 对于每个View,只从数据库中提取所需的属性并将其传递给ViewModel。

Now the problem arises: how should I design my Domain Objects to support this? 现在问题出现了: 我应该如何设计我的域对象来支持它?

Solution 1, one partially loaded Domain Object (no-go) 解决方案1,一个部分加载的域对象(禁止)

If I have only one Customer Domain Object which is used by all ViewModels, it would have different data depending on the ViewModel that requested it. 如果我只有一个由所有ViewModel使用的Customer域对象,则它将具有不同的数据,具体取决于请求它的ViewModel。 Obviously this is a no-go way because if I have to use this Customer object somewhere else I cannot be sure does it have enough properties loaded. 显然这是一种禁止的方式,因为如果我必须在其他地方使用这个Customer对象,我不能确定它是否有足够的属性加载。

For example, I might have method GetDataStoragePath which is supposed to return string describing path to customer's private files. 例如,我可能有方法GetDataStoragePath ,它应该返回描述客户私有文件路径的字符串。 The method requires properties FirstName , LastName , SSN and IsExternalCustomer . 该方法需要属性FirstNameLastNameSSNIsExternalCustomer Now, let's say CustomerThirdView doesn't need IsExternalCustomer , so it is not loaded when CustomerThirdViewModel requests Model to load Customer . 现在,假设CustomerThirdView不需要IsExternalCustomer ,因此当CustomerThirdViewModel请求Model加载Customer时,它不会被加载。 Now if I use this Customer somewhere else (it is not a ViewModel specific object), the method GetDataStoragePath will fail. 现在,如果我在其他地方使用此Customer (它不是ViewModel特定对象),则GetDataStoragePath方法将失败。

Solution 2, three different Domain Objects 解决方案2,三个不同的域对象

In another solution there would be 3 different Domain Objects (used as data containers) with suitable interfaces, and then GetDataStoragePath would depend only from this interface. 在另一个解决方案中,将有3个不同的域对象(用作数据容器)和合适的接口,然后GetDataStoragePath将仅依赖于此接口。 Example: 例:

public interface ICanGetDataStoragePath {
    string FirstName { get; }
    string LastName { get; }
    string SSN { get; }
    bool IsExternalCustomer { get; }
}

public CustomerProfileData : ICanGetDataStoragePath { ... } // Implements interface
public CustomerListViewData : ICanGetDataStoragPath { ... } // Implements interface
public CustomerThirdViewData { ... } // Does NOT implement interface

public class CustomerLogic : ICustomerLogic {
    public string GetDataStoragePath(ICanGetDataStoragePath customer) {...}
}

This would lead to Anemic Domain Model but it is not a problem in my opinion. 这将导致贫血领域模型,但在我看来这不是问题。 However, it seems messy since I can easily imagine that there would be 20 different methods with different needs which would result in 20 interfaces (and only for Customer, there are LOTS of other domain objects also). 然而,它似乎很乱,因为我可以很容易地想象有20种不同的方法有不同的需求会导致20个接口(并且只有Customer,还有很多其他域对象)。 Of course in this simple case I could pass all four parameters separately to GetDataStoragePath but in real life there are many more required properties. 当然,在这个简单的例子中,我可以将所有四个参数分别传递给GetDataStoragePath,但在现实生活中,还有更多必需的属性。

Are there any other options? 还有其他选择吗? What would be the best way to solve the problem? 什么是解决问题的最佳方法?

Your model obviously has to much Data. 你的模型显然需要很多数据。 Why not make 3 models and one composite model? 为什么不制作3个模型和一个复合模型?

ie

public class CustomerProfile 
{
    public string Phone { get; set; }
    // other profile fields
}

public class Customer 
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string SSN { get; set; }
    public bool IsExternalCustomer { get; set; }
    public CustomerProfile Profile { get; set; }
}

Then you'd put all of your always required fields into the Customer class and group the rest together, ie in a CustomerProfile class. 然后,您将所有始终必需的字段放入Customer类,并将其余字段组合在一起,即在CustomerProfile类中。 If it's null , then that data wasn't fetched and isn't available 如果它为null ,则表示未提取该数据且该数据不可用

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

相关问题 单元测试域模型对象 - Unit testing Domain model objects 从JSON排除域模型对象的属性 - Excluding properties on domain model objects from JSON 在所有解决方案项目中共享域模型对象 - Sharing Domain model objects in all solution projects 在Facebook域模型中识别实体和价值对象 - Identifying Entities and Value Objects in Facebook domain model NHibernate完全填充部分加载的集合 - NHibernate fully populate a partially loaded collection BestPractice:使用AutoMapper或LINQ(LINQ to Objects)在域模型和表示模型之间进行映射的优点和缺点 - BestPractice: Pros and Cons for using AutoMapper or LINQ (LINQ to Objects) for mapping between a Domain Model and a Presentation Model 使用反射时部分类型化的泛型对象 - Partially typed generic objects when using reflection 检查加载的程序集是否为域中性 - Check if loaded assembly is domain neutral 贫血领域模型和领域服务 - anemic domain model and domain services C#/ DDD:在使用洋葱架构时,如何使用域层不可实例化的内部状态对象的实体建模? - C# / DDD: How to model entities with internal state objects not instantiable by the domain layer when using onion architecture?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM