Unit.MVC4的懒< T>未在ASP.NET MVC应用程序4工作 [英] Unit.MVC4’s lazy<T> is not working in ASP.NET MVC 4 app

查看:115
本文介绍了Unit.MVC4的懒< T>未在ASP.NET MVC应用程序4工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET MVC应用4。

I am using ASP.NET MVC 4 application.

主页控制器的构造函数进行参数2参数(Iservice1服务1,Iservice2服务2)
不是所有的code路径使用任何服务(服务1,服务2),只在一些code路径我需要服务1实例/对象或服务2实例/对象。的

Home controller’s constructor is parameterized with 2 parameter(Iservice1 service1, Iservice2 service2) Not all the code path uses any of the Service (service1, service2), only in some code path I need service1 instance/object or service2 instance/object.

我不希望使用container.Resolve< <&懒LT; IService1> >();

I don’t want to use container.Resolve<<Lazy<IService1>>();

从这个链接(的http:// MSDN .microsoft.com / EN-US /库/ dn178463(v = pandp.30)的.aspx )我明白了unity.mvc 4使用统一3具有延迟加载的支持,但如何做到这一点的ASP。 NET MVC 4。

From this link (http://msdn.microsoft.com/en-us/library/dn178463(v=pandp.30).aspx) I understood that unity.mvc 4 use unity 3 which has Lazy loading support, but how to do this in ASP.NET MVC 4.

推荐答案

在一般情况下,实例构造函数应该做的无非就是做适当的null检查和存储传入的引用。这使得对象的构造图​​快,可靠。任何初始化应推迟到以后的时刻时,该组件被用于第一次的时间。

In general, instance constructors should do nothing more than do the proper null checks and store the incoming references. This makes construction of object graphs fast and reliable. Any initialization should be postponed to a later moment in time, the time the component is used for the first time.

这将在 - 大多数情况下─prevent需要从延迟组件的创作性能,如果这确实仍然是一个问题,你可能要考虑,有一个容器的更好的性能

This will in -most cases- prevent the need from delaying creation of a component for performance and if this really still is a problem, you might want to consider a container that has better performance.

对于您需要延迟创作的闲暇时刻,不使用懒&LT; T&GT; 这一点。注射懒&LT; T&GT; 是一个漏水的抽象,就像的IDisposable 是,当放在接口。注射懒&LT; T&GT; 泄漏,因为从理论上讲,每一个依赖可能会很慢或可能需要推迟。对于有介绍较慢的实施服务时,使整个应用程序的巨大变化prevent,我们最好让应用程序中的每一个相关性的懒&LT; IDependency&GT; 前面,因为这让我们可以不用,使以后的变化。

For those spare moments that you need to delay the creation, don't use a Lazy<T> for this. Injecting Lazy<T> is a leaky abstraction, just as IDisposable is when placed on interfaces. Injecting a Lazy<T> leaks, because in theory, every dependency could be slow or could need to be postponed. To prevent having to make sweeping changes throughout the applications when a slower implementation of a service is introduced, we'd better make every dependency in the application a Lazy<IDependency> up front, because this saves us from having to make changes later on.

但是,这当然是愚蠢和丑陋。但是,即使该应用程序是足够小,这样的巨大变化是可以承受的,为什么要消费者知道或关心的是服务需要延迟初始化的事实呢?这不就是一个实现细节?为什么我们这个烘烤偷懒行为,这个服务的合同吗?这样做使我们的code和我们需要编写更复杂的测试。这是不必要的偶然复杂

But that's of course silly and ugly. But even when the application is small enough that such sweeping change is affordable, why should the consumer know or care about the fact that that service needs lazy initialization? Isn't that an implementation detail? Why are we baking this lazy behavior into the contract of this service? Doing so makes our code and the tests we need to write more complicated. That's unneeded accidental complexity.

因此​​,而不是注入懒&LT; IService1&GT; ,您应该简单地注入了 IService1 并实现并注册代理实现偷懒行为。这实际上是很容易做到如下:

So instead of injecting a Lazy<IService1>, you should simply inject an IService1 and implement and register a proxy that implements lazy behavior. That's actually really easy to do as follows:

public class LazyService1Proxy : IService1
{
    private Lazy<IService1> service;

    public LazyService1Proxy(Lazy<IService1> service) {
        this.service = service;
    }

    void IService1.Method1() {
        this.service.Value.Method1();
    }

    object IService1.Method2(string foo) {
        return this.service.Value.Method2(foo);
    }
}

此代理可以注册如下:

container.Register<IService1>(new InjectionFactory(c => 
    new LazyService1Proxy(
        new Lazy<IService1>(
            () => c.Resolve<RealService1Impl>()))));

这篇关于Unit.MVC4的懒&LT; T&GT;未在ASP.NET MVC应用程序4工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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