是否可以使这些东西工作(某种方式强制Objectfactory创建像Activator这样的实例)
在下面的示例中,所有内容都放在一个sigle程序集中
public interface IUnitOfWorkFactory
{
IUnitOfWork Create();
}
internal class NHUnitOfWorkFactory : IUnitOfWorkFactory
{
public IUnitOfWork Create()
{
//// do needed stuff
}
}
bootstrapping:
ObjectFactory.Configure(x =>
{
x.ForRequesedType<IUnitOfWorkFactory>.TheDefaultIsConcreteType<NHUnitOfWorkFactory>();
});
用法:
IUnitOfWorkFactory factory = ObjectFactory.GetInstance<IUnitOfWorkFactory>();
我的结果:
Porktal.Core.Tests.UnitOfWorkTests.Can_Start_Unit_of_Work : StructureMap.StructureMapException : StructureMap Exception Code: 207 Internal exception while creating Instance 'Porktal.Core.Data.NH.NHUnitOfWorkFactory, Porktal.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' of PluginType Porktal.Core.Data.IUnitOfWorkFactory. Check the inner exception for more details. ---- System.MethodAccessException : Porktal.Core.Data.NH.NHUnitOfWorkFactory..ctor() Stack Trace: at StructureMap.Pipeline.ConfiguredInstanceBase`1.StructureMap.Pipeline.IConfiguredInstance.Build(Type pluginType, BuildSession session, InstanceBuilder builder) at StructureMap.Pipeline.ConfiguredInstanceBase`1.build(Type pluginType, BuildSession session) at StructureMap.Pipeline.Instance.createRawObject(Type pluginType, BuildSession session) at StructureMap.Pipeline.Instance.Build(Type pluginType, BuildSession session) at StructureMap.Pipeline.BuildPolicy.Build(BuildSession buildSession, Type pluginType, Instance instance) at StructureMap.InstanceFactory.Build(BuildSession session, Instance instance) at StructureMap.BuildSession.CreateInstance(Type pluginType, Instance instance) at StructureMap.BuildSession.b__0(Type t) at StructureMap.Util.Cache`2.get_Item(KEY key) at StructureMap.BuildSession.CreateInstance(Type pluginType) at StructureMap.Container.GetInstance(Type pluginType) at StructureMap.Container.GetInstance[T]() at StructureMap.ObjectFactory.GetInstance[PLUGINTYPE]() at Porktal.Core.Bootstraper.Bootstrap() in D:\Porktal\Porktal.Core\Bootstraper.cs:line 20 at Porktal.Core.Tests.UnitOfWorkTests.Can_Start_Unit_of_Work() in D:\Porktal\Porktal.Core.Tests\UnitOfWorkTests.cs:line 11 ----- Inner Stack Trace ----- at PorktalCoreDataNHNHUnitOfWorkFactoryInstanceBuilder44203c8113d44053be045df4db28c3dc.BuildInstance(IConfiguredInstance , BuildSession ) at StructureMap.Pipeline.ConfiguredInstanceBase`1.StructureMap.Pipeline.IConfiguredInstance.Build(Type pluginType, BuildSession session, InstanceBuilder builder)
答案 0 :(得分:9)
您有2个选项。您可以将NHUnitOfWorkFactory公开(首选)。
或者您可以将构建内部类的代码放在程序集中(它可以访问内部成员)以lambda的形式,并将其传递给StructureMap:
ObjectFactory.Configure(x => {
x.ForRequestedType<IUnitOfWorkFactory>()
.TheDefault.Is.ConstructedBy(() => new NHUnitOfWorkFactory())
});
与更新版本的StructureMap相当:
ObjectFactory.Configure(x => {
x.For<IUnitOfWorkFactory>().Use(() => new NHUnitOfWorkFactory())
});
答案 1 :(得分:0)
我还没有验证这一点,但是使结构化组件可以看到NHUnitOfWorkFactory程序集的内部结构可能会起到作用。
您在AssemblyInfo类中使用InternalsVisibleTo属性。