码迷,mamicode.com
首页 > 其他好文 > 详细

关于注册组件配置和注入的感悟

时间:2020-01-14 12:47:09      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:相关   class   conf   使用   register   系统   加载   运行   pat   

 

C#程序员,可能因为早期接触了很多Web.config这种配置思路的影响,常常难以理解通过代码注入组件的思想,其实是一样的,都是在程序运行时,加载相关组件到运行环境中,供使用;

如:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
        config.Filters.Add(new ExceptionHandlingAttribute());

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}
 config.Filters.Add(new ExceptionHandlingAttribute());这行代码,其实是给过滤器增加一个新的实例,Register方法会在系统启动时被框架调用;


 

关于注册组件配置和注入的感悟

标签:相关   class   conf   使用   register   系统   加载   运行   pat   

原文地址:https://www.cnblogs.com/Tpf386/p/12191144.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!