• SimpleInjector与MVC4集成,与Web Api集成,以及通过属性注入演示


    SimpleInjector与MVC4集成,与Web Api集成,以及通过属性注入演示

     
     

    1,与MVC集成

    http://simpleinjector.codeplex.com/wikipage?title=Integration%20Guide&referringTitle=Home
    我们自己建个MVC4项目测试

    1.1 nuget


    只需要安装Mvc的集成即可,其它的依赖会自动安装:

    Install-Package SimpleInjector.Integration.Web.Mvc

    1.2 Global.asax:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
                GlobalConfiguration.Configure(WebApiConfig.Register);
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
                 
                //按如下步骤添加
                // 1. Create a new Simple Injector container
                var container = new Container();
     
                // 2. Configure the container (register)
                //container.Register<IUserService, UserService>(Lifestyle.Transient);
                //container.Register<IUserRepository, SqlUserRepository>(Lifestyle.Singleton);
                //随便写个例子测试,名字就不计较了,Lifestyle的区别自行查文档
                container.Register<Iaaa, aaa>(Lifestyle.Transient);
     
                // 3. Optionally verify the container's configuration.
                container.Verify();
     
                // 4. Store the container for use by Page classes.
                //WebApiApplication.Container = container;
                System.Web.Mvc.DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
     
                // 5. register global filters //如果有注册全局过滤器的需要加此节点,和下面注释掉的方法
                //RegisterGlobalFilters(GlobalFilters.Filters, container);
            }
     
            //public static void RegisterGlobalFilters(GlobalFilterCollection filters, Container container)
            //{
            //}
        }

    1.3 测试接口和方法的实现

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    public interface Iaaa
    {
        int a { get; set; }
        string hello(string str);
    }
     
    public class aaa : Iaaa
    {
        public aaa()
        {
            a = DateTime.Now.Millisecond;
        }
        public int a { get; set; }
     
        public string hello(string str)
        {
            return "hello " + str + " timestamp: " + a;
        }
    }

    1.4 到一个controller里面测试


    注意私有变量,和构造函数的使用即可。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    public class testController : Controller
    {
        private Iaaa _srv;
     
        public testController(Iaaa srv)
        {
            _srv = srv;
        }
        public string Index(string id)
        {
            return _srv.hello(id);
        }
    }

    访问/test/myname 正确输出"hello myname timestamp 234"
    其实跟官网的一模一样:http://simpleinjector.codeplex.com/

    2,与WebApi集成

    http://simpleinjector.codeplex.com/wikipage?title=Web%20API%20Integration&referringTitle=Integration%20Guide
    在同一个项目里测试就行了,更好演示与mvc和与web api集成的区别,所以也不需要新建项目,及添加引用了

    2.1 新建一个webapi的控制器tst


    基本上是复制test控制器的代码,很简单:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    public class tstController : ApiController
    {
        private Iaaa _srv;
        public tstController(Iaaa srv)
        {
            _srv = srv;
        }
        public string get(string id)
        {
            return _srv.hello(id);
        }
    }

    2.2 测试失败


    访问/api/tst/aaa,却直接报错Type 'WebApplication1.Controllers.tstController' does not have a default constructor,按照上面给的官网的集成说明提示更改Global.asax即可,改后如下(见修改点1和2)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
                GlobalConfiguration.Configure(WebApiConfig.Register);
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
                 
                //按如下步骤添加
                // 1. Create a new Simple Injector container
                var container = new Container();
     
                // a.1 webapi, frist register controller 修改点1
                var services = GlobalConfiguration.Configuration.Services;
                var controllerTypes = services.GetHttpControllerTypeResolver()
                    .GetControllerTypes(services.GetAssembliesResolver());
     
                // register Web API controllers (important! http://bit.ly/1aMbBW0)
                foreach (var controllerType in controllerTypes)
                {
                    container.Register(controllerType);
                }
     
                // 2. Configure the container (register)
                //container.Register<IUserService, UserService>(Lifestyle.Transient);
                //container.Register<IUserRepository, SqlUserRepository>(Lifestyle.Singleton);
                //随便写个例子测试,名字就不计较了,Lifestyle的区别自行查文档
                container.Register<Iaaa, aaa>(Lifestyle.Transient);
     
                // 3. Optionally verify the container's configuration.
                container.Verify();
     
                // 4. Store the container for use by Page classes.
                //WebApiApplication.Container = container;
                System.Web.Mvc.DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
                 
                // a.2 webapi  按照文档,写在verify()后面 修改点2
                GlobalConfiguration.Configuration.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);
     
                // 5. register global filters //如果有注册全局过滤器的需要加此节点,和下面注释掉的方法
                //RegisterGlobalFilters(GlobalFilters.Filters, container);
            }
     
            //public static void RegisterGlobalFilters(GlobalFilterCollection filters, Container container)
            //{
            //}
        }

    再测试就通过了

    3,通过属性注入

    我们上面演示的都是通过构造器注入,关于属性注入,SimpleInjector做了严格限制,但是还是支持,需要显式注入:http://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&referringTitle=Home#Property-Injection
    直接演示一种最简单的方法吧:

    1
    2
    3
    container.RegisterInitializer<HandlerBase>(handlerToInitialize => {
        handlerToInitialize.ExecuteAsynchronously = true;
    });

    所以完成版的Global.asax如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    protected void Application_Start()
           {
               AreaRegistration.RegisterAllAreas();
               GlobalConfiguration.Configure(WebApiConfig.Register);
               FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
               RouteConfig.RegisterRoutes(RouteTable.Routes);
               BundleConfig.RegisterBundles(BundleTable.Bundles);
                
               //按如下步骤添加
               // 1. Create a new Simple Injector container
               var container = new Container();
     
               // a.1 webapi, frist register controller 修改点1
               var services = GlobalConfiguration.Configuration.Services;
               var controllerTypes = services.GetHttpControllerTypeResolver()
                   .GetControllerTypes(services.GetAssembliesResolver());
     
               // register Web API controllers (important! http://bit.ly/1aMbBW0)
               foreach (var controllerType in controllerTypes)
               {
                   container.Register(controllerType);
               }
     
               // 2. Configure the container (register)
               //container.Register<IUserService, UserService>(Lifestyle.Transient);
               //container.Register<IUserRepository, SqlUserRepository>(Lifestyle.Singleton);
               //随便写个例子测试,名字就不计较了,Lifestyle的区别自行查文档
               container.Register<Iaaa, aaa>(Lifestyle.Transient);
               container.RegisterInitializer<tstController>(c => c.s2 = new bbb());//显然,其实就是手动指定
     
               // 3. Optionally verify the container's configuration.
               container.Verify();
     
               // 4. Store the container for use by Page classes.
               //WebApiApplication.Container = container;
               System.Web.Mvc.DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
                
               // a.2 webapi 修改点2,按照文档,写在verify()后面
               GlobalConfiguration.Configuration.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);
     
               // 5. register global filters //如果有注册全局过滤器的需要加此节点,和下面注释掉的方法
               //RegisterGlobalFilters(GlobalFilters.Filters, container);
           }
     
           //public static void RegisterGlobalFilters(GlobalFilterCollection filters, Container container)
           //{
           //}
       }


    完整版的webapi的controller如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    public class tstController : ApiController
    {
        private Iaaa _srv;
        public Ibbb s2 { get; set;} //注意s2,是一个属性
     
        public tstController(Iaaa srv)
        {
            _srv = srv;
        }
        //演示构造函数注入
        public string get(string id)
        {
            return _srv.hello(id);
        }
       //演示属性注入
        public string get()
        {
            return s2.curtime;
        }
    }

        取名字随意了点,包涵。
        访问/api/tst/,得到current time is: 2013-12-15T23:06:0,我们并没有在构造器中初始化Ibbb,但已经是从aaa对象里取到了值,测试通过。

    4,示例代码

    git clone https://github.com/walkerwzy/simpleinjectorSample.git

     
    分类: c#.net MVC
    标签: DIsimpleinjector
  • 相关阅读:
    【软件工程】学期总结
    【操作系统】实验四 主存空间的分配和回收
    学术诚信与职业道德
    【软件工程】《构建之法》八、九、十章读后感
    【操作系统】实验三 进程调度模拟程序
    【软件工程】《构建之法》6-7章读后感
    【操作系统】实验二 作业调度模拟程序
    【软件工程】复利计算器--结对编程3.0评论博客
    复利计算器--结对编程2.0
    学习进度条
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/4527670.html
Copyright © 2020-2023  润新知