0

我正在注册要为接口创建的实现。此实现构造函数具有三个参数:

public ProfileImageService(ISqlConnection connection, string localStorageLocation, string serverPath)

我已经像这样注册了 ISqlConnection:

c.Register<Data.ISqlConnection, Data.TsqlConnection>(setup: Setup.With(openResolutionScope: true));

但是,当我像这样注册 ProfileImageService 时:

c.Register<Data.Services.Profile.IImageService, Data.Services.Profile.ProfileImageService>(Made.Of(() => new Data.Services.Profile.ProfileImageService(Arg.Index<Data.ISqlConnection>(0), Arg.Index<string>(1), Arg.Index<string>(2)), requestIgnored =>c.Resolve<Data.ISqlConnection>(), requestIgnored => localImageStoragePath, requestIgnored => localImageUrl));

我收到以下错误:

使用(可能注入)项 Abc.Data.TsqlConnection 需要 Message=State。要启用项目使用,您可以指定 container.With(rules => rules.WithItemToExpressionConverter(YOUR_ITEM_TO_EXPRESSION_DELEGATE))。

我已阅读此SpecifyDependencyAndPrimitiveValues并尝试像他们一样对其进行修改,但他们的示例仅显示了原语或依赖项,而不是两者,而且我找不到它们在没有编译错误的情况下混合的语法。谁能给我一些关于如何注册这个实例的指导,指定两个字符串的值和依赖项的值?

4

1 回答 1

2

这是你如何做到的(使用最新的 DryIoc v2.10.6):你不需要c.Resolve<ISqlConnection>()在参数中指定,只需使用Arg.Of<ISqlConnection>()

using System;
using DryIoc;

public class Program
{
    public static void Main()
    {
        var c = new Container();

        var localImageStoragePath = "a";
        var localImageUrl = "b";

        c.Register<ISqlConnection, TsqlConnection>();
        c.Register<IImageService, ProfileImageService>(
            Made.Of(() => new ProfileImageService(
                Arg.Of<ISqlConnection>(), 
                Arg.Index<string>(0), 
                Arg.Index<string>(1)), 
                _ => localImageStoragePath, 
                _ => localImageUrl));

        var imgService = c.Resolve<IImageService>();

        Console.WriteLine(imgService);
    }

    public interface IImageService {}

    public class ProfileImageService: IImageService
    {
        public ProfileImageService(ISqlConnection conn, string a, string b) {}

    }

    public interface ISqlConnection {}

    public class TsqlConnection : ISqlConnection {} 
}

或者更好的是,如果您的字符串参数不应该更改:

    c.Register<IImageService, ProfileImageService>(
        Made.Of(() => new ProfileImageService(
            Arg.Of<ISqlConnection>(), 
            localImageStoragePath, 
            localImageUrl)));

这是现场证明

于 2017-05-03T09:46:38.623 回答