6

我正在将大量代码移至 Castle Trunk,其中包括用于配置容器的新流利界面。由于该项目有一个无法维护的巨大 windsorConfig xml 文件,我想我会开始利用这个新功能。我知道其他容器(例如 StructureMap 2.0)也包含用于容器配置的流畅接口,所以这个问题不是基于 Windsor。

我的问题是,您使用新的流利样式接口进行容器配置时使用了哪些约定/习惯用法/模式?

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

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

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

4

4 回答 4

3

深入了解 StructureMap 2.5。它提供了几个功能来显着减少引导 IOC 容器的工作。它提供了一种配置技术的约定(请参阅下面的博客条目)

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

使用 StructureMap 创建您自己的自动注册约定

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

StructureMap 2.5.2 发布

于 2009-01-22T16:40:22.703 回答
2

我有一个使用 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);
}

并让程序集提供默认的 Configurator 类。我们还使用静态类包装了 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 包装器的文章。

于 2009-02-17T20:24:33.397 回答
1

棘手的问题 [而且我不是 IoC 专家] 但请记住,任何“整体静态函数”都不应该配置文件那样可怕。您可以为事物定义自己的约定,并尝试将事物抽象化。我使用 Ninject,但对于 Windsor,我想这将涉及使用诸如 Register 和 AllTypesOf 策略之类的东西来制作简短的小功能:

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

不知道内部层次结构导出自己的默认配置。这似乎有点可怕和倒置。

于 2009-01-19T18:11:20.850 回答
1

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

忍者

于 2009-02-15T18:10:28.353 回答