2016-04-20 107 views

回答

2

传统上你会使用StructureMap的ObjectFactory.GetInstance<T>来解决你的静态方法的依赖性。不过,由于使用它会将代码紧密耦合到IoC容器(请参阅服务定位器反模式上的this post),因此它一直被弃用。

下一个最好的方法是创建一个返回的IContainer实例的ObjectFactory,类似这样的自己的静态等效:

public static class ObjectFactory 
{ 
    private static readonly Lazy<Container> _containerBuilder = new Lazy<Container>(defaultContainer, LazyThreadSafetyMode.ExecutionAndPublication); 

    public static IContainer Container 
    { 
     get { return _containerBuilder.Value; } 
    } 

    private static Container defaultContainer() 
    { 
      return new Container(x => { 
       x.AddRegistry(new YourRegistry()) }; 
      }); 
    } 
} 

this后进行了较深入实施。

+0

我目前正在执行此解决方案。一旦我确认它有效,我会接受答案。谢谢! – jkruer01

+0

没问题。你有没有设法让它工作?乐意效劳。 –

相关问题