1

让我用代码解释我的问题,所以我有一个类:

public class ComplexEntity
{
    private readonly ISomeDependency _someDependency;
    private readonly int _optionalArg;

    public ComplexEntity(ISomeDependency someDependency, int optionalArg)
    {
        _someDependency = someDependency;
        _optionalArg = optionalArg;
    }
}

和模块:

public class FooModule : Module
{
    protected override void OnMap(ContainerBuilder builder)
    {
        builder.RegisterType<ConcreteDependency>().As<ISomeDependency>().SingleInstance();
        builder.RegisterType<ComplexEntity>().AsSelf();//SingleInstance fails my test below. 
    }
}

所以我的问题是 - 我如何ComplexEntity使用可选参数(类型为 int - 实际上它无关紧要)来解决,并且根据该可选参数,它将返回相同的实体(如果已经请求)或创建一个新的实体一 - 看看下面的测试:

    int optionalArgument = 10;
    int anotherOptionalArgument = 11;

    //I expect ResolveOptional returns same references for the same optional argument, 
    //thus instance1 should be equals instance2, but not equals instance3
    var instance1 = _container.ResolveOptional<ComplexEntity>(
        new TypedParameter(optionalArgument.GetType(), optionalArgument));
    var instance2 = _container.ResolveOptional<ComplexEntity>(
        new TypedParameter(optionalArgument.GetType(), optionalArgument));

    var instance3 = _container.ResolveOptional<ComplexEntity>(
        new TypedParameter(anotherOptionalArgument.GetType(), anotherOptionalArgument));

    bool ref12Equals = object.ReferenceEquals(instance1, instance2); //should be true
    bool ref13Equals = object.ReferenceEquals(instance1, instance3); //should be false
    bool ref23Equals = object.ReferenceEquals(instance2, instance3); //should be false
4

1 回答 1

0

在 autofac 范围内可以是单例或工厂(瞬态),而不是两者。如果您需要更复杂的生命周期管理,我相信您需要为此负责并自己管理。如果我理解正确,您需要一个生成新实例或返回单例的范围管理器。如果你仍然想使用 DI,你可以把它放在容器中,像这样:

    private const int FactoryMode = 11;
    private const int SingletonMode = 10;

    public static void Main()
    {
        var containerBuilder = new ContainerBuilder();
        containerBuilder.RegisterType<ComplexTypeScope>().SingleInstance();
        containerBuilder.Register((c, p) => c.Resolve<ComplexTypeScope>().Get(p.TypedAs<int>()));

        var container = containerBuilder.Build();
        ComplexType instance1 = container.Resolve<ComplexType>(new TypedParameter(typeof(int), SingletonMode));
        ComplexType instance2 = container.Resolve<ComplexType>(new TypedParameter(typeof(int), SingletonMode));
        ComplexType instance3 = container.Resolve<ComplexType>(new TypedParameter(typeof(int), FactoryMode));

        Debug.Assert(ReferenceEquals(instance1, instance2));
        Debug.Assert(!ReferenceEquals(instance1, instance3));
    }

    class ComplexType
    {
    }

    class ComplexTypeScope
    {
        private readonly Lazy<ComplexType> complexTypeSingleTon;

        public ComplexTypeScope()
        {
            complexTypeSingleTon = new Lazy<ComplexType>(() => new ComplexType());
        }

        public ComplexType Get(int parameter)
        {
            return parameter == FactoryMode ? new ComplexType() : complexTypeSingleTon.Value;
        }
    }

当然,使用这种方法,容器不会管理组件的生命周期,例如自动处置。

于 2013-05-22T08:57:25.097 回答