我正在尝试将我的DI映射中的代码从Unity转换为Structuremap,但我似乎无法让它工作。我使用的存储库模式就像在Kazi Manzur Rashid发现的收缩器中找到的那样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());
}
}
}
答案 0 :(得分:0)
我不认识Unity,但我猜你正试图让IRepository请求返回ProductRepository。
在StructureMap中,代码更简单:
var container = new Container(x => {
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.ConnectImplementationsToTypesClosing(typeof(IRepository<>));
});
});