如何使用 StructureMap 将默认构造函数参数设置为 null?

发布于 2024-09-26 01:55:59 字数 445 浏览 0 评论 0原文

类有一个以 IMyInterface 作为参数的唯一构造函数。如果我定义 IMyInterface 的具体类型并将其注册到 StructureMap ,那么就没有问题,我的类可以使用该具体类型进行实例化。

但是,在某些情况下,不会注册任何具体类型。在这种情况下,我希望 IMyInterface 参数接收 null。相反,我得到一个异常:

StructureMap Exception Code: 202 没有为 PluginFamily IMyInterface 定义默认实例。

是否可以为缺少的插件定义默认值?

上下文:我的类是一个服务,使用 Spark 视图引擎并定义了一些默认名称空间。该服务使用 ISparkNamespacesProvider (IMyInterface) 来添加其他命名空间。客户端应用程序可以注册或不注册这样的提供者。这就是为什么服务的构造函数要么接收提供者,要么不接收提供者。

A class has a unique constructor taking IMyInterface as its argument. If I define a concrete type of IMyInterface and registers it to StructureMap then there is no issue and my class can be instanciated with this concrete type.

However, in some cases, no concrete type will be registered. In that case, I would like to receive null for the IMyInterface parameter. Instead I get an exception:

StructureMap Exception Code: 202
No Default Instance defined for PluginFamily IMyInterface.

Is it possible to define a default value for a missing plugin?

Context: my class, which is a service, uses the Spark view engine and defines some default namespaces. The service uses a ISparkNamespacesProvider (IMyInterface) to add aditional namespaces. The client app may register such a provider or not. That's why the constructor of the service will receive either a provider or none.

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

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

发布评论

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

评论(2

凡尘雨 2024-10-03 01:55:59

摘自此处

For<IService>().Use<MyService>()
 .Ctor<IMyInterface>("nameOfParameter").Is(null);

但是你应该考虑为什么你的班级依赖于IMyInterface。如果它是可选的——那就是代码味道。也许您应该将其重构为需要它的方法的方法参数或可设置的属性。

不需要在具体实现和 null 之间切换。在组合根构建依赖关系图时,您应该确切地知道没有 .If(isSomething()).Use().Ctor(null) 的依赖关系是什么。

您可能想查看 tekpub 演示文稿和这本书(寻找所谓的 MEAP 访问)关于 DI 和 IOC。


实现您想要的一种方法是使用所谓的 '穷人依赖注入'。也就是说 - 定义第二个构造函数:

public MyClass():this(null){...}

但我不建议这样做。

Taken from here:

For<IService>().Use<MyService>()
 .Ctor<IMyInterface>("nameOfParameter").Is(null);

But You should think about why Your class is dependent on IMyInterface. If it's optional - that's a code smell. Maybe You should refactor it out as method argument for method that needs it or as settable property.

There shouldn't be need for switching between concrete implementation and null. When composing dependency graph at composition root, You should know exactly what will be Your dependencies w/o .If(isSomething()).Use<MyService>().Ctor<IMyInterface>(null).

You might want to check out this tekpub presentation and this book (look for so called MEAP access) about DI and IOC.


One way to accomplish what You want is using so called 'poor man dependency injection'. That is - to define second constructor:

public MyClass():this(null){...}

But I wouldn't recommend that.

赠意 2024-10-03 01:55:59

StructureMap 现在通过 UseIfNone 支持这种情况https://structuralmap.github.io/registration/fallback-服务/

StructureMap now supports this case via UseIfNone https://structuremap.github.io/registration/fallback-services/

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