Hello,
I have a static class in my codes. I need to resolve a dependency using IocManager.Instance. but it doesn't seem to be resolving the dependency. It seems this instance is not the same as the application IocManager.
Is there any suggestion for the workaround?
public static class TradeFactory
{
private static ITradeIdGenerator TradeIdGenerator
{
get
{
return IocManager.Instance.Resolve();
}
}
public static Trade GenerateTrade(string currencyPair, Price executionPrice, Volume executedQuantity,Order matchedOrder, Order inboundOrder)
{
Trade trade=new Trade(TradeIdGenerator.GenerateTradeId(),currencyPair, executionPrice, executedQuantity, DateTime.Now,
matchedOrder, inboundOrder);
return trade;
}
}
Regards,
Gunpal Jain
10 Answer(s)
-
0
-
Does ITradeIdGenerator have an implementation that also implements ISingletonDependency (or ITransientDependency)?
-
Does it work when ITradeIdGenerator is constructor-injected into a non-static class?
-
Can you post the stack trace in Logs.txt?
-
-
0
I get this problem too.
public class ComponentsInstaller : IWindsorInstaller { public void Install(IWindsorContainer container, IConfigurationStore store){ container.Register(Component.For<ICustomerConnector>().ImplementedBy<CustomerConnector>().LifestyleTransient()); } } MyCoreModule: public override void Initialize() ... //Here it works var connector = IocManager.Resolve<ICustomerConnector>(); } CustomerConnectorFactory : MyServiceBase readonly IWindsorContainer _container; public CustomerConnectorFactory(IWindsorContainer container) { _container = container; } public ICustomerConnector Create() { //This works! var conn = _container.Resolve<ICustomerConnector>(); //This breaks with ComponentNotFoundException var connector = IocManager.Instance.IocContainer.Resolve<ICustomerConnector>(); return connector; }
-
0
Why use
IocManager.Instance
? -
0
Because this Factory is overridden by multiple other factories, so there is much more code in form of constructors to add and maintain. Why would I not use it (except that it doesn't work)?
-
0
Because your code will not be easy to test. You can use the Property Injection pattern.
-
0
Yes, this is true. I will use injection instead. But isn't it still weird that it doesn't work if I would have had a static factory that required the static Instance?
-
0
Where do you run ICustomerConnector's Create method ?
-
0
In a test class in the TestModule.
-
0
@dennisahlin then there might be missing module dependency but I don't suggest using IocManager in unit tests because of https://github.com/aspnetboilerplate/aspnetboilerplate/issues/585#issuecomment-135305251
-
0
Ok, good to know. Thank you!