代码之家  ›  专栏  ›  技术社区  ›  Nic Strong

您使用什么约定/习惯用法/模式使用新的Fluent接口配置IOC容器

  •  6
  • Nic Strong  · 技术社区  · 17 年前

    我的问题是,对于使用新的fluent样式接口的容器配置,您使用了哪些约定/习惯用法/模式?

    我的第一个想法是在某个地方创建一个静态方法(例如ContainerConfig.Config),将应用程序使用的所有相关类型加载到容器中。我担心的是,最终这个单一函数会像xml配置文件一样无法维护(减去角度括号税)。

    我的第二个想法是将其分解,以便按照惯例,每个依赖程序集导出其默认配置。我可以看出这对于程序集内部使用的层次结构很有用。但是对于外部使用的类型,是否应该在内部定义配置?

    我想得越多,提出的问题就越多。你对此有什么想法?

    4 回复  |  直到 17 年前
        1
  •  3
  •   JacquesB    17 年前

    深入了解StructureMap 2.5。它提供了几个特性,可以显著减少引导IOC容器的工作量。它提供了一种优于配置技术的约定(参见下面的博客条目)

    请参阅Jeremy Miller(StructureMap的作者)最近发表的以下博客文章

    Create your own Auto Registration Convention with StructureMap

            // Example from the blog post above
            var container = new Container(registry =>
            {
                registry.Scan(x =>
                {
                    x.TheCallingAssembly();
                    x.With<DefaultConventionScanner>();
                });
            });
    

    StructureMap 2.5.2 is Released

        2
  •  2
  •   Davy Landman    17 年前

    我有一个使用Unity的项目,我看了一个关于StructureMap的视频,我从一开始就喜欢注册的想法。

    /// <summary>
    /// An interface which must be implemented to create a configurator class for the UnityContainer.
    /// </summary>
    public interface IUnityContainerConfigurator
    {
        /// <summary>
        /// This method will be called to actually configure the container.
        /// </summary>
        /// <param name="destination">The container to configure.</param>
        void Configure(IUnityContainer destination);
    }
    

    并让程序集提供默认的配置器类。我们还使用静态类包装了Unity IoC,以便调用 IoC.Resolve<T> ,我刚刚向该包装器添加了以下函数:

        /// <summary>
        /// Configure the IoC
        /// </summary>
        public static class Configure
        {
            /// <summary>
            /// Configure the IoC using by calling the supplied configurator.
            /// </summary>
            /// <typeparam name="TConfigurator">The configurator to use</typeparam>
            public static void From<TConfigurator>() where TConfigurator : IUnityContainerConfigurator, new()
            {
                From(new TConfigurator());
            }
            /// <summary>
            /// Configure the IoC using by calling the supplied configurator.
            /// </summary>
            /// <param name="configurationInterface">The configurator instance to use</param>
            public static void From(IUnityContainerConfigurator configurationInterface)
            {
                configurationInterface.Configure(instance);
            }
            // other configuration.
        }
    

    因此,在初始化表单中,无论是程序还是网站,我都会调用:

    IoC.Configure.From<BLL.DefaultMapping>();
    

    在BLL中有这样一个类:

    public class DefaultMapping:IUnityContainerConfigurator
    {
        public void Configure(IUnityContainer destination)
        {
            destionation.RegisterType<IRepository, SQLRepository>();
            // and more..
        }
    }
    

    唯一的缺点是所有层都耦合到所选的IoC容器。

    使现代化 :自从回答这个问题后,我在我的博客上发表了一篇文章,其中包含 Unity wrapper .

        3
  •  1
  •   Dan Fitch    17 年前

    棘手的问题[我不是国际奥委会专家],但请记住,任何“单一静态函数”都不应该被忽略 几乎

    kernel.Register(AllTypesOf<ISomethingProvider>.
        FromAssembly(Assembly.Load("SomeAssembly")));
    

    不知道内部层次结构如何导出自己的默认配置。这似乎有点吓人,而且有点颠倒。

        4
  •  1
  •   Jeffrey Cameron    17 年前

    您可以尝试检查Ninject框架。非常简单,界面流畅,速度极快;)没有XML配置,API非常简单。强烈推荐

    Ninject