2014-12-03 35 views
0

我使用AutoFac和使用AM以下线在我的注册AutoFac决心界面键注册

两个不同clases实现具有两个不同的密钥相同的接口..

_builder.RegisterType<CopyAppDataUserBudgetLine>().Keyed<ICopyAppData>(EntityType.UserBudgetLine).As<ICopyAppData>().InstancePerDependency(); 
_builder.RegisterType<CopyAppDataBudgetLine>().Keyed<ICopyAppData>(EntityType.BudgetLine).As<ICopyAppData>().InstancePerDependency(); 

同一类注册两个不同的密钥

_builder.RegisterType<RemoveOldAppData>().AsImplementedInterfaces().Keyed<IRemoveOldData>(EntityType.UserBudgetLine).InstancePerDependency(); 
_builder.RegisterType<RemoveOldAppData>().AsImplementedInterfaces().Keyed<IRemoveOldData>(EntityType.BudgetLine).InstancePerDependency(); 

类定义

public class RemoveOldAppData : RemoveAppDataBase, IRemoveOldData 
    { 
     public RemoveOldAppData(KonstruktEntities context, 
      ISQLQueryWhereClauseHelper sqlQueryWhereClauseHelper, 
      IQueryExecutionHelper queryExecutionHelper) : base(context,sqlQueryWhereClauseHelper,queryExecutionHelper) { } 
     public void RemoveBudgetLines(EntityType entityType, AccessEngine.LineAccessFilter filter) 
     { 
      ... 
     } 
    } 

public class CopyAppDataBudgetLine : CopyAppDataBase , ICopyAppData 
    { 
     public CopyAppDataBudgetLine(KonstruktEntities context, 
      ISQLQueryWhereClauseHelper sqlWhereClauseHelper, 
      IQueryExecutionHelper queryExecutionHelper, 
      ITableColumns columns) : base(context,sqlWhereClauseHelper,queryExecutionHelper,columns) { } 

     public void CopyData(string receivingUserId, AccessEngine.LineAccessFilter queryFilter) 
     { 
      ... 
     } 
     public EntityType CopyDataEntityType 
     { 
      get; 
      set; 
     } 
    } 

这里是我如何努力解决这些

using (var scope = _container.BeginLifetimeScope()) 
{ 
    var copyAppDataUserBudgetLine = scope.Resolve<ICopyAppData>(); 
    copyAppDataUserBudgetLine.CopyData("leif.andersson", filterAcccess); 

问题:如何解决上述采取特定的EntityType(UserBudgetLine),做我这班上别的地方或构造?

编辑:我得到了它使用的工作如下:

var copyAppDataUserBudgetLine = scope.ResolveKeyed<ICopyAppData>(EntityType.UserBudgetLine); 

这是正确的做法,或者我应该去了解这种不同?

回答

0

我假设EntityType是一些枚举,你键入的类?

当我解析键控接口时,我更喜欢使用自定义工厂,它在构造函数中接受来自Autofac的IIndex。

Autofac给我解决了这个工厂Autofac将基本上为我提供一个我的类型和他们的键的字典。

我会做类似下面,我已经从假你的榜样编码实现这个...

public Enum EntityType 
{ 
    UserBudgetLine, 
    BudgetLine 
} 

public class EntityCopyAppDataFactory 
{ 
    private readonly IIndex<EntityType, ICopyAppData> _copyAppDataIndex; 

    public EntityCopyAppDataFactory(IIndex<EntityType, ICopyAppData> copyAppDataIndex) 
    { 
     _copyAppDataIndex = copyAppDataIndex; 
    } 

    public ICopyAppData GetCopyAppData(EntityType entityType) 
    { 
     return _copyAppDataIndex[entityType]; 
    } 
} 

然后在Autofac

builder.RegisterType<EntityCopyAppDataFactory>(); 

注册这个工厂,然后我会问对于我的Autofac工厂来说,并且使用EntityType.UserBudgetLine调用GetCopyAppData,我需要的范围如下,或者通过堆栈顶部的Constructor Injection。

using (var scope = _container.BeginLifetimeScope()) 
{ 
    var myFactory = scope.Resolve<EntityCopyAppDataFactory>(); 
    var copyAppDataUserBudgetLine = myFactory.GetCopyAppData(EntityType.UserBudgetLine); 
} 

我希望这能给你一些思考另一个方向的食物而不是调用ResolveKeyed?

+0

谢谢。您是否在所有情况下使用这些类型的定制工厂,或者仅在使用键控注册时使用这些类型的定制工厂? – JohanLarsson 2014-12-06 09:31:38

+0

我使用这个自定义工厂概念,随时与Keyed注册工作。你也可以使用Func <>的autofac注册来完成相同的工作,但我更喜欢为我的工厂使用单独的类来保持代码清洁和分离。 – jonhoare 2014-12-08 07:55:48

+0

这里是使用IIndex和一个很好的描述的另一个例子http://nblumhardt.com/2010/08/the-iindexkv-relationship-type/ – jonhoare 2014-12-08 07:56:27

0

这就是我最终做的。这是做到这一点的一种方式,但我希望能够始终为所有类型的接口使用 scope.Resolve,并且不会被迫为这些类型的场景执行scope.ResolveKeyed

var copyAppDataUserBudgetLine = scope.ResolveKeyed<ICopyAppData>(EntityType.UserBudgetLine);