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容器的更多相关文章

  1. 手动实现一个 IOC/DI 容器

    第一章为源码解析. 第二章为实现一个简单的 IOC 容器. 第三章进阶 Spring 插件开发. 手动实现一个 IOC/DI 容器 上一篇文章里我们已经对 Spring 的源码有了一个大概的认识,对于 ...

  2. String框架搭建的基本步骤,及从 IOC & DI 容器中获取 Bean(spring框架bean的配置)--有实现数据库连接池的链接

    Spring框架的插件springsource-tool-suite-3.4.0.RELEASE-e4.3.1-updatesite(是一个压缩包)导入步骤: eclipse->help-> ...

  3. 深入理解IoC/DI

    ------------------------------------------------------------------------ 理解IoC/DI 1.控制反转 --> 谁控制谁 ...

  4. IoC/DI基本思想的演变

    ---------------------------------------------------------------------------------- (1)IoC/DI的概念 IoC ...

  5. 工厂方法模式与IoC/DI

    IoC——Inversion of Control  控制反转 DI——Dependency Injection   依赖注入 1:如何理解IoC/DI        要想理解上面两个概念,就必须搞清 ...

  6. spring--学习之IOC DI

    2.1.1  IoC是什么 Ioc-Inversion of Control,即"控制反转",不是什么技术,而是一种设计思想.在Java开发中,Ioc意味着将你设计好的对象交给容器 ...

  7. spring ioc DI 理解

    下面是我从网上找来的一些大牛对spring ioc和DI的理解,希望也能让你对Spring ioc和DI的设计思想有更进一步的认识. 一.分享Iteye的开涛对Ioc的精彩讲解 Ioc—Inversi ...

  8. 工厂方法模式与IoC/DI控制反转和依赖注入

    IoC——Inversion of Control  控制反转 DI——Dependency Injection   依赖注入 要想理解上面两个概念,就必须搞清楚如下的问题: 参与者都有谁? 依赖:谁 ...

  9. IoC/DI

    From:http://jinnianshilongnian.iteye.com/blog/1471944 我对IoC/DI的理解 博客分类: spring杂谈 IoCDI  IoC IoC: Inv ...

随机推荐

  1. linux中/etc/profile、/etc/profile.d/、/etc/bashrc、~/.bashrc、~/.bash_profile、~/.bash_logout的作用与区别

    作用: /etc/profile:登录时用来设置环境变量,执行文件中的命令,对所有用户生效. /etc/profile.d/:登录时和执行bash命令打开子shell时执行目录下所有已.sh结尾的脚本 ...

  2. 企业Centos服务器分区常用方案

    方案一: 一般公司生产环境,数据不是非常重要,集群架构中的一个节点服务器: /boot: 200M swap: 8G内存以下,1.5倍 8G内存以上,设置8G就够了 / : 剩下的所以存储空间 方案二 ...

  3. 转载+++++iptables详解+++++转载

    转载:http://blog.chinaunix.net/uid-26495963-id-3279216.html 一.前言 防火墙,其实说白了讲,就是用于实现Linux下访问控制的功能的,它分为硬件 ...

  4. js 正则表达式验证

    验证数字的正则表达式集 验证数字:^[0-9]*$ 验证n位的数字:^\d{n}$ 验证至少n位数字:^\d{n,}$ 验证m-n位的数字:^\d{m,n}$ 验证零和非零开头的数字:^(0|[1-9 ...

  5. OGNL表达式与EL表达式

    一.OGNL表达式 a)什么是OGNL? OGNL是Object-Graph Navigation Language的缩写,它是一种功能强大的表达式语言, 通过它简单一致的表达式语法.主要功能:    ...

  6. HAProxy的三种不同类型配置方案

    haproxy是一款功能强大.灵活好用反向代理软件,提供了高可用.负载均衡.后端服务器代理的功能,它在7层负载均衡方面的功能很强大(支持 cookie track, header rewrite等等) ...

  7. 3.从AbstractQueuedSynchronizer(AQS)说起(2)——共享模式的锁获取与释放

    在上节中解析了AbstractQueuedSynchronizer(AQS)中独占模式对同步状态获取和释放的实现过程.本节将会对共享模式的同步状态获取和释放过程做一个解析.上一节提到了独占模式和共享模 ...

  8. 开涛spring3(6.3) - AOP 之 6.3 基于Schema的AOP

    6.3  基于Schema的AOP 基于Schema的AOP从Spring2.0之后通过“aop”命名空间来定义切面.切入点及声明通知. 在Spring配置文件中,所以AOP相关定义必须放在<a ...

  9. SharePoint 无法删除搜索服务应用程序

    在SharePoint的使用中,经常会遇到某些服务创建失败,某些服务删除不成功的情况.这里,我们就遇到了搜索服务创建失败,然后删除也不成功,使用管理中心的UI无法删除,PowerShell命令也无法删 ...

  10. Linux常用网络测试命令

    Linux常用网络测试命令 1. ifconfig     可以使用ifconfig命令来配置并查看网络接口的配置情况.    例如: (1) 配置eth0的IP地址, 同时激活该设备. #ifcon ...