SimpleInjector的使用
SimpleInjector的使用
国庆大假,但是,小弟依然学习,前天去看了房交会,尼玛,吓屎宝宝了,还是安静的坐在公司里巧代码比较合适;
the usage of injector consists of four to six steps;
1.create a new container
2.configure the container(register)
3.[optionally] verify the container
4.store the container for use by the application
5.retrieve instances from the container(resolve)
6.[optionally] dispose the container when the application ends;
usage 1:(Getting an object by a specified type;)
using SimpleInjector;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
public interface IUser
{
void show();
} public class User : IUser
{
public void show()
{
Console.WriteLine("show instance...国庆敲代码!");
}
} class Program
{ static void Injector()
{
var container = new Container();
container.Register<IUser, User>(); //这里完成我们测注册
var repository = container.GetInstance<IUser>();
repository.show(); }
static void Main(string[] args)
{ Injector();
Console.ReadLine();
}
}
}
接下来是我们MVC中的实例
首先在集成MVC的时候,记得添加这个东东: Install-Package SimpleInjector.Integration.Web.Mvc
接口
public interface IFuck
{
//这里是属性;
int a { get; set; } string show(string name); }
实现类
public class Fuck: Service.IFuck
{ public Fuck()
{
a = DateTime.Now.Millisecond;
}
public int a { get; set; }
public string show(string name)
{
return "fuck " + name+" "+a;
}
}
然后在global中注册;
using SimpleInjector;
using SimpleInjector.Integration.Web.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using WebApplication3.Service; namespace WebApplication3
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes); //ioc的注册主要是下面的这一坨代码地呀
var container = new Container();
container.Register<IFuck, ServiceImple.Fuck>(Lifestyle.Transient);
container.Verify();
DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container)); }
}
}
最后再我们controller中使用滴呀;
public class UserController : Controller
{
private readonly Service.IFuck _ifuck;
public UserController(Service.IFuck ifuck)
{
_ifuck = ifuck; //然后这里就是我们基本的
}
public ActionResult Test()
{
ViewBag.value = _ifuck.show(" 房地产开发商 ");
return View();
}
}
结果:

接下来是我们API中的实例
首先你要添加这个东西;
SimpleInjector.Integration.WebApi
我这里就将就建立一个API的项目
添加路由;
public class WebApiConfig
{
public static void Register(HttpConfiguration config)
{ config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
} }
global中注册
GlobalConfiguration.Configure(App_Start.WebApiConfig.Register); // 注册 api 路由 必须写在mvc 路由注册之前否则会找不到。
接着就是我们的ioc 注册的啦;
总结:
算了,还是看官网的教程吧,这文章写的太乱了;
这里我们总结了三种类型的IOC:返回实例,mvc,webapi
然后这个只是一个入门,在实际的大项目开发中,还有....
因为,没发现,注册的时候每次都要手动的去添加接口和服务吗?这样在大型团队开发中是很麻烦,
那么解决的方法就是:DLL,
下期节目我们再做解答;
SimpleInjector的使用的更多相关文章
- SimpleInjector与MVC4集成,与Web Api集成,以及通过属性注入演示
SimpleInjector与MVC4集成,与Web Api集成,以及通过属性注入演示 1,与MVC集成 见http://simpleinjector.codeplex.com/wikipage? ...
- MVC+simpleinjector 简单使用
一.首先添加NuGet包如下图
- C#开源
商业协作和项目管理平台-TeamLab 网络视频会议软件-VMukti 驰骋工作流程引擎-ccflow [免费]正则表达式测试工具-Regex-Tester Windows-Phone-7-SDK E ...
- MVC WebAPI 三层分布式框架开发
版权声明:本文为博主原创文章,未经博主允许不得转载. 前言:SOA(面向服务的架构)是目前企业应用开发过程中普遍采用的技术,基于MVC WebAPI三层分布式框架开发,以此适用于企业信息系统的业务处理 ...
- C# 开源项目一
商业协作和项目管理平台-TeamLab 网络视频会议软件-VMukti 驰骋工作流程引擎-ccflow [免费]正则表达式测试工具-Regex-Tester Windows-Phone-7-SDK E ...
- AspNet Identity and IoC Container Registration
https://github.com/trailmax/IoCIdentitySample TL;DR: Registration code for Autofac, for SimpleInject ...
- How To Easily Call WCF Services Properly z
Please note: this article has been superceded by the documentation for the ChannelAdam WCF Library. ...
- C#开源大全--汇总(转)
商业协作和项目管理平台-TeamLab 网络视频会议软件-VMukti 驰骋工作流程引擎-ccflow [免费]正则表达式测试工具-Regex-Tester Windows-Phone-7-SDK E ...
- Redis Master/Slave 实践
本次我们将模拟 Master(1) + Slave(4) 的场景,并通过ASP.NET WEB API进行数据的提交及查询,监控 Redis Master/Slave 数据分发情况,只大致概述,不会按 ...
随机推荐
- BZOJ 1355 & KMP
BZOJ放这种丝帛我也是醉了... 不过来填一下求最小循环节的坑... 以这道题为例,相同文本串粘起来的串中取一小节,可以把任意一个字符看做文本串头. 那么我们一次KMP求出next函数然后显见,最后 ...
- HDU-I Hate It
Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写 ...
- 匈牙利 算法&模板
匈牙利 算法 一. 算法简介 匈牙利算法是由匈牙利数学家Edmonds于1965年提出.该算法的核心就是寻找增广路径,它是一种用增广路径求二分图最大匹配的算法. 二分图的定义: 设G=(V,E)是一个 ...
- JavaScript进阶(三)之对象
返回星期方法 getDay() 返回星期,返回的是0-6的数字,0 表示星期天.如果要返回相对应“星期”,通过数组完成,代码如下: <script type="text/javascr ...
- BZOJ4553: [Tjoi2016&Heoi2016]序列
Description 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他.玩具上有一个数列,数列中某些项的值 可能会变化,但同一个时刻最多只有一个值发生变化.现在佳媛姐姐已经研究出了所 ...
- 如何让代码实现C++
其实本来就知道 但是主要突然忘了去怎样实现C++代码 ,所以写个笔记记忆一下 让代码实现C++ 只需要修改.m文件为 .mm 就可以了 突然是不是很像打脸. 不过希望能给不知道的小伙伴一点帮助哦 ...
- Linux查看操作系统时间
date命令的功能是显示和设置系统日期和时间. 该命令的一般格式为: date [选项] 显示时间格式(以+开头,后面接格式) date 设置时间格式 命令中各选项的含义分别为: -d datestr ...
- java pio项目使用
一.简介 pio是apache的一个针对microsoft office的一个开源项目. Apache POI - the Java API for Microsoft Documents 官网地址: ...
- [LintCode] Segment Tree Build II 建立线段树之二
The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...
- [CareerCup] 17.13 BiNode 双向节点
17.13 Consider a simple node-like data structure called BiNode, which has pointers to two other node ...