[英]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个与客户相关的不同视图: CustomerProfileView
, CustomersListView
和CustomerThirdView
。 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? 现在问题出现了: 我应该如何设计我的域对象来支持它?
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
. 该方法需要属性
FirstName
, LastName
, SSN
和IsExternalCustomer
。 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
方法将失败。
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.