Autofac.如何使用自定义方法(属性)解析某些界面?

编程入门 行业动态 更新时间:2024-10-27 04:26:32
本文介绍了Autofac.如何使用自定义方法(属性)解析某些界面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下接口:

public interface IConfigurationProvider<TSettings> where TSettings : ISettings, new() { TSettings Settings { get; } } public interface ISettings { }

我具有以下IConfigurationProvider实现:

I have the following implementation of IConfigurationProvider:

public class ConfigurationProvider<TSettings> : IConfigurationProvider<TSettings> where TSettings : ISettings, new() { public ConfigurationProvider() { this.BuildConfiguration(); } public TSettings Settings { get; private set; } private void BuildConfiguration() { this.Settings = new TSettings(); //...load and assign properties to 'this.Settings' //...skipped // now 'Settings' property contains configured 'ISettings' instance } }

我也可以有不同的类来实现"ISettings"接口.例如,

I can also have distinct classes implementing 'ISettings' interface. For example,

public class UserSettings : ISettings { public int SomeProperty1 { get; set; } public int SomeProperty2 { get; set; } } public class CatalogSettings : ISettings { public int SomeProperty3 { get; set; } public int SomeProperty4 { get; set; } }

我正在使用以下代码来配置"ContainerBuilder":builder.RegisterGeneric(typeof(ConfigurationProvider<>)).As(typeof(IConfigurationProvider<>));

I'm using the following code to configure 'ContainerBuilder': builder.RegisterGeneric(typeof(ConfigurationProvider<>)).As(typeof(IConfigurationProvider<>));

工作正常.为了获得"UserSettings",我使用以下代码:

It works fine. And in order to get 'UserSettings' I use the following code:

var userSettingsProvider = builder.Resolve<IConfigurationProvider<UserSettings>>(); var userSettings = userSettingsProvider.Settings;

问题:我应该如何配置"ContainerBuilder",以便可以通过以下方式解决某些"ISettings"问题:

The question: how should I configure 'ContainerBuilder' so I can resolve a certain 'ISettings' the following way:

var userSettings = builder.Resolve<UserSettings>();

Autofac可以吗?

Is it possible with Autofac?

预先感谢

推荐答案

您可以使用IRegistrationSource进行此操作-请参见 nblumhardt/2010/01/declarative-context-adapters-autofac2/进行概述.

You can do this with an IRegistrationSource - see nblumhardt/2010/01/declarative-context-adapters-autofac2/ for an overview.

基本结构为:

class SettingsSource : IRegistrationSource { static readonly MethodInfo BuildMethod = typeof(SettingsSource).GetMethod( "BuildRegistration", BindingFlags.Static | BindingFlags.NonPublic); public IEnumerable<IComponentRegistration> RegistrationsFor( Service service, Func<Service, IEnumerable<IComponentRegistration>> registrations) { var ts= service as TypedService; if (ts != null && typeof(ISettings).IsAssignableFrom(ts.ServiceType) { var buildMethod = BuildMethod.MakeGenericMethod(ts.ServiceType); yield return (IComponentRegistration) buildMethod.Invoke(null, null); } } static IComponentRegistration BuildRegistration<TSettings>() where TSettings : ISettings { return RegistrationBuilder .ForDelegate((c, p) => c.Resolve<IConfigurationProvider<TSettings>>().Settings) .CreateRegistration(); } public bool IsAdapterForIndividualComponents { get { return false; } } }

这是这样注册的:

builder.RegisterGeneric(typeof(ConfigurationProvider<>)) .As(typeof(IConfigurationProvider<>)); builder.RegisterSource(new SettingsSource());

(未经编译或测试,请让我知道它是否掉下来;))

(Not compiled or tested, so let me know if it falls over ;))

更多推荐

Autofac.如何使用自定义方法(属性)解析某些界面?

本文发布于:2023-11-05 21:15:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561882.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   如何使用   属性   界面   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!