简体   繁体   English

在这种情况下,我应该使用哪种模式?

[英]What pattern(s) am I supposed to use for this situation?

For several applications I made for my current client I have shared user accounts. 对于我为当前客户所做的几个应用程序,我拥有共享的用户帐户。 This means that each account in one application should exist in the other applications. 这意味着一个应用程序中的每个帐户应存在于其他应用程序中。
Each application has it's own set of settings. 每个应用程序都有自己的一组设置。
The number of applications and the settings themselves will be the parts that really change over time so I want to separate them. 应用程序的数量和设置本身将是随时间变化的部分,因此我想将它们分开。

The data store is accessed through an IRepository class (XMLRepository, SQLRepository etc). 通过IRepository类(XMLRepository,SQLRepository等)访问数据存储。 They abstract the actual data access logic away. 他们将实际的数据访问逻辑抽象掉了。 The SettingsService class should be able to get an ISetting class as followed SettingsService类应该能够按照以下方式获取ISetting类

public T GetSetting<T>(IUser user) where T : ISetting

Since the fields of an ISettings class will be different for each type I would reckon that it's the actual Settings class that should know how to fill it's own fields, but it doesn't know how to get the values. 由于每种类型的ISettings类的字段都将有所不同,因此我认为实际的Settings类应该知道如何填充其自身的字段,但不知道如何获取值。
The repository however would know how to access the data, but it doesn't know where to put them. 但是,存储库将知道如何访问数据,但不知道将数据放在何处。

The GetSetting is actually a factory method if I'm not mistaking. 如果我没有记错的话,GetSetting实际上是一种工厂方法。 I have the feeling this problem is not something new and there is probably a good pattern to solve this. 我觉得这个问题并不是什么新鲜事物,可能有一个很好的模式可以解决这个问题。

What are my options? 我有什么选择?

You will need some sort of factory for each concrete type of ISetting that can create the concrete SomeSetting instance from data returned from a Repository. 对于每种ISetting的具体类型,您都需要某种工厂,该工厂可以根据从存储库返回的数据创建具体的SomeSetting实例。

How such a factory should work depends on how you envision the settings persistence schema. 这样的工厂应该如何工作取决于您如何设想设置持久性架构。 Do you have a custom schema for each type of ISetting, or do you simply serialize and deserialize settings in a BLOB/XML? 您是否对每种ISetting类型都有自定义架构,还是只是简单地对BLOB / XML中的设置进行序列化和反序列化?

In the first case, you will need a custom Repository for each settings schema. 在第一种情况下,您将需要为每个设置架构自定义存储库。 This is the easy scenario, since each specialized Repository will simply act as the custom factory. 这是简单的方案,因为每个专用存储库都将简单地充当定制工厂。

In the other case, you can save metadata together with the BLOB that either stores which custom factory to use to deserialize the BLOB, or alternatively simply the type of the serialized BLOB (and you can then use the serialization API of .NET to serialize and deserialize the object). 在另一种情况下,您可以将元数据与BLOB一起保存,后者存储用于反序列化BLOB的自定义工厂,或者存储序列化BLOB的类型(然后您可以使用.NET的序列化API进行序列化和反序列化对象)。

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

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