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. hdu4681 String DP(2013多校第8场)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4681 思路: 我是胡搞过的 就是先预处理出(i,j)的正向的最大连续子串和逆向最大连续子串 然后对于A ...

  2. 分享一个完整的Mybatis分页解决方案

    Mybatis 的物理分页是应用中的一个难点,特别是配合检索和排序功能叠加时更是如此. 我在最近的项目中开发了这个通用分页器,过程中参考了站内不少好文章,阅读源码帮助更大minglisoft.cn/t ...

  3. Eclipse设置问题:字体大小、修改注释内容、修改快捷键

    一.设置字体大小,看下图,包括了设计代码字体大小和控制台输出字体大小 二.修改注释内容 选择window---->>preferences 选择Java---->>code s ...

  4. Fast Fourier Transform ——快速傅里叶变换

    问题: 已知$A=a_{0..n-1}$, $B=b_{0..n-1}$, 求$C=c_{0..2n-2}$,使: $$c_i = \sum_{j=0}^ia_jb_{i-j}$$ 定义$C$是$A$ ...

  5. LNMP1.3一键安装Linux环境,配置Nginx运行ThinkPHP3.2

    LNMP1.3一键安装Linux环境,配置Nginx运行ThinkPHP3.2 你是否遇见过:安装LNMP1.3环境后,运行ThinkPHP 3.2,只能打开首页,不能访问控制器,报404错误. 按照 ...

  6. 《安卓网络编程》之第三篇 使用Apache接口

    在Android系统中,提供了一下三种通信接口: 标准的Java 接口:java.net Apache接口:org.apache.http Android网络接口:android.net.http 在 ...

  7. C#控制台或应用程序中两个多个Main()方法的可行性方案

    大多数初级程序员或学生都认为在C#控制台或应用程序中只能有一个Main()方法.但是事实上是可以有多个Main()方法的. 在C#控制台或应用程序中,在多个类中,且每个类里最多只能存在一个Main() ...

  8. Akka(4): Routers - 智能任务分配

    Actor模式最大的优点就是每个Actor都是一个独立的任务运算器.这种模式让我们很方便地把一项大型的任务分割成若干细小任务然后分配给不同的Actor去完成.优点是在设计时可以专注实现每个Actor的 ...

  9. javaSE_05Java中方法(函数)与重载、递归-思维导图

    思维导图看不清楚时: 1)可以将图片另存为图片,保存在本地来查看 2)右击在新标签中打开放大查看

  10. JavaScript设计模式_02_策略模式

    在程序设计中,我们常常遇到这种情况,要实现某一个功能我们有很多种算法可以实现.这些算法灵活多样,而且可以随意互相替换.这种解决方案就是所谓的策略模式. /* * pre:策略模式 * 示例:公司计算奖 ...