配置StructureMap以生成并“记住”消息。构造函数参数文字 [英] Configuring StructureMap to generate and "remember" a constructor param literal

查看:65
本文介绍了配置StructureMap以生成并“记住”消息。构造函数参数文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于以下结构的StructureMap配置:

I have a StructureMap config that looks something like:

cfg.For<ICacheOrder>().Use<CacheOrder>().Ctor<int>().Is(context => LoginHelper.LoginID);
cfg.For<ICacheProduct>().Use<CacheProduct>().Ctor<int>().Is(context => LoginHelper.LoginID);
cfg.For<ISQLOrder>().Use<SQLOrder>().Ctor<int>().Is(context => LoginHelper.LoginID);
cfg.For<ISQLProduct>().Use<SQLProduct>().Ctor<int>().Is(context => LoginHelper.LoginID);

通过构造函数注入,可以创建对象链,其中一些需要确定的int LoginID在创建时。静态LoginHelper确定LoginID。

Via constructor injection, a chain of objects can be created, with some needing an int LoginID that is determined at the time of creation. The static LoginHelper determines the LoginID.

当前在我的配置中,为每个创建的对象调用LoginHelper。有没有办法(也许通过StructureMap的IContext)记住 LoginID,并且只在创建链中确定一次?

Presently in my config, LoginHelper is called for every created object. Is there a way, perhaps via StructureMap's IContext, for LoginID to be "remembered" and only determined once within a chain of creation?

我知道我可以重构和创建一个ILogin接口/混凝土,StructureMap可以构造和缓存它-但我希望我的各个层只关心一个简单的int LoginID。

I know that I could refactor and create an ILogin interface/concrete that StructureMap could construct and cache - but I'd prefer my various layers to be concerned only with a simple int LoginID.

推荐答案

虽然可以在服务中注入原始配置值,但是当您将同一基元重复注入到多个服务中时,就会缺少抽象。

Although it's okay to inject primitive configuration values in your services, when you repetitively inject that same primitive into multiple services, you are missing an abstraction.

这您的配置显然是这种情况;您缺少抽象。

This is clearly the case with your configuration; you are missing an abstraction.

解决方案是让这些服务依赖抽象而不是原始值。例如:

The solution is to let those services depend on an abstraction rather than a primitive value. For instance:

public interface ICurrentUser
{
    int LoginID { get; }
}

您可以创建一个非常简单的实现,如下所示:

And you can create a rather simple implementation as follows:

public class CurrentUserImpl : ICurrentUser
{
    public CurrentUserImpl()
    {
        this.LoginID = LoginHelper.LoginID;
    }

    public int LoginID { get; private set; }
}

这意味着您将不得不更改<$ c $的构造函数c> CacheOrder , CacheProduct SQLOrder SQLProduct ,但是执行此操作后,您的配置将更易于维护:

This means that you will have to change the constructors of CacheOrder, CacheProduct, SQLOrder and SQLProduct, but when you do this, your configuration gets much more maintainable:

cfg.For<ICacheOrder>().Use<CacheOrder>();
cfg.For<ICacheProduct>().Use<CacheProduct>();
cfg.For<ISQLOrder>().Use<SQLOrder>();
cfg.For<ISQLProduct>().Use<SQLProduct>();

记住参数文字的问题现在马上消失了,因为我们现在可以注册 ICurrentUser 如下:

The problem of "remembering a param literal" now goes away immediately, because we can now register the ICurrentUser as follows:

cfg.For<ICurrentUser>().Use<CurrentUserImpl>();

Structure Map中的默认生命周期是每个请求(每个对象图),因此将同一个实例注入

The default lifecycle in Structure Map is per request (per object graph) so the same instance is injected into all objects in a single object graph.

另一种选择是使用 HttpContext 生命周期进行注册,但这是该课程仅在运行ASP.NET Web应用程序时有效。

Another option is to register it using the HttpContext lifecycle, but this of course only works when running an ASP.NET web application.

这篇关于配置StructureMap以生成并“记住”消息。构造函数参数文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆