在ASP.NET中使用Unity.WebForms [英] Using Unity.WebForms in ASP.NET

查看:178
本文介绍了在ASP.NET中使用Unity.WebForms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在Webforms项目中实现DI,所以我在我的UI层中安装了Unity.WebForms dll。一旦我使用UnityWebFormsStart类文件为我创建了一个App_Start文件夹。在这个文件里面有一个RegisterDependencies方法要求编辑。

I am trying to implement DI in a webforms project, so I installed the Unity.WebForms dlls in my UI layer. As soon as I did an App_Start folder was created for me with a UnityWebFormsStart class file. Inside this file there is a method RegisterDependencies which asks to be edited.

注册依赖关系后的下一步是什么?有没有什么我需要添加在Global.asax类文件?我如何和在哪里解决Webform中的类型?我是用任何属性来装饰的吗?

What is the next step after registering the dependencies? Is there something I need to add in the Global.asax class file? And how and where do I resolve a type inside a webform? Do I decorate that with any attributes?

推荐答案

Unity.WebForms dll和NuGet包在后台为您做了几件事情。它将确保在每个新的Web请求开始时启动子容器,并在每个请求结束时进行处理。这允许您使用每个Web请求生活方式(使用Unity中的 HierarchicalLifetimeManager )注册组件,这对于诸如O / RM单元的组件(例如实体框架的 DbContext

The Unity.WebForms dll and NuGet package does a few things for you in the background. It will ensure that a child container is started at the begin of each new web request and disposed at the end of each request. This allows you to register components with a 'per web request' lifestyle (using the HierarchicalLifetimeManager in Unity), which is useful for components such as O/RM unit of works such as Entity Framework's DbContext.

该包确定的另一件事是给定的HttpHandler(通常是您的页面)和其所有子控件均为建立。 BuildUp方法是初始化不是由容器本身创建的组件的方法。

The other thing that the package ensures is that the given HttpHandler (usually your Page) and all its child controls are Built up. The BuildUp method is the way to initialize components that are not created by the container itself.

所以想法是在页面类和控件中使用属性注入在应用程序中的所有其他组件中使用构造函数注入。构造函数注入是执行依赖注入的首选机制,但不幸的是ASP.NET页面和控件类中的构造函数注入是不可能的。

So the idea is to use property injection in your page classes and controls, but solely use constructor injection in ALL other components in your application. Constructor injection is the preferred mechanism for doing dependency injection, but constructor injection is unfortunately not possible in ASP.NET Page and Control classes.

所以你的页面可能如下所示:

So your page could look like this:

public class CancelOrderPage : Page
{
    [Dependency]
    public ICommandHandler<CancelOrder> CancelOrderHandler { get; set; }

    void CancelButton_Click(object sender, EventArgs e) {
        this.CancelOrderHandler.Handle(new CancelOrder {
            OrderId = Guid.Parse(this.OrderIdHiddenField.Value)
        });
    }
}

对于其余的应用程序,使用构造函数注入:

For the rest of your application, use constructor injection:

public class CancelOrderHandler : ICommandHandler<CancelOrder>
{
    private readonly IAuthorizedRepository<Order> orderRepository;
    private readonly IEventPublisher eventPublisher;

    public CancelOrderHandler(IAuthorizedRepository<Order> orderRepository,
        IEventPublisher eventPublisher) {
        this.orderRepository = orderRepository;
        this.eventPublisher = eventPublisher;
    }

    public void Handle(CancelOrder command) {
        // some implementation
    }
}

RegisterDependencies 中,您必须注册您的依赖关系。您可以手动执行:

In the RegisterDependencies you will have to register your dependencies. You can do this manually:

container.RegisterType<ICommandHandler<CancelOrder>, CancelOrderHandler>();
container.RegisterType<IEventPublisher, InProcessPublisher>();
container.RegisterType(
    typeof(AuthorizedRepository<>), 
    typeof(DbContextRepo<>));

或者您可以使用批注册

这篇关于在ASP.NET中使用Unity.WebForms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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