.net 您使用什么约定/习惯用法/模式使用新的Fluent接口配置IOC容器 我在一个大的代码体上移动到CaseBar,它包含了用于配置容器的新的FLUENT接口。由于该项目有一个巨大的windsorConfig xml文件,无法维护,我想我应该开始利用这个新功能。我知道其他容器(例如StructureMap 2.0)也包含用于容器配置的流畅接口,所以这个问题不是基于Windsor

.net 您使用什么约定/习惯用法/模式使用新的Fluent接口配置IOC容器 我在一个大的代码体上移动到CaseBar,它包含了用于配置容器的新的FLUENT接口。由于该项目有一个巨大的windsorConfig xml文件,无法维护,我想我应该开始利用这个新功能。我知道其他容器(例如StructureMap 2.0)也包含用于容器配置的流畅接口,所以这个问题不是基于Windsor,.net,inversion-of-control,structuremap,fluent-interface,.net,Inversion Of Control,Structuremap,Fluent Interface,我的问题是,对于使用新的fluent样式接口的容器配置,您使用了哪些约定/习惯用法/模式 我的第一个想法是在某个地方创建一个静态方法(例如ContainerConfig.Config),将应用程序使用的所有相关类型加载到容器中。我担心的是,最终这个单一函数会像xml配置文件一样无法维护(减去角度括号税) 我的第二个想法是将其分解,以便按照惯例,每个依赖程序集导出其默认配置。我可以看出这对于程序集内部使用的层次结构很有用。但是对于外部使用的类型,是否应该在内部定义配置 我想得越多,提出的问题就越多

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

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

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


我想得越多,提出的问题就越多。您对此有何想法?

棘手的问题[我不是国际奥委会专家],但请记住,任何“单一静态函数”都不应该像配置文件那样可怕。你可以为事物定义自己的约定,并尝试将事物抽象下来。我使用Ninject,但对于Windsor,我认为它将涉及使用诸如Register with the AllTypesOf strategy之类的东西来生成简短的小函数:

kernel.Register(AllTypesOf<ISomethingProvider>.
    FromAssembly(Assembly.Load("SomeAssembly")));
kernel.Register(所有类型)。
FromAssembly(Assembly.Load(“SomeAssembly”));

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

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

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

//上述博客文章中的示例
var容器=新容器(注册表=>
{
registry.Scan(x=>
{
x、 装配件();
x、 带();
});
});

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


我有一个使用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);
}
//
///为UnityContainer创建configurator类时必须实现的接口。
/// 
公共接口IUnityContainerConfiguration
{
/// 
///将调用此方法来实际配置容器。
/// 
///要配置的容器。
无效配置(IUnityContainer目的地);
}
并让程序集提供默认的配置器类。我们还使用一个静态类包装了Unity IoC,这样我们就可以调用
IoC.Resolve
,我刚刚在包装中添加了以下函数:

    /// <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
/// 
公共静态类配置
{
/// 
///通过调用提供的配置程序,使用配置IoC。
/// 
///要使用的配置程序
public static void From(),其中TConfigurator:IUnityContainerConfigurator,new()
{
来自(新的TConfigurator());
}
/// 
///通过调用提供的配置程序,使用配置IoC。
/// 
///要使用的configurator实例
来自(IUnityContainerConfigurator配置界面)的公共静态无效
{
configurationInterface.Configure(实例);
}
//其他配置。
}
因此,在初始化表单中,无论是程序还是网站,我都会调用:

IoC.Configure.From<BLL.DefaultMapping>();
IoC.Configure.From();
在BLL中有这样一个类:

public class DefaultMapping:IUnityContainerConfigurator
{
    public void Configure(IUnityContainer destination)
    {
        destionation.RegisterType<IRepository, SQLRepository>();
        // and more..
    }
}
公共类默认映射:IUnityContainerConfiguration
{
公共无效配置(IUnityContainer目标)
{
destination.RegisterType();
//还有更多。。
}
}
唯一的缺点是所有层都耦合到所选的IoC容器

更新:自从回答这个问题后,我在我的博客上发表了一篇文章,其中包含

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