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的使用的更多相关文章

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

    SimpleInjector与MVC4集成,与Web Api集成,以及通过属性注入演示   1,与MVC集成 见http://simpleinjector.codeplex.com/wikipage? ...

  2. MVC+simpleinjector 简单使用

    一.首先添加NuGet包如下图

  3. C#开源

    商业协作和项目管理平台-TeamLab 网络视频会议软件-VMukti 驰骋工作流程引擎-ccflow [免费]正则表达式测试工具-Regex-Tester Windows-Phone-7-SDK E ...

  4. MVC WebAPI 三层分布式框架开发

    版权声明:本文为博主原创文章,未经博主允许不得转载. 前言:SOA(面向服务的架构)是目前企业应用开发过程中普遍采用的技术,基于MVC WebAPI三层分布式框架开发,以此适用于企业信息系统的业务处理 ...

  5. C# 开源项目一

    商业协作和项目管理平台-TeamLab 网络视频会议软件-VMukti 驰骋工作流程引擎-ccflow [免费]正则表达式测试工具-Regex-Tester Windows-Phone-7-SDK E ...

  6. AspNet Identity and IoC Container Registration

    https://github.com/trailmax/IoCIdentitySample TL;DR: Registration code for Autofac, for SimpleInject ...

  7. How To Easily Call WCF Services Properly z

    Please note: this article has been superceded by the documentation for the ChannelAdam WCF Library. ...

  8. C#开源大全--汇总(转)

    商业协作和项目管理平台-TeamLab 网络视频会议软件-VMukti 驰骋工作流程引擎-ccflow [免费]正则表达式测试工具-Regex-Tester Windows-Phone-7-SDK E ...

  9. Redis Master/Slave 实践

    本次我们将模拟 Master(1) + Slave(4) 的场景,并通过ASP.NET WEB API进行数据的提交及查询,监控 Redis Master/Slave 数据分发情况,只大致概述,不会按 ...

随机推荐

  1. iOS学习21之UILabel, UITextField, UIButton, UIImageView

    1.UILabel 1> 概述 UILabel (标签): 是显示文本的控件.在App中 UILabel 是出现频率最高的控件 UILabel 是 UIView 子类,作为子类一般是为了扩充父类 ...

  2. 利用百度云盘API上传文件至百度云盘

    一.获取Access Token示例 1. 请您将以下HTTP请求直接粘贴到浏览器地址栏内,并按下回车键. https://openapi.baidu.com/oauth/2.0/authorize? ...

  3. ACM 水池数目

    水池数目 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 南阳理工学院校园里有一些小河和一些湖泊,现在,我们把它们通一看成水池,假设有一张我们学校的某处的地图,这个地 ...

  4. ACM: CodeForces 140A New Year Table-数学几何

    CodeForces 140A New Year Table Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d ...

  5. HDU - Pseudoforest

    Description In graph theory, a pseudoforest is an undirected graph in which every connected componen ...

  6. BZOJ 1191 超级英雄 Hero 题解

    BZOJ 1191 超级英雄 Hero 题解 Description 现在电视台有一种节目叫做超级英雄,大概的流程就是每位选手到台上回答主持人的几个问题,然后根据回答问题的多少获得不同数目的奖品或奖金 ...

  7. BZOJ4027: [HEOI2015]兔子与樱花 贪心

    觉得是贪心,但是一开始不太肯定...然后就A了 一个点对它的父亲的贡献就是自己的权值加儿子的个数 #include<bits/stdc++.h> using namespace std; ...

  8. Cento 安装配置FastFDS

    unzip -x libfastcommon-master.zip ./make.sh ./make.sh install /usr/include/fastcommon cd FastDFS ./m ...

  9. 通过MongoDB的samus驱动实现基本数据操作

    一.MongoDB的驱动 MongoDB支持多种语言的驱动: 在此我们只介绍 C# 的驱动.仅C#驱动都有很多种,每种驱动的形式大致相同,但是细节各有千秋,因此代码不能通用.比较常用的是官方驱动和sa ...

  10. IDictionary<TKey, TValue> vs. IDictionary

    Enumerating directly over an IDictionary<TKey,TValue>returns a sequence of  KeyValuePair struc ...