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. Docker - 生成镜像

    利用docker commit命令生成镜像 Docker镜像是多层存储,每一层是在前一层的基础上进行的修改.而容器是镜像为基础层的多层存储. 如果不使用数据卷,运行一个容器的时候,对任何文件的修改都会 ...

  2. get和post方式请求数据,jsonp

    get方式请求数据: p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 19.0px Consolas; color: #289c97 } p.p2 { ma ...

  3. hdu1420 Prepared for New Acmer 简单数学

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1420 简单数学题 第一次wa在可能和会出现取模后值为负数的情况. 只要会一个数论上的简单公式(a*b) ...

  4. mysql安装出现的问题

    ERROR 1045 (28000): Access denied for user root@localhost (using password: NO) 错误描述: Mysql中添加用户之后可能出 ...

  5. Lists, Maps and Sets in Java

    ArrayList vs LinkedList vs Vector From the hierarchy diagram, they all implement List interface. The ...

  6. java虚拟机学习-深入理解JVM(1)

    1   Java技术与Java虚拟机 说起Java,人们首先想到的是Java编程语言,然而事实上,Java是一种技术,它由四方面组成: Java编程语言.Java类文件格式.Java虚拟机和Java应 ...

  7. iOS安全攻防之越狱设备检测

    iOS 越狱(iOS Jailbreaking),是用于获取苹果公司便携装置操作系统iOS最高权限的一种技术手段,用户使用这种技术及软件可以获取到 iOS 的最高权限,甚至可能可以进一步解开运营商对手 ...

  8. 初码-Azure系列-文章目录

    系统迁移 初码-Azure系列-记一次MySQL数据库向Azure的迁移 初码-Azure系列-迁移PHP应用至Azure的一些实践记录和思考 初码-Azure系列-记一次从阿里云到Azure的迁移和 ...

  9. AngularJS入门第一课

    1.ng-app=" "   定义angularJS的使用范围: 2.ng-init="变量=值;变量='值'"   初始化变量的值,有多个变量时,中间用分号隔 ...

  10. Thinkphp5使用阿里大于短信验证

    现在各种平台登录验证很多时候会使用短信验证,快捷安全,有很多平台提供短信验证服务,相比较而言阿里大于价格比较便宜,快捷,所以在在千锋日常的php教学中多以此为例来说明短信验证的使用.下面我们在tp5中 ...