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:

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 测试接口和方法的实现

    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里面测试


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

    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控制器的代码,很简单:

    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)

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
直接演示一种最简单的方法吧:

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

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

 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如下:

    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

你可能感兴趣的:(inject)