Autofac获得按约定的基础上构造函数的参数名称装饰QueryHandler? [英] Autofac get decorated QueryHandler by convention based on constructor parameter name?

查看:384
本文介绍了Autofac获得按约定的基础上构造函数的参数名称装饰QueryHandler?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们注入 IQueryHandler< TQuery的,TResult> 进入我们的MVC控制器。我们在全球注册所有这些在容器

We inject IQueryHandler<TQUery,TResult> into our MVC controllers. We globally register all of these in the container

我们已经写了装饰,能够缓存的 IQueryHandler 的结果。

We have written a decorator that can cache the results of IQueryHandler.

我们希望有时相同的处理获得缓存reults等次没有。

We want to sometimes get cached reults and other times not from the same handler.

是否有可能获得有条件基于构造函数参数的名称装饰处理。例如注 IQueryHandler&LT; UnemployedQuery,IEnumerable的&LT;人与GT;&GT; cachedPeopleHandler 如果我们用缓存的preFIX构造函数参数的名称,我们实际上把它包裹着装饰?

Is it possible to conditionally get a decorated handler based on the name of the constructor parameter. e.g. inject IQueryHandler<UnemployedQuery, IEnumerable<People>> cachedPeopleHandler if we prefix constructor parameter name with cached we actually get it wrapped with decorator?

只是想使用更约定优于配置的方法来简化事情。

Just trying to use a more convention over configuration approach to simplify things.

推荐答案

是有可能做到这一点。下面是关于如何实现它一个简单的例子:

Yes it's possible to do it. Below is a simple working example on how you can achieve it:

class Program
{
    public interface IQueryHandler{}

    private class QueryHandler : IQueryHandler
    {
    }

    private class CacheQueryHandler : IQueryHandler
    {
    }

    public interface IService
    {
    }

    private class Service : IService
    {
        private readonly IQueryHandler _queryHandler;
        private readonly IQueryHandler _cacheQueryHandler;

        public Service(IQueryHandler queryHandler, IQueryHandler cacheQueryHandler)
        {
            _queryHandler = queryHandler;
            _cacheQueryHandler = cacheQueryHandler;
        }

        public override string ToString()
        {
            return string.Format("_queryHandler is {0}; _cacheQueryHandler is {1}", _queryHandler,
                _cacheQueryHandler);
        }
    }

    static void Main(string[] args)
    {
        var builder = new ContainerBuilder();
        // Register the dependency
        builder.RegisterType<QueryHandler>().As<IQueryHandler>();
        // Register the decorator of the dependency
        builder.RegisterType<CacheQueryHandler>().Keyed<IQueryHandler>("cache");

        // Register the service implementation
        builder.RegisterType<Service>().AsSelf();

        // Register the interface of the service
        builder.Register(c =>
        {
            var ctor = typeof (Service).GetConstructors()[0];

            var parameters =
                ctor.GetParameters()
                    .Where(p => p.Name.StartsWith("cache"))
                    .Select(p => new NamedParameter(p.Name, c.ResolveKeyed("cache", p.ParameterType)));

            return c.Resolve<Service>(parameters);
        }).As<IService>();

        using (var container = builder.Build())
        {
            var service = container.Resolve<IService>();
            Console.WriteLine(service.ToString());

            Console.ReadKey();
        }

    }
}

更新:结果
基本上你需要:结果
1.想起来了一般惯例。 preFIX你的情况构造函数参数名的缓存。结果
2.注册你的依赖关系如常。结果
3.注册您的装饰,所以他们不会覆盖原始的依赖,你可以很容易地解决这些问题基础上的约定。例如键控,命名,通过属性等结果
4.注册您实际的实现类的使用装饰结果
5.注册您的界面,通过里面有所有法术的lambda前pression描述类。

Update:
Basically you need to:
1. Think up a general convention. Prefix "cache" of ctor parameter name in your case.
2. Register your dependencies as usual.
3. Register your decorators, so they don't overwrite your original dependencies and you can easily resolve them basing on your convention. e.g. Keyed, Named, via Attribute, etc.
4. Register you actual implementation of class that uses decorators
5. Register your interface that describes the class via lambda expression that has all magic inside.

请注意:我公司提供的只是一个简单的和工作的例子。这是对你做那该多好,使用方便,快捷如使得它作为一个扩展的,通用的,反映缓存等结果这并不难呢。结果
谢谢你。

Note: I provided just a simple and working example. It's on you to make it nice, easy to use and fast e.g. make it as an extension, generic, cache reflection results etc. It's not difficult anyway.
Thanks.

这篇关于Autofac获得按约定的基础上构造函数的参数名称装饰QueryHandler?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆