StructureMap InstanceInterceptor 未被调用

发布于 2024-08-18 20:27:48 字数 606 浏览 5 评论 0原文

我想拦截 SM 中实例的创建,我正在尝试以下操作,但它没有调用 InstanceInterceptor 实现,有人知道为什么吗?

ForRequestedType<IPublishResources>()
 .TheDefault
 .Is
 .OfConcreteType<PublisherService>()
 .InterceptWith(new PublisherServiceInterceptor());

测试代码使用ObjectFactory创建实例,如下所示:

// Given we have a configure object factory in StructureMap...
ObjectFactory.Configure(x => x.AddRegistry(new StructureMapServiceRegistry()));

// When we request a publisher service...
var publisher = ObjectFactory.GetInstance<IPublishResources>();

Cheers

AWC

I want to intercept the creation of an instance in SM and I'm trying the following but it's not calling the InstanceInterceptor implementation, does anyone know why?

ForRequestedType<IPublishResources>()
 .TheDefault
 .Is
 .OfConcreteType<PublisherService>()
 .InterceptWith(new PublisherServiceInterceptor());

The test code uses the ObjectFactory to create instances, and is shown below:

// Given we have a configure object factory in StructureMap...
ObjectFactory.Configure(x => x.AddRegistry(new StructureMapServiceRegistry()));

// When we request a publisher service...
var publisher = ObjectFactory.GetInstance<IPublishResources>();

Cheers

AWC

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

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

发布评论

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

评论(1

戏舞 2024-08-25 20:27:48

我无法在 2.5.4 版本中重现您的问题。这是我的代码。

public interface IPublishResources {}
class PublishResources : IPublishResources {}
public class LoggingInterceptor : InstanceInterceptor
{
    //this interceptor is a silly example of one
    public object Process(object target, IContext context)
    {
        Console.WriteLine("Interceptor Called");
        return context.GetInstance<PublishResources>();
    }
}

public class MyRegistry : Registry
{
    public MyRegistry()
    {
        For<IPublishResources>()
            .Use<PublishResources>()
            .InterceptWith(new LoggingInterceptor());
    }
}

[TestFixture]
public class Structuremap_interception_configuraiton
{
    [Test]
    public void connecting_implementations()
    {
        var container = new Container(cfg =>
        {
            cfg.AddRegistry<MyRegistry>();
        });

        container.GetInstance<IPublishResources>();
    }
}

一个问题。你真的需要在这里使用拦截器吗?如果您只需要定义一个工厂,您可以这样做。

    public interface IPublishResourcesFactory
{
    IPublishResources Create();
}

public class MyRegistry : Registry
{
    public MyRegistry()
    {
        For<IPublishResources>().Use(c =>
        {
            return c.GetInstance<IPublishResourcesFactory>().Create();
        });

        //or

        For<IPublishResources>().Use(c =>
        {
            //other object building code.
            return new PublishResources();
        });
    }
}

I could not reproduce your problem in release 2.5.4. Here is my code.

public interface IPublishResources {}
class PublishResources : IPublishResources {}
public class LoggingInterceptor : InstanceInterceptor
{
    //this interceptor is a silly example of one
    public object Process(object target, IContext context)
    {
        Console.WriteLine("Interceptor Called");
        return context.GetInstance<PublishResources>();
    }
}

public class MyRegistry : Registry
{
    public MyRegistry()
    {
        For<IPublishResources>()
            .Use<PublishResources>()
            .InterceptWith(new LoggingInterceptor());
    }
}

[TestFixture]
public class Structuremap_interception_configuraiton
{
    [Test]
    public void connecting_implementations()
    {
        var container = new Container(cfg =>
        {
            cfg.AddRegistry<MyRegistry>();
        });

        container.GetInstance<IPublishResources>();
    }
}

A question. Do you really need to use an Interceptor here? If you only need to define a factory you can do somethign like this.

    public interface IPublishResourcesFactory
{
    IPublishResources Create();
}

public class MyRegistry : Registry
{
    public MyRegistry()
    {
        For<IPublishResources>().Use(c =>
        {
            return c.GetInstance<IPublishResourcesFactory>().Create();
        });

        //or

        For<IPublishResources>().Use(c =>
        {
            //other object building code.
            return new PublishResources();
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文