您使用新的 Fluent 接口配置 IOC 容器时使用哪些约定/惯用语/模式

发布于 2024-07-11 08:41:18 字数 482 浏览 11 评论 0 原文

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

我的问题是,您使用新的流畅样式界面进行容器配置时使用什么约定/惯用语/模式?

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

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

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

I am in the middle of moving over a large body of code to Castle Trunk which includes the new fluent interface for configuring the container. Since the project has a huge windsorConfig xml file that is beyond maintainable, I thought I would start to take advantage of this new feature. I know other containers (e.g. StructureMap 2.0) also contain fluent interfaces for container configuration, so this question isn't based around Windsor.

My question is what conventions/idioms/patterns are you using for container configuration using the new fluent style interfaces?

My first thought was to create a static method somewhere (e.g. ContainerConfig.Config) that would load all the relevant types that the app uses into the container. My worry is eventually this monolithic function would end up being almost as unmaintainable as the xml config file (less the angle bracket tax).

My second thought was to break it down so each dependent assembly, by convention exports its default configuration. I can see this been useful for hierarchies used internally by the assembly. But for types used externally should there configuration even be defined internally?

The more I thought about it the more questions I seemed to raise. What are your thoughts on it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

上课铃就是安魂曲 2024-07-18 08:41:20

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

Ninject

You could try examining the Ninject framework. Very simple, fluent interface and lightning-fast ;) No XML configuration and the API is quite simple. Highly recommended

Ninject

木緿 2024-07-18 08:41:18

更深入地了解 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 已发布

Take a deeper look at StructureMap 2.5. It offers several features to dramatically reduce the work to bootstrap the IOC container. It offers a convention over configuration technique (see the blog entries below)

See the following recent blog posts from Jeremy Miller (author of 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

你的往事 2024-07-18 08:41:18

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

并让程序集提供默认的配置器类。 以便我们可以调用 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.
    }

我们还使用静态类包装了 Unity IoC , 或者我刚刚调用的网站:

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

在 BLL 中有一个这样的类:

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

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

更新:自从这个答案之后,我在我的博客上发布了一篇文章,其中包含Unity 包装器

I had a project where we were using Unity, and I watched a video about StructureMap and I liked the registration idea from the start.

So I created the following interface:

/// <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);
}

And have assemblies offer a default Configurator class. We've also wrapped our Unity IoC using a static class so that we can call IoC.Resolve<T>, and I just added the following functions to that wrapper:

    /// <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.
    }

So in the initialization form either the program or the website I'd just call:

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

In the BLL there is a class like this:

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

The only downside is that all you're layers are coupled to the chosen IoC container.

Update: Since this answer I have posted an article on my blog containing the Unity wrapper.

小矜持 2024-07-18 08:41:18

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

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

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

Tricky questions [and I'm no IoC expert] but keep in mind that any "monolithic static function" should not be nearly as scary as the config file. You can define your own conventions for things, and try to abstract things down. I use Ninject, but for Windsor, I'd imagine it would involve making short little functions using things like Register with the AllTypesOf strategy:

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

Don't know about the internal hierarchies exporting their own default configuration. That seems a little scary and inverted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文