在SimpleInjector中用构造函数注册泛型类型怎么办? [英] Register generic types with constructor in SimpleInjector how?

查看:99
本文介绍了在SimpleInjector中用构造函数注册泛型类型怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Microsoft Unity用作IoC容器,并编写了一些类似这样的代码:

I use Microsoft Unity as IoC container and wrote some code like this:

public static void RegisterTypes(IUnityContainer container)
{
    MyContext ctx = new MyContext (); // is EntityFramework DbContext
    container.RegisterType(typeof(IEntityRepository<>),
                           typeof(EntityRepository<>), 
                           new InjectionConstructor(ctx));
    container.RegisterType(typeof(IEntityService<>), 
                           typeof(EntityService<>));
}

它对我来说毫无问题,但是我想知道如何在 SimpleInjector 中编写其等效内容.

It works for me without problem, but I want to know how to write its equivalent in SimpleInjector.

推荐答案

您的配置没有什么特别的,@ Patryk会为您提供正确的文档

There's nothing special about your configuration and @Patryk pointed you at the correct documentation here. Simple Injector will do the auto-wiring for you, so you don't have to specify constructor arguments explicitly.

但是您当前的设置有些令人讨厌,因为您的DbContext似乎是单身.在整个应用程序期间,您只有一个实例.在几乎所有情况下,这都是非常糟糕的主意.您的应用程序可以生存很长时间(例如Windows服务),也可以并发处理许多线程(例如ASP.NET和WCF).仅在您的单线程应用程序寿命很短的情况下(例如控制台应用程序),您才可能只有一个DbContext.您不想将DbContext注册为单例.

But there's something smelly about your current setup, because it seems like your DbContext is a singleton; you only have one instance for the duration of your whole application. This is in almost all cases a very bad idea. Either your application lives for a long time (such as a Windows Service) or handles many threads concurrently (such as ASP.NET and WCF). Only in the case where you have a single-threaded application that lives for a very short time (such as a console application), you might have just one DbContext. You don't want to register your DbContext as a singleton.

但是也许不是单例,实际上您每个请求调用一次RegisterTypes.在这种情况下,每个请求将有一个DbContext,但这意味着您将为每个请求创建一个新的容器实例,这也很糟糕.这对于Unity和Simple Injector的性能都非常不利. DI库经过特别优化,可以在应用程序运行期间拥有一个容器实例.

But perhaps it is not a singleton and you actually call this RegisterTypes once per request. In this case you will have one DbContext per request, but this means you create a new container instance each request and that will be bad as well. That will be very bad for performance with both Unity and Simple Injector. DI libraries are especially optimized for having one single container instance for the duration of the application.

因此,您宁愿在应用程序使用期间拥有一个容器实例,并且通常将DbContext注册到Scoped生活方式中.

So you rather want to have one single container instance for the duration of your application and have your DbContext usually registered with a Scoped lifestyle.

在简单注入器中,有许多有范围的生活方式,每个都是特定于所选技术.假设您使用的是MVC,则您的注册可能如下所示:

In Simple Injector there are many scoped lifestyles, each one is specific for the chosen technology. Assuming you are using MVC, your registration might look like this:

public static void RegisterTypes(Container container)
{
    container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();

    container.Register<MyContex>(Lifestyle.Scoped);

    container.Register(typeof(IEntityRepository<>), typeof(EntityRepository<>));

    container.Register(typeof(IEntityService<>), typeof(EntityService<>));
}

注意EntityRepository<T>的注册.如果EntityRepository<T>的构造函数参数为MyContext,并且MyContext已在容器中注册,则Simple Injector将能够在其构造函数中为您注入此依赖项.此过程称为自动装配,通常应让容器为您完成此操作,因为这将使您的DI配置更具可维护性.

Do note the registration for EntityRepository<T>. If EntityRepository<T> has a constructor argument of MyContext, and MyContext is registered in the container, Simple Injector will be able to inject this dependency for you in its constructor. This process is called auto-wiring and you should normally let the container do this for you, because this makes your DI configuration much more maintainable.

这篇关于在SimpleInjector中用构造函数注册泛型类型怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆