使用Unity注入对象插入的IValueConverter实例(Using Unity to inj

2019-06-23 20:49发布

我在Silverlight 5项目,自定义数据转换成不同颜色的IValueConverter的一个实例。 我需要从数据库中读出的实际颜色值(因为这些可以由用户编辑)。

由于Silverlight使用异步调用从数据库中加载通过实体框架的数据,我创建了一个简单的存储库,它从数据库保存的值。

接口:

public interface IConfigurationsRepository
{
    string this[string key] { get; }
}

执行:

public class ConfigurationRepository : IConfigurationsRepository
{
    private readonly TdTerminalService _service = new TdTerminalService();

    public ConfigurationRepository()
    {
        ConfigurationParameters = new Dictionary<string, string>();
        _service.LoadConfigurations().Completed += (s, e) =>
            {
                var loadOperation = (LoadOperation<Configuration>) s;
                foreach (Configuration configuration in loadOperation.Entities)
                {
                    ConfigurationParameters[configuration.ParameterKey] = configuration.ParameterValue;
                }
            };
    }

    private IDictionary<string, string> ConfigurationParameters { get; set; }

    public string this[string key]
    {
        get
        {
            return ConfigurationParameters[key];
        }
    }
}

现在我想用统一到我的仓库的这种情况下注入的IValueConverter实例...

App.xaml.cs:

private void RegisterTypes()
{
    _container = new UnityContainer();
    IConfigurationsRepository configurationsRepository = new ConfigurationRepository();
    _container.RegisterInstance<IConfigurationsRepository>(configurationsRepository);
}

的IValueConverter:

public class SomeValueToBrushConverter : IValueConverter
{
    [Dependency]
    private ConfigurationRepository ConfigurationRepository { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
       switch ((SomeValue)value)
        {
            case SomeValue.Occupied:
                return new SolidColorBrush(ConfigurationRepository[OccupiedColor]);
            default:
                throw new ArgumentException();
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

问题是,我没有在转换器实例中得到相同的统一容器(即库未注册)。

Answer 1:

根据你的意见,你需要使用一个服务定位,让您的ConfigurationRepository的实例,导致由Unity创建的转换isn't的实例,但在Silverlight中/ XAML引擎。

所以,你的装饰有该DependencyAttribute不需额外属性被注入。

C#

public class SomeValueToBrushConverter : IValueConverter
{
    public SomeValueToBrushConverter(){
      ConfigurationRepository = ServiceLocator.Current.GetInstance<ConfigurationRepository>();
    }

    private ConfigurationRepository ConfigurationRepository { get; set; }
}

在你RegisterTypes方法,你需要配置服务定位:

_container = new UnityContainer();
UnityServiceLocator locator = new UnityServiceLocator(_container);
ServiceLocator.SetLocatorProvider(() => locator);


Answer 2:

它是可以使用MarkupExtension解决从DI容器的依赖关系:

public class IocResolver : MarkupExtension
{
    public IocResolver()
    { }

    public IocResolver(string namedInstance)
    {
        NamedInstance = namedInstance;
    }

    [ConstructorArgument("namedInstance")]
    public string NamedInstance { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        var provideValueTarget = (IProvideValueTarget)serviceProvider
            .GetService(typeof(IProvideValueTarget));

        // Find the type of the property we are resolving
        var targetProperty = provideValueTarget.TargetProperty as PropertyInfo;

        if (targetProperty == null)
            throw new InvalidProgramException();

        Debug.Assert(Resolve != null, "Resolve must not be null. Please initialize resolving method during application startup.");
        Debug.Assert(ResolveNamed != null, "Resolve must not be null. Please initialize resolving method during application startup.");

        // Find the implementation of the type in the container
        return NamedInstance == null
            ? (Resolve != null ? Resolve(targetProperty.PropertyType) : DependencyProperty.UnsetValue)
            : (ResolveNamed != null ? ResolveNamed(targetProperty.PropertyType, NamedInstance) : DependencyProperty.UnsetValue);
    }

    public static Func<Type, object> Resolve { get; set; }
    public static Func<Type, string, object> ResolveNamed { get; set; }
}

该IocResolver不得不在应用程序启动等初始化:

IocResolver.Resolve = kernel.Get; 
IocResolver.ResolveNamed = kernel.GetNamed;
// or what ever your DI container looks like

在此之后,你可以用它在XAML注入在XAML依赖性:

<!-- Resolve an instance based on the type of property 'SomeValueToBrushConverter' -->
<MyConverter SomeValueToBrushConverter="{services:IocResolver}" />

<!-- Resolve a named instance based on the type of property 'SomeValueToBrushConverter' and the name 'MyName' -->
<MyConverter SomeValueToBrushConverter="{services:IocResolver  NamedInstance=MyName}" />


文章来源: Using Unity to inject objects into IValueConverter instance