Skip to content

其它注入说明

注入所有实现

如果需要注入所有的实现,可以通过数组或List接收,容器会将此类型的所有实现都汇总后注入

C#
public class UserController
{
    [AutoAssemble]
    private IEnumerable<IUserService> services; 

    public UserController()
    {
    }

    public void Print()
    {
        foreach (var service in services)
        {
            Console.WriteLine("Hello " + service?.GetUserName());
        }
    }
}

public interface IUserService
{
    string GetUserName();
}

[Service(1)]
public class UserService1 : IUserService
{
    public string GetUserName() => "Jack";
}

[Service(2)]
public class UserService2 : IUserService
{
    public string GetUserName() => "Lily";

}

此时,调用UserController的Print方法时,会输出:

sh
> Hello Jack
> Hello Lily

注意

注入所有实现,其类型可以是数组、List<>、IList<>、IEnumerable<>、ICollection<>等,不支持自定义可迭代类型

泛型的注入

泛型是比较特殊的类型,容器会根据需要找到最适合的泛型类型

一般情况

C#
[Service]
public class UserController
{
    [AutoAssemble]
    private IFormatComponent<int> format;

    public UserController()
    {
    }

    public void Print()
    {
       Console.WriteLine(format.Format(23));
    }
}
public interface IFormatComponent<T> where T : new()
{
    string Format(T value);
}

[Service]
public class IntFormat : IFormatComponent<int>
{
    public string Format(int value)
    {
        return value.ToString("d4");
    }
}

[Service]
public class DateTimeFormat : IFormatComponent<DateTime>
{
    public string Format(DateTime value)
    {
        return value.ToString("yyyy-MM-dd HH:mm:ss.fff");
    }
}

此时,调用UserController的Print方法时,会输出:

sh
> 0023

多泛型参数情况

C#
[Service]
public class UserController
{
    [AutoAssemble]
    private IConcatComponent<string, string, string> common;

    [AutoAssemble]
    private IConcatComponent<int, string, string> firstInt;

    [AutoAssemble]
    private IConcatComponent<string, int, string> secondInt;

    [AutoAssemble]
    private IConcatComponent<int, int, string> twoInt;

    public UserController()
    {
    }

    public void Print()
    {
        Console.WriteLine(common.Concat("s1", "s2", "s3"));
        Console.WriteLine(firstInt.Concat(1, "s2", "s3"));
        Console.WriteLine(secondInt.Concat("s1", 1, "s3"));
        Console.WriteLine(twoInt.Concat(1, 2, "s3"));
    }
}
public interface IConcatComponent<T1, T2, T3>
{
    string Concat(T1 t1, T2 t2, T3 t3);
}

[Service]
public class CommonConcat<T1, T2, T3> : IConcatComponent<T1, T2, T3>
{
    public string Concat(T1 t1, T2 t2, T3 t3)
    {
        return "Common :" + t1?.ToString() + "," + t2?.ToString() + "," + t3?.ToString();
    }
}

[Service]
public class FirstInt<T2, T3> : IConcatComponent<int, T2, T3>
{
    public string Concat(int t1, T2 t2, T3 t3)
    {
        return "FirstInt :" + t1 + "," + t2?.ToString() + "," + t3?.ToString();
    }
}

[Service]
public class SecondInt<T1, T3> : IConcatComponent<T1, int, T3>
{
    public string Concat(T1 t1, int t2, T3 t3)
    {
        return "SecondInt :" + t1?.ToString() + "," + t2 + "," + t3?.ToString();
    }
}

[Service]
public class TwoInt<T3> : IConcatComponent<int, int, T3>
{
    public string Concat(int t1, int t2, T3 t3)
    {
        return "TwoInt :" + t1 + "," + t2 + "," + t3?.ToString();
    }
}

此时,调用UserController的Print方法时,会输出:

sh
> Common :s1,s2,s3
> FirstInt :1,s2,s3
> SecondInt :s1,1,s3
> TwoInt :1,2,s3

注意 : 以上都是在各个服务版本相同的情况下,如果有更高的版本,只要可以实例化,则会以高版本注入,例如,将CommonConcat的版本提高

C#
[Service(1)]
public class CommonConcat<T1, T2, T3> : IConcatComponent<T1, T2, T3>
{
    public string Concat(T1 t1, T2 t2, T3 t3)
    {
        return "Common :" + t1?.ToString() + "," + t2?.ToString() + "," + t3?.ToString();
    }
}

此时,调用UserController的Print方法时,会输出:

sh
> Common :s1,s2,s3
> Common :1,s2,s3
> Common :s1,1,s3
> Common :1,2,s3

沪ICP备2025119739号