Unity 到结构图帮助

发布于 2024-09-15 11:58:25 字数 1524 浏览 8 评论 0原文

我正在尝试将 DI 映射中的代码从 Unity 转换为 Structuremap,但我似乎无法让它工作。我使用的存储库模式类似于 Kazi Manzur Ra​​shid 在收缩器中找到的存储库模式,在这里找到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晚风撩人 2024-09-22 11:58:25

我不了解 Unity,但我猜您正在尝试请求 IRepository 返回 ProductRepository。

在 StructureMap 中,代码更简单一些:

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

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:

var container = new Container(x => {
  x.Scan(scan =>
  {
      scan.TheCallingAssembly();
      scan.ConnectImplementationsToTypesClosing(typeof(IRepository<>));
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文