Unity 到结构图帮助
我正在尝试将 DI 映射中的代码从 Unity 转换为 Structuremap,但我似乎无法让它工作。我使用的存储库模式类似于 Kazi Manzur Rashid 在收缩器中找到的存储库模式,在这里找到 http://shrinkr.codeplex.com / 任何帮助将不胜感激!
统一代码:
....
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());
}
}
}
I am trying to convert this code in my DI mapping from Unity to Structuremap but I cannot seem to get it to work. I am using Repository pattern like the one in found in shrinkr by Kazi Manzur Rashid found here http://shrinkr.codeplex.com/ Any help would be appreciated!
Unity Code:
....
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());
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不了解 Unity,但我猜您正在尝试请求 IRepository 返回 ProductRepository。
在 StructureMap 中,代码更简单一些:
I don't know Unity, but I'm guessing you are trying to make requests for IRepository return a ProductRepository.
In StructureMap, the code is a bit simpler: