领域驱动设计:在不使用服务定位器的情况下从实体访问配置值

发布于 2025-01-01 05:00:17 字数 1407 浏览 1 评论 0原文

我有一个 User 实体,它有一个 HasCompletedSecurity 属性,该属性指示特定的 User 是否已回答系统所需的安全问题数量。系统所需的安全问题数量是可配置的,并可从配置文件中检索。 User 类应该如何访问配置的信息?

我目前有一个 IConfigurationService 接口,在该接口后面我有使用 ConfigurationManager 或 Azure 等效项(如果可用)的实现。我已经通过静态 InjectionService 类封装了对 DI 容器的访问,并且当前正在解析配置的值,如下所示:

public class User
{
    private static readonly IConfigurationService _configurationService = 
        InjectionService.Resolve<IConfigurationService>();

    public bool HasCompletedSecurity
    {
        get
        {
            // Uses the static _configurationService to get the 
            // configured value:
            int numberOfRequiredResponses = 
                GetConfiguredNumberOfRequiredResponses();

            return this.SecurityQuestionResponses.Count()
                >=
                GetConfiguredNumberOfRequiredResponses();
        }
    }
}

这当然是 ServiceLocator 反模式,我一点也不喜欢它。静态依赖使得对使用此类的任何内容进行单元测试变得很尴尬。

我正在使用实体框架并从此处获取提示我不想通过 DI 容器传递我的实体来为它们提供依赖项,所以...我应该如何访问配置的值?

编辑:有了这个确切的示例(我确实很欣赏有关其正确架构的建议),我感兴趣的更大问题是如何管理对来自实体的服务?答案是否只是以您永远不需要的方式构建实体?

I have a User entity which has a HasCompletedSecurity property which indicates whether that particular User has answered the number of security questions required by the system. The number of security questions the system requires is configurable and retrieved from a config file. How should the User class access the configured information?

I currently have an IConfigurationService interface behind which I have implementations which use the ConfigurationManager or the Azure equivalent if it is available. I've encapsulated access to my DI container through a static InjectionService class, and am currently resolving the configured value like so:

public class User
{
    private static readonly IConfigurationService _configurationService = 
        InjectionService.Resolve<IConfigurationService>();

    public bool HasCompletedSecurity
    {
        get
        {
            // Uses the static _configurationService to get the 
            // configured value:
            int numberOfRequiredResponses = 
                GetConfiguredNumberOfRequiredResponses();

            return this.SecurityQuestionResponses.Count()
                >=
                GetConfiguredNumberOfRequiredResponses();
        }
    }
}

This is of course an example of the ServiceLocator anti-pattern, and I don't like it one bit. The static dependency makes unit testing anything which uses this class awkward.

I'm using the Entity Framework and taking a cue from here I don't want to pass my entities through a DI container to give them their dependencies, so... how should I be accessing the configured value instead?

Edit: With this exact example to one side (and I do appreciate the suggestions as to the correct architecture for it), the larger question I'm interested in is how do you manage non-static references to services from entities? Is the answer to just architect the entities in such a way that you never need to?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

镜花水月 2025-01-08 05:00:17

以下是我定义 User 类的方式:

public class User
{
    public bool HasCompletedSecurity { get; set; }

    // other members...
}

说实话,这是一个更好的解决方案,因为它沿着时间维度解耦了值。考虑一下:如果用户在 2010 年完成了所有安全问题,而您后来更改了业务规则,那么您是否要使现有用户失效?

在大多数情况下,记录并坚持认为用户在过去某个时间完成了当时有效的安全程序可能更为合理。这样,您就不会打扰现有用户。

Here's how I would define the User class:

public class User
{
    public bool HasCompletedSecurity { get; set; }

    // other members...
}

Seriously, this is a better solution because it decouples the value along the temporal dimension. Consider this: if a user completed all security questions in 2010 and you later on change the business rule, are you then going to invalidate the existing user?

In most cases it would probably be more reasonable to record and persist that sometime in the past, the user completed the security procedure that was in effect at that time. In that way, you don't bother existing users.

江挽川 2025-01-08 05:00:17

您仍然可以使用控制反转的概念,而无需使用任何排序 IoC 容器或要求在实体的构造函数中使用它。我会使用准策略模式来解决这个问题,并有类似的内容:

public interface ISecurityPolicy
{
    public int MinimumSecurityQuestionResponses { get; }
}

public class User
{
    public void HasCompletedSecurity (ISecurityPolicy security_policy)
    {
         return this.SecurityQuestionResponses.Count()
                     >= security_policy.MinimumSecurityQuestionResponses;
    }
}

这将提供用户必须满足的特定安全策略的责任放在调用者上,而不是 User 类本身。

从那时起,您可以根据需要提供该额外参数,也许将其包装在将 ISecurityPolicy 注入到服务中的 IUserSecurityService 中,等等

。仍然是控制反转,但它处于方法级别,因为这一特定方法实际上是唯一关心安全策略/配置的方法。

You can still using the concept of Inversion of Control without using any sort IoC container or requiring its use in the constructor of your entity. I would approach this using a quasi-strategy pattern and have something like:

public interface ISecurityPolicy
{
    public int MinimumSecurityQuestionResponses { get; }
}

public class User
{
    public void HasCompletedSecurity (ISecurityPolicy security_policy)
    {
         return this.SecurityQuestionResponses.Count()
                     >= security_policy.MinimumSecurityQuestionResponses;
    }
}

This puts the onus of providing the particular security policy that the user must satisfy on the caller, not the User class itself.

From that point on, you can provide that extra parameter however you want to, maybe be wrapping this in a IUserSecurityService that will have the ISecurityPolicy injected into the service, etc.

This is still Inversion of Control, but it's at the method level, since this one particular method is really the only one that cares about the security policy/configuration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文