StructureMap经典的IoC/DI容器
StructureMap是一款很老的IoC/DI容器,从2004年.NET 1.1支持至今。
一个使用例子
//创建业务接口
public interface IDispatchService { }
public interface ICourier { }
public interface IPaymentGateway { }
public interface IPaymentMerchant { }
//接口的实现
public class DispacthService : IDispatchService
{
private ICourier _courier;
public DispacthService(ICourier courier)
{
_courier = courier;
}
public override string ToString()
{
return _courier.ToString();
}
}
public class FedExCourier : ICourier { }
public class StreamLinePaymentMerchant : IPaymentMerchant { }
public class PaymentGateway : IPaymentGateway
{
private IPaymentMerchant _paymentMerchant;
public PaymentGateway(IPaymentMerchant paymentMerchant)
{
_paymentMerchant = paymentMerchant;
}
public override string ToString()
{
return _paymentMerchant.ToString();
}
}
//业务使用
public class OrderService
{
private IPaymentGateway _paymentGateway;
private IDispatchService _dispacthService;
public OrderService(IPaymentGateway paymentGateway, IDispatchService dispacthService)
{
_paymentGateway = paymentGateway;
_dispacthService = dispacthService;
}
public override string ToString()
{
return string.Format("IPaymentGateway:{0} IDispatchService:{1}", _paymentGateway.ToString(), _dispacthService.ToString());
}
}
//配置依赖关系
public class BootStrapper
{
public static void ConfigureStructureMap()
{
ObjectFactory.Initialize(x => x.AddRegistry<ModelRegistry>());
}
}
public class ModelRegistry : Registry
{
public ModelRegistry()
{
For<IPaymentGateway>().Use<PaymentGateway>();
For<IPaymentMerchant>().Use<StreamLinePaymentMerchant>();
For<IDispatchService>().Use<DispacthService>();
For<ICourier>().Use<FedExCourier>();
}
}
class Program
{
static void Main(string[] args)
{
BootStrapper.ConfigureStructureMap();//启用配置
OrderService orderService = ObjectFactory.GetInstance<OrderService>();
Console.WriteLine(orderService.ToString());
IPaymentGateway paymentGateway= ObjectFactory.GetInstance<PaymentGateway>();
Console.WriteLine(paymentGateway);
Console.ReadKey();
}
}
文档:http://structuremap.github.io/documentation/
下载:https://sourceforge.net/projects/structuremap/
GitHub:https://github.com/structuremap/structuremap
StructureMap经典的IoC/DI容器的更多相关文章
- 手动实现一个 IOC/DI 容器
第一章为源码解析. 第二章为实现一个简单的 IOC 容器. 第三章进阶 Spring 插件开发. 手动实现一个 IOC/DI 容器 上一篇文章里我们已经对 Spring 的源码有了一个大概的认识,对于 ...
- String框架搭建的基本步骤,及从 IOC & DI 容器中获取 Bean(spring框架bean的配置)--有实现数据库连接池的链接
Spring框架的插件springsource-tool-suite-3.4.0.RELEASE-e4.3.1-updatesite(是一个压缩包)导入步骤: eclipse->help-> ...
- 深入理解IoC/DI
------------------------------------------------------------------------ 理解IoC/DI 1.控制反转 --> 谁控制谁 ...
- IoC/DI基本思想的演变
---------------------------------------------------------------------------------- (1)IoC/DI的概念 IoC ...
- 工厂方法模式与IoC/DI
IoC——Inversion of Control 控制反转 DI——Dependency Injection 依赖注入 1:如何理解IoC/DI 要想理解上面两个概念,就必须搞清 ...
- spring--学习之IOC DI
2.1.1 IoC是什么 Ioc-Inversion of Control,即"控制反转",不是什么技术,而是一种设计思想.在Java开发中,Ioc意味着将你设计好的对象交给容器 ...
- spring ioc DI 理解
下面是我从网上找来的一些大牛对spring ioc和DI的理解,希望也能让你对Spring ioc和DI的设计思想有更进一步的认识. 一.分享Iteye的开涛对Ioc的精彩讲解 Ioc—Inversi ...
- 工厂方法模式与IoC/DI控制反转和依赖注入
IoC——Inversion of Control 控制反转 DI——Dependency Injection 依赖注入 要想理解上面两个概念,就必须搞清楚如下的问题: 参与者都有谁? 依赖:谁 ...
- IoC/DI
From:http://jinnianshilongnian.iteye.com/blog/1471944 我对IoC/DI的理解 博客分类: spring杂谈 IoCDI IoC IoC: Inv ...
随机推荐
- (转)详解JS位置、宽高属性之一:offset系列
很多初学者对于JavaScript中的offset.scroll.client一直弄不明白,虽然网上到处都可以看一张图(图1),但这张图太多太杂,并且由于浏览器差异性,图示也不完全正确. 图一 不知道 ...
- declare 命令
declare命令用于声明和显示shell变量. declare为shell指令,命令与 typeset一样,可同时指定多个属性.若不加上任何参数,则会显示全部的shell变量与函数(与执行set指令 ...
- [刷题]算法竞赛入门经典(第2版) 5-12/UVa511 - Do You Know the Way to San Jose?
题意:N张地图,查找某地点在不在某些地图上,若在,使用细节多的地图.使用哪个地图的破要求挺多,细心一点就好. 代码:(Accepted,0.000s) //UVa511 - Do You Know t ...
- DevCloud让代码检查更科学
代码检查是软件开发工作中不可或缺的一部分,众所周知,规范化的编码是一个优质项目的保证.华为软件开发云(DevCloud)便提供了专业科学的自动化代码检查工作. 一.华为软件开发云(DevCloud)目 ...
- 编写自己的一个简单的web容器(二)
昨天我们已经能够确定浏览器的请求能够被我们自己编写的服务类所接收并且我们服务类响应的数据也能够正常发送到浏览器客户端,那么我们今天要解决的问题就是让我们的数据能够被浏览器识别并解析. Http(Htt ...
- 130行C语言实现个用户态线程库——ezthread
准确的说是除掉头文件,测试代码和非关键的纯算法代码(只有双向环形链表的ADT),核心代码只有130行左右,已经是蝇量级的用户态线程库了.把这个库取名为ezthread,意思是,这太easy了,人人都可 ...
- EntityFramework6.X之概述
实体框架(EF6.X)是一种对象/关系映射器(O/R Mapping解决方案),一套支持开发面向数据的软件应用技术,采用特定域对象和关系数据形式使用数据,而不必考虑存储这些数据的基础数据库表和列,上层 ...
- Telegram学习解析系列(三) : Build Telegram报错分析总结
正好通过这次 Telegram 的运行,很想把常见的项目运行的错误好好的总结一下,在前面的博客中,又星星散散的总结过错误和一些警告的消除方法,这次把错误处理一下,还有Telegram项目中有999+的 ...
- 开涛spring3(2.1) - IoC基础
2.1.1 IoC是什么 Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想.在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的在 ...
- C# 特性参数(注解属性加在参数前面)
特性参数 webapi 框架里有很多特性参数,为了解除一些新人的疑惑,写个小例子分享下. class Program { static void Main(string[] args) { var m ...