Autofac 和开放仿制药

发布于 2024-11-18 18:16:10 字数 1482 浏览 0 评论 0原文

这个场景有可能实现吗?

[TestFixture]
    public class AutofacTests
    {
        private IContainer _container;

        public AutofacTests()
        {
            var builder = new ContainerBuilder();

            builder.RegisterType<Command1>();
            builder.RegisterType<Command2>();

            builder.RegisterType<CommandHandler1>();
            builder.RegisterType<CommandHandler2>();

            builder.RegisterGeneric(typeof (IHandle<>));

            _container = builder.Build();
        }

        [Test]
        [Ignore]
        public void Open_generics_test()
        {
            //this isn't working
            CommandHandler1 commandHadler1 = _container.Resolve<IHandle<Command1>>();
            CommandHandler2 commandHadler2 = _container.Resolve<IHandle<Command2>>();
        }

        interface IHandle<T> where T : class
        {
            void Handle(T command);
        }

        class CommandHandler1 : IHandle<Command1>
        {
            public void Handle(Command1 command)
            {
                throw new System.NotImplementedException();
            }
        }

        class Command1{}

        class CommandHandler2 : IHandle<Command2>
        {
            public void Handle(Command2 command)
            {
                throw new System.NotImplementedException();
            }
        }

        class Command2{}
    }

Is it possible to get this scenario to work ?

[TestFixture]
    public class AutofacTests
    {
        private IContainer _container;

        public AutofacTests()
        {
            var builder = new ContainerBuilder();

            builder.RegisterType<Command1>();
            builder.RegisterType<Command2>();

            builder.RegisterType<CommandHandler1>();
            builder.RegisterType<CommandHandler2>();

            builder.RegisterGeneric(typeof (IHandle<>));

            _container = builder.Build();
        }

        [Test]
        [Ignore]
        public void Open_generics_test()
        {
            //this isn't working
            CommandHandler1 commandHadler1 = _container.Resolve<IHandle<Command1>>();
            CommandHandler2 commandHadler2 = _container.Resolve<IHandle<Command2>>();
        }

        interface IHandle<T> where T : class
        {
            void Handle(T command);
        }

        class CommandHandler1 : IHandle<Command1>
        {
            public void Handle(Command1 command)
            {
                throw new System.NotImplementedException();
            }
        }

        class Command1{}

        class CommandHandler2 : IHandle<Command2>
        {
            public void Handle(Command2 command)
            {
                throw new System.NotImplementedException();
            }
        }

        class Command2{}
    }

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

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

发布评论

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

评论(1

夏日落 2024-11-25 18:16:10

我很确定 RegisterGeneric 需要一个具体的实现类型来关闭(例如 Handler。我不认为你可以使用像你这样的接口。

你可以使用以下替代注册代码实现您想要的效果,

var builder = new ContainerBuilder();

builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
    .AsClosedTypesOf(typeof(IHandle<>));

_container = builder.Build();

var commandHandler1 = _container.Resolve<IHandle<Command1>>();
var commandHandler2 = _container.Resolve<IHandle<Command2>>();

我已将其工作版本添加到 AutofacAnswers 上。 GitHub。

I'm pretty sure RegisterGeneric requires a concrete implementation type to close over (e.g. Handler<T>. I don't think you can use an interface like you have.

You can achieve what you want with the following alternative registration code.

var builder = new ContainerBuilder();

builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
    .AsClosedTypesOf(typeof(IHandle<>));

_container = builder.Build();

var commandHandler1 = _container.Resolve<IHandle<Command1>>();
var commandHandler2 = _container.Resolve<IHandle<Command2>>();

I have added a working version of this to AutofacAnswers on GitHub.

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