我正在使用提供者模式和 Unity。
这是典型的提供者实现。我有SomeProvider
作为抽象基础提供者的抽象方法以及实例化defaultProvider
.
public abstract class SomeProvider : ProviderBase
{
#region provider boilerplate
private const string PROVIDER_NAME = "someProvider";
private static volatile SomeProvider defaultProvider = null;
public static SomeProvider Provider
{
get { return defaultProvider; }
}
private static object providerLockObject = new object();
static SomeProvider()
{
LoadProvider();
}
private static void LoadProvider()
{
if (defaultProvider == null)
{
lock (providerLockObject)
{
if (defaultProvider == null)
{
// exception handling omitted for brevity
var section = ConfigurationManager.GetSection(PROVIDER_NAME)
as BaseProviderConfigurationSection;
defaultProvider = ProvidersHelper.InstantiateProvider(
section.Providers[section.DefaultProvider], typeof(SomeProvider)) as SomeProvider;
}
}
}
}
protected SomeProvider() { }
#endregion
#region abstract methods
public abstract bool DoSomething();
#endregion
}
这是ASomeProvider
实现的SomeProvider
。请注意,ASomeProvider
具有依赖性ADependency
但SomeProvider
没有。
public class ASomeProvider : SomeProvider
{
#region provider boilerplate
private string name;
public override string Name
{
get { return name; }
}
public override void Initialize(string name, NameValueCollection config)
{
this.name = name;
base.Initialize(name, config);
}
#endregion
// Provider pattern needs parameterless ctor and calls this
public ASomeProvider() { }
// constructor injection
public ASomeProvider(ADependency aDependency)
{
this.ADependency = aDependency;
}
[Dependency]
public SomeDependency ADependency { get; set; }
#region methods
public override void DoSomething()
{
// do something
}
#endregion
}
class SomeDependency {}
我ASomeProvider
在业务层中使用如下:
public class SomeBusinessLayer
{
public SomeProvider someProvider;
public SomeBusinessLayer(SomeProvider someProvider)
{
this.someProvider = someProvider;
}
#region methods
public bool DoSomethingWrapper()
{
return someProvider.DoSomething();
}
#endregion
}
我有BusinessLayerFactory
使用 Unity 连接对象并返回对象的工厂,如下所示:
public static class BusinessLayerFactory
{
private static UnityContainer container;
private static void WireUp()
{
container = new UnityContainer();
container.RegisterInstance(SomeProvider.Provider);
container.RegisterInstance(new SomeDependency());
container.RegisterType<SomeBusinessLayer>(new ContainerControlledLifetimeManager());
}
public static SomeBusinessLayer SomeBusinessLayer_Unity
{
get
{
return container.Resolve<SomeBusinessLayer>();
}
}
public static SomeBusinessLayer SomeBusinessLayer_Self()
{
var asomeProvider = SomeProvider.Provider as ASomeProvider;
if (asomeProvider != null && asomeProvider.ADependency == null)
asomeProvider.ADependency = new ADependency();
return new SomeBusinessLayer(SomeProvider.Provider);
}
}
我遇到的问题是,当我解决 get 时SomeBusinessLayer
,someProvider
它的依赖项ADependency
为 null。
var someBusinessLayer = BusinessLayerFactory.SomeBusinessLayer_Unity;
((ASomeProvider)someBusinessLayer.someProvider).ADependency
是null
。
原因是提供者模式使用辅助方法ProvidersHelper.InstantiateProvider(ProviderSettings providerSettings, Type type)
来实例化默认提供者,而不是通过 Unity 的Resolve<T>()
方法。
我可以考虑在没有 Unity 的情况下执行此操作,如我在SomeBusinessLayer_Self
where I new ADependency
if中所示,null
但只是想知道 Unity 是否以及如何处理此问题。
方法注入或Ctor注入对我来说很好。
我该如何解决这个问题并让 Unity 和 Provider 模式一起工作?我使用 Unity 的主要原因是在工厂中连接对象。
我没有使用 MVC。
更新:
要解决此问题: . 给@seth-flowers的小费
如果没有明确的演员表,我想不出办法container.BuildUp<ASomeProvider>(aSomeProvider)
。
container.RegisterInstance(new SomeDependency());
//this does not build up the object with dependency
container.BuildUp(SomeProvider.Provider);
//this works and does build up
var aSomeProvider = SomeProvider.Provider as ASomeProvider;
if(aSomeProvider != null)
container.BuildUp<ASomeProvider>(aSomeProvider);
//compile error
container.BuildUp<ASomeProvider>(SomeProvider.Provider);
container.RegisterInstance(SomeProvider.Provider);
container.RegisterType<SomeBusinessService>(new ContainerControlledLifetimeManager());