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

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

问题描述

我正在尝试在 webforms 项目中实现 DI,所以我在我的 UI 层中安装了 Unity.WebForms dll.一旦我做了一个 App_Start 文件夹就为我创建了一个 UnityWebFormsStart 类文件.在此文件中,有一个方法 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 类文件中添加一些内容?我如何以及在哪里解析网络表单中的类型?我是否用任何属性装饰它?

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 Page 和 Control 类中无法进行构造函数注入.

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天全站免登陆