我如何可以模拟模块/安装/注册与简单的喷油器喷油器、模块、简单

2023-09-04 00:40:10 作者:忘却成全

Autofac具有模块,温莎有安装和StructureMap登记处...用简单的喷油器我怎么能打包配置逻辑成可重用的类?

我曾尝试:

 公共接口的IModule {}

公共类FooModule:IModule的
{
    公共FooModule(SimpleInjector.Container容器)
    {
        container.RegisterSingleton<动物资源局,酒吧及GT;();
        container.RegisterSingleton<的IFoo,富>();
    }
}
 

和我的作文根使用它:

 公共静态无效的主要(字串[] args)
{
    VAR集装箱=​​新SimpleInjector.Container();
    container.RegisterCollection&其中; IModule的>(新FooModule(容器));
    ...
}
 

不过, FooModule 依赖于容器中,也许在不是一个好的做法? 看到 HTTP://$c$c.google.com/p/autofac/维基/ BestPractices :

  西门子plc1200的模拟输入模块怎么使用

如果组件具有在容器上的依赖,看看他们是如何使用的   容器检索服务,而这些服务组件(依赖性增加   注入)构造函数的参数来代替。

解决方案

一个模块功能是故意留出的简单喷油器的核心库,但有一个的 SimpleInjector.Packaging的NuGet包,可以让你做到这一点。 '包'这个名词简单的进样器使用。这个库然而,无非是一个 IPackage 接口和两个扩展方法。您可以通过编写code这样实现相同的:

一个包:

公共静态类BootstrapperPackage {     公共静态无效RegisterServices(集装箱货柜)     {         container.Register<动物资源局,酒吧及GT;(Lifestyle.Scoped);         container.Register<的IFoo,富>(Lifestyle.Singleton);     } }

在你的作文根:

公共静态无效的主要(字串[] args) {     VAR集装箱=​​新SimpleInjector.Container();     BootstrapperPackage.RegisterServices(容器);     ... }

与 SimpleInjector.Packaging的NuGet包区别是,这个包定义的接口,让,并允许你动态地在一个单独的一行加载多个包:

 公共类BusinessLayerPackage:IPackage
{
    公共RegisterServices(集装箱货柜)
    {
        container.Register<动物资源局,酒吧及GT;(Lifestyle.Scoped);
        container.Register<的IFoo,富>(Lifestyle.Singleton);
    }
}

公共静态无效的主要(字串[] args)
{
    VAR集装箱=​​新SimpleInjector.Container();

    container.RegisterPackages(AppDomain.CurrentDomain.GetAssemblies());
}
 

不过,如果你并不真正需要动态加载,使用静态方法(如上图所示)会做得很好。

Autofac has modules, Windsor has Installers and StructureMap Registries ... with Simple Injector how can I pack configuration logic into reusable classes?

I have tried:

public interface IModule { }

public class FooModule : IModule
{
    public FooModule(SimpleInjector.Container container)
    {
        container.RegisterSingleton<IBar, Bar>();
        container.RegisterSingleton<IFoo, Foo>();
    }
}

And I use it in the Composition Root:

public static void Main(string[] args)
{
    var container = new SimpleInjector.Container();
    container.RegisterCollection<IModule>(new FooModule(container));
    ...
}

However, FooModule depends on container and maybe in not a good practice... see http://code.google.com/p/autofac/wiki/BestPractices:

If components have a dependency on the container, look at how they're using the container to retrieve services, and add those services to the component's (dependency injected) constructor arguments instead.

解决方案

A 'module' feature is deliberately left out of the Simple Injector core library, but there is a SimpleInjector.Packaging NuGet package that allows you to do this. 'Package' is the term Simple Injector uses. This library however, is nothing more than one IPackage interface and two extension methods. You can achieve the same by writing code like this:

A package:

public static class BootstrapperPackage
{
    public static void RegisterServices(Container container)
    {
        container.Register<IBar, Bar>(Lifestyle.Scoped);
        container.Register<IFoo, Foo>(Lifestyle.Singleton);            
    }
}

In your composition root:

public static void Main(string[] args)
{
    var container = new SimpleInjector.Container();

    BootstrapperPackage.RegisterServices(container);

    ...
}

The difference with the SimpleInjector.Packaging NuGet package is that this package defines an interface for you, and allows you to dynamically load multiple packages in one single line:

public class BusinessLayerPackage : IPackage
{
    public RegisterServices(Container container)
    {
        container.Register<IBar, Bar>(Lifestyle.Scoped);
        container.Register<IFoo, Foo>(Lifestyle.Singleton);            
    }
}

public static void Main(string[] args)
{
    var container = new SimpleInjector.Container();

    container.RegisterPackages(AppDomain.CurrentDomain.GetAssemblies());
}

However, if you don't really need dynamic loading, using static methods (as shown above) will do just fine.

 
精彩推荐
图片推荐