Unity to Structuremap帮助

时间:2010-08-23 19:38:02

标签: structuremap unity-container

我正在尝试将我的DI映射中的代码从Unity转换为Structuremap,但我似乎无法让它工作。我使用的存储库模式就像在Kazi Manzur Ra​​shid发现的收缩器中找到的那样http://shrinkr.codeplex.com/任何帮助都将不胜感激!

Unity代码:

....

private static readonly Func<LifetimeManager> perRequest = () => new PerRequestLifetimeManager();

....

IBuildManager buildManager = container.Resolve<IBuildManager>();         RegisterRepositories(buildManager, container);

....

private static void RegisterRepositories(IBuildManager buildManager, IUnityContainer container)
        {
            Type genericRepositoryType = typeof(IRepository<>);

            IEnumerable<Type> repositoryContractTypes = buildManager.PublicTypes.Where(type => (type != null) && type.IsInterface && type.GetInterfaces().Any(interfaceType => interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition().Equals(genericRepositoryType))).ToList();

            foreach (Type repositoryImplementationType in buildManager.ConcreteTypes.Where(implementationType => repositoryContractTypes.Any(contractType => contractType.IsAssignableFrom(implementationType))))
            {
                foreach (Type repositoryInterfaceType in repositoryImplementationType.GetInterfaces())
                {
                    container.RegisterType(repositoryInterfaceType, repositoryImplementationType, perRequest());
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

我不认识Unity,但我猜你正试图让IRepository请求返回ProductRepository。

在StructureMap中,代码更简单:

var container = new Container(x => {
  x.Scan(scan =>
  {
      scan.TheCallingAssembly();
      scan.ConnectImplementationsToTypesClosing(typeof(IRepository<>));
    });
});