structuremap

How to inject different instance(s) for different context in ASP.NET MVC using StructureMap?

一笑奈何 提交于 2020-01-10 05:49:06
问题 We are using classes inheriting from Registry to configure our StructureMap container in our ASP.NET MVC 4 application startup. Some excerpt from one of the registry-classes: For<ISomeInterface>().HybridHttpOrThreadLocalScoped().Use<SomeImplementation>(); We would like use different instances of our interfaces depending on the context. (For example switching from database "online" mode to "maintenance" mode where everything is saved on filesystem; therefore using other interfaces (i.e.

为什么我需要一个IoC容器而不是直接的DI代码? [关闭]

半腔热情 提交于 2020-01-06 15:40:05
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 我一直在使用 依赖注入 (DI)一段时间,在构造函数,属性或方法中注入。 我从未觉得需要使用 控制反转 (IoC)容器。 但是,我读的越多,我觉得社区使用IoC容器的压力就越大。 我使用.NET容器,如 StructureMap , NInject , Unity 和 Funq 。 我仍然没有看到IoC容器如何使我的代码受益/改进。 我也害怕在工作中开始使用容器,因为我的许多同事都会看到他们不理解的代码。 他们中的许多人可能不愿意学习新技术。 请说服我需要使用IoC容器。 当我在工作中与开发人员交谈时,我将使用这些论点。 #1楼 我是一名康复的国际奥委会成瘾者。 这些天我发现很难证明在大多数情况下使用IOC来证明其合理性。 IOC容器牺牲了编译时检查,据说可以为您提供“简单”设置,复杂的生命周期管理以及在运行时动态发现依赖关系。 我发现编译时间检查的丢失以及由此导致的运行时间魔术/异常,在绝大多数情况下都不值得花里胡哨。 在大型企业应用程序中,他们可能很难跟踪正在发生的事情。 我不购买集中化论证,因为你可以非常容易地集中静态设置,为你的应用程序使用抽象工厂,并虔诚地将对象创建推迟到抽象工厂,即做适当的DI。 为什么不这样做静态无魔法DI: interface IServiceA { } interface

Configuring StructureMap to generate and “remember” a constructor param literal

ぐ巨炮叔叔 提交于 2020-01-05 07:58:28
问题 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); Via constructor injection, a chain of objects can be created, with some needing an

Configuring StructureMap to generate and “remember” a constructor param literal

依然范特西╮ 提交于 2020-01-05 07:58:02
问题 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); Via constructor injection, a chain of objects can be created, with some needing an

Using structure map with MSTest

a 夏天 提交于 2020-01-05 04:09:18
问题 I have created myself a test project with a single test in it. Within the "TestInitialize" method I'm trying to call my StructureMap setup method where I set which concrete instances should be used for which interfaces. However as soon as the test tries to run I get a {"Operation could destabilize the runtime."} exception. Without using structuremap it works fine (but obiviously I need it in there). Can anyone point me in the right direction as to what might be causing this exception? 回答1:

StructureMap - Can the state of cached entity be inspected in a custom ILifecycle?

核能气质少年 提交于 2020-01-05 03:56:28
问题 I'm spiking a custom StructureMap ILifecycle with semantics similar to the SingletonLifecycle but with a predicate that must be met for the existing instance to be returned. The idea is that the custom lifecycle class will take a predicate as a constructor parameter, and that if the predicate is not met then a new empty cache will be returned. So far I've based the code on the existing SingletonLifecycle but can see no way of inspecting the state of the cached entity. From within an

How Structure Map Automocker Inject works?

☆樱花仙子☆ 提交于 2020-01-04 12:47:41
问题 I have constructor containing IEnumerable parameter. When I try to Inject concrete object to automocker it is not used. When I use wrapper class containing IEnumerable property all works as expected. How can I test TestClass1? IEnumerable parameter public class TestClass1 { public TestClass1(IEnumerable<IItem> items) { Items = items; } public IEnumerable<IItem> Items { get; private set; } } [TestMethod] public void TestClass1Constructor() { RhinoAutoMocker<TestClass1> autoMocker = new

Registering Method on Generic Factory with StructureMap

被刻印的时光 ゝ 提交于 2020-01-03 17:20:49
问题 I am trying to use a method on a generic factory class in my structuremap registry. Normally, i would use the following when registering a type using a factory method: For<Foo>().Use(x => new FooFactory().GetFoo()); And the following when registering a generic type: For(typeof(ISomeGeneric<>)).Use(typeof(SomeGeneric<>)); How can I combine the two and retrieve a generic type from a generic factory method? I think it should be something like: For(typeof(IFoo<>)).Use(typeof(x => new FooFactory<>

Need help implementing Command Handlers/Bus using Unity DI

时间秒杀一切 提交于 2020-01-03 02:04:17
问题 Folks, I am trying to re-factor a legacy brownfield application into a CQRS architecture with commands and a command bus for domain modifications. The application will more than likely be implemented in Asp.Net MVC3. My employer prefers the use of Unity for DI in MVC applications. Any examples I can find showing a dependency container for command/bus resolution are based on Structuremap or Autofac, however I will need to use Unity in this implementation. Has anyone used Unity in this manner

How do you Resolve signalR v2.0 with StructureMap v2.6

时光毁灭记忆、已成空白 提交于 2020-01-02 09:32:18
问题 In Application_Start of Global.asax i have the following ObjectFactory.Initialize(cfg => { cfg.For<IDependencyResolver>().Singleton().Add<StructureMapDependencyResolver> (); }); My Interface for the Hub is public interface IDashboardHub { void Initialize(); } and my hub is as follows: public class DashboardHub : Hub, IDashboardHub { private readonly ICpeAccountService _accountService; public DashboardHub(ICpeAccountService service) { _accountService = service; } [Authorize] public void