StructureMap 2.5 和内部实现者

发布于 2024-08-07 00:39:54 字数 2713 浏览 10 评论 0原文

在下面的示例中,是否有可能让这些东西工作(强制 Objectfactory 创建实例(如 Activator)),

所有内容都放置在单个程序集

public interface IUnitOfWorkFactory
{
    IUnitOfWork Create();
}

internal class NHUnitOfWorkFactory : IUnitOfWorkFactory
{
    public IUnitOfWork Create()
    {
        ////  do needed stuff
    }
}

引导中:

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)

is it possible to get this stuff working(some way to force Objectfactory create instances like Activator)

in the below example everything is placed in a sigle assembly

public interface IUnitOfWorkFactory
{
    IUnitOfWork Create();
}

internal class NHUnitOfWorkFactory : IUnitOfWorkFactory
{
    public IUnitOfWork Create()
    {
        ////  do needed stuff
    }
}

bootstrapping :

ObjectFactory.Configure(x =>
{
    x.ForRequesedType<IUnitOfWorkFactory>.TheDefaultIsConcreteType<NHUnitOfWorkFactory>();
}); 

usage:

IUnitOfWorkFactory factory = ObjectFactory.GetInstance<IUnitOfWorkFactory>();

My result:

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如日中天 2024-08-14 00:39:54

你有两个选择。您可以将 NHUnitOfWorkFactory 公开(首选)。

或者,您可以将构造内部类的代码以 lambda 的形式放入程序集中(可以访问内部成员),并将其传递给 StructureMap:

ObjectFactory.Configure(x => {
  x.ForRequestedType<IUnitOfWorkFactory>()
   .TheDefault.Is.ConstructedBy(() => new NHUnitOfWorkFactory())
});

较新版本的 StructureMap 的等效项是:

ObjectFactory.Configure(x => {
  x.For<IUnitOfWorkFactory>().Use(() => new NHUnitOfWorkFactory())
});

You have 2 options. You can make NHUnitOfWorkFactory public (preferred).

Or you can put the code that constructs your internal class in your assembly (where it has access to internal members) in the form of a lambda , and pass it to StructureMap:

ObjectFactory.Configure(x => {
  x.ForRequestedType<IUnitOfWorkFactory>()
   .TheDefault.Is.ConstructedBy(() => new NHUnitOfWorkFactory())
});

The equivalent with newer versions of StructureMap is:

ObjectFactory.Configure(x => {
  x.For<IUnitOfWorkFactory>().Use(() => new NHUnitOfWorkFactory())
});
前事休说 2024-08-14 00:39:54

我还没有验证这一点,但是使 NHUnitOfWorkFactory 程序集的内部结构对 StructureMap 程序集可见可能会成功。

您可以在 AssemblyInfo 中使用 InternalsVisibleTo 属性班级。

I haven't verified this, but making the internals of the assembly of NHUnitOfWorkFactory visible to the StructureMap assembly might do the trick.

You use the InternalsVisibleTo attribute in your AssemblyInfo class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文