IoC是个好东西,但是为了这个功能而使用类似 Castle 这种大型框架的话,感觉还是不大好

代码是之前写的,一直没详细搞,今天整理了一下,感觉挺实用的.

IoC定义接口:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace System
{
/// <summary>
/// 一个接口,支持IoC定义
/// </summary>
public interface IIoCDefine
{
/// <summary>
/// 定义类型
/// </summary>
/// <param name="type"></param>
/// <param name="implType"></param>
void Define(Type type, Type implType);
/// <summary>
/// 定义类型
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="Impl"></typeparam>
void Define<T, Impl>() where Impl : T;
}
}

IoC解析接口:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace System
{
/// <summary>
/// 一个接口,支持IoC解析
/// </summary>
public interface IIoCResolve
{ /// <summary>
/// 解析类型
/// </summary>
/// <param name="type"></param>
object Resolve(Type type);
/// <summary>
/// 解析类型
/// </summary>
/// <typeparam name="T"></typeparam>
T Resolve<T>();
}
}

具体实现:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace System
{
/// <summary>
/// 支持定义解析
/// </summary>
internal class ResolveBase : IIoCResolve, IIoCDefine
{ ICache<Type, Type> _typeMaps = CacheFactory.CreateCache<Type, Type>(); ICache<string, Type> _genericTypeMaps = CacheFactory.CreateCache<string, Type>(); /// <summary>
/// 定义类型
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="Impl"></typeparam>
public void Define<T, Impl>() where Impl : T
{
Define(typeof(T), typeof(Impl));
} /// <summary>
/// 定义类型
/// </summary>
/// <param name="type"></param>
/// <param name="implType"></param>
public void Define(Type type, Type implType)
{
if (type == null)
{
throw Error.ArgumentNullException("type");
}
if (implType == null)
{
throw Error.ArgumentNullException("implType");
} if (implType.IsInterface || implType.IsAbstract)
{
throw Error.ArgumentException("不能是抽象类型或接口", "implType");
} if (!type.IsGenericTypeDefinition == implType.IsGenericTypeDefinition)
{
throw Error.ArgumentException("泛型不能与非泛型互转", "implType");
} if (!implType.IsGenericTypeDefinition && !type.IsAssignableFrom(implType) && !type.GetTypeInfo().IsAssignableFrom(implType))
{
throw Error.ArgumentException("不是子类", "implType");
} _typeMaps[type] = implType; if (type.IsGenericType)
{
_genericTypeMaps[GetGenericTypeDefinitionKey(type)] = implType;
} } string GetGenericTypeDefinitionKey(Type type)
{ // var temp = _typeMaps.Keys.FirstOrDefault(o => o.IsGenericType && o.Name == type.Name && o.Namespace == o.Namespace && o.Module.Equals(type.Module));
//return type.Name + "_" + type.Namespace + "_" + type.Module.Name; return type.FullName + "_" + type.Module.Name;
} /// <summary>
/// 得到类型实例
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public T Resolve<T>()
{
Type type = typeof(T); return (T)Resolve(type);
} /// <summary>
/// 得到类型实例
/// </summary>
/// <returns></returns>
public object Resolve(Type type)
{ var instanceType = GetInstanceType(type); //if (instanceType.IsAbstract || instanceType.IsInterface)
//{
// throw Error.ArgumentException("解析失败 : 不能是接口或抽象类");
//} return ActivatorCache.CreateInstance(instanceType); // return Activator.CreateInstance(instanceType);
} /// <summary>
/// 得到解析类型
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
Type GetInstanceType(Type type)
{
if (type == null)
{
throw Error.ArgumentNullException("type");
}
Type instanceType; if (!type.IsGenericType)
{
//如果非泛型,直接拿缓存
instanceType = _typeMaps[type];
if (instanceType == null)
{
throw Error.ArgumentException("解析失败 : 未定义解析类型");
}
return instanceType;
} //是泛型
instanceType = _genericTypeMaps[GetGenericTypeDefinitionKey(type)]; if (instanceType == null)
{
if (type.IsGenericTypeDefinition)
{
throw Error.ArgumentException("解析失败 : 未定义解析类型");
}
//找不到具体泛型类型
//从泛型定义中找
var genericType = type.GetGenericTypeDefinition();
instanceType = _genericTypeMaps[GetGenericTypeDefinitionKey(genericType)];
if (instanceType == null)
{
throw Error.ArgumentException("解析失败 : 未定义解析类型");
}
instanceType = instanceType.MakeGenericType(type.GenericTypeArguments);
} if (type.IsGenericTypeDefinition)
{
//指定泛型参数类型
var gType = instanceType.MakeGenericType(type.GenericTypeArguments);
instanceType = gType;
} return instanceType;
} //bool Equals(Type[] types1, Type[] types2)
//{
// if (types1 == null || types2 == null)
// {
// return false;
// }
// if (types1.Length != types2.Length)
// {
// return false;
// } // int length = types1.Length; // for (int i = 0; i < length; i++)
// {
// var type1 = types1[i];
// var type2 = types2[i]; // if (!types1.Equals(type2) && !(type1.IsAssignableFrom(type2) || type2.IsAssignableFrom(type1)))
// {
// return false;
// } // } // return true; //} public object Resolve(Type type, params object[] values)
{
var instanceType = GetInstanceType(type); if (instanceType.IsAbstract || instanceType.IsInterface)
{
throw Error.ArgumentException("解析失败 : 不能是接口或抽象类");
} var types = values.Select(o => o.GetType()).ToArray(); return ActivatorCache.CreateInstance(instanceType, values); // return Activator.CreateInstance(instanceType, values);
} public T Resolve<T>(params object[] values)
{ return (T)Resolve(typeof(T), values);
}
}
}

给个入口:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace System
{ /// <summary>
/// IoC入口对象
/// </summary>
public sealed class IoCManager : IIoCResolve, IIoCDefine
{
IoCManager()
{
var resolve = new ResolveBase();
_resolve = resolve;
_define = resolve;
} public static readonly IoCManager Instance = new IoCManager(); IIoCResolve _resolve; IIoCDefine _define; /// <summary>
/// 定义类型
/// </summary>
/// <param name="type"></param>
/// <param name="implType"></param>
public void Define(Type type, Type implType)
{
_define.Define(type, implType);
} /// <summary>
/// 定义类型
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="Impl"></typeparam>
public void Define<T, Impl>() where Impl : T
{
_define.Define<T, Impl>();
} /// <summary>
/// 解析类型
/// </summary>
/// <param name="type"></param>
/// <param name="implType"></param>
public object Resolve(Type type)
{
return _resolve.Resolve(type);
} /// <summary>
/// 解析类型
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="Impl"></typeparam>
public T Resolve<T>()
{
return _resolve.Resolve<T>();
} }
}

到这里就大功告成了!!!

测试:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Dai.CommonLib.Tests.Models
{
public interface TestInterface
{
} public interface TestInterface<T>:TestInterface
{
} class TestClass : TestInterface
{ } class TestClass<T> : TestInterface<T>
{ } class TestClass1<T> : TestClass<T>
{ } }
      [TestMethod]
public void TestMethod2()
{ IoCManager.Instance.Define(typeof(TestInterface), typeof(TestClass<decimal>)); IoCManager.Instance.Define(typeof(TestInterface<>), typeof(TestClass<>)); IoCManager.Instance.Define(typeof(TestInterface<string>), typeof(TestClass1<string>)); var model = IoCManager.Instance.Resolve<TestInterface>(); var model1 = IoCManager.Instance.Resolve<TestInterface<int>>(); var model2 = IoCManager.Instance.Resolve<TestInterface<string>>(); Assert.IsTrue(model is TestClass<decimal>); Assert.IsTrue(model1 is TestClass<int>); Assert.IsTrue(model2 is TestClass1<string>); IoCManager.Instance.Define(typeof(TestInterface), typeof(TestClass)); var model3 = IoCManager.Instance.Resolve<TestInterface>(); Assert.IsTrue(model3 is TestClass);
}

优点: 支持泛型,如果指定了具体的泛型类型,那优先解析具体类型,否则从泛型类型定义中替换具体泛型类型参数,如上面的代码的 TestClass和TestClass1类型

下次写个Aop Inject 静态编织注入的文章

自制简单实用IoC的更多相关文章

  1. jQuery的几种简单实用效果

    许久未分享博客,或许已生疏. 闲来无事, 分享几个jQuery简单实用的效果案例 不喜勿喷... 1.页面常用的返回顶部 <!DOCTYPE html> <html lang=&qu ...

  2. IoC原理-使用反射/Emit来实现一个最简单的IoC容器

    从Unity到Spring.Net,到Ninject,几年来陆陆续续用过几个IoC框架.虽然会用,但也没有一直仔细的研究过IoC实现的过程.最近花了点时间,下了Ninject的源码,研究了一番,颇有收 ...

  3. 【最简单IOC容器实现】实现一个最简单的IOC容器

    前面DebugLZQ的两篇博文: 浅谈IOC--说清楚IOC是什么 IoC Container Benchmark - Performance comparison 在浅谈IOC--说清楚IOC是什么 ...

  4. 经验分享:10个简单实用的 jQuery 代码片段

    尽管各种 JavaScirpt 框架和库层出不穷,jQuery 仍然是 Web 前端开发中最常用的工具库.今天,向大家分享我觉得在网站开发中10个简单实用的 jQuery 代码片段. 您可能感兴趣的相 ...

  5. 简单实用的PHP防注入类实例

    这篇文章主要介绍了简单实用的PHP防注入类实例,以两个简单的防注入类为例介绍了PHP防注入的原理与技巧,对网站安全建设来说非常具有实用价值,需要的朋友可以参考下   本文实例讲述了简单实用的PHP防注 ...

  6. php简单实用的操作文件工具类(创建、移动、复制、删除)

    php简单实用好用的文件及文件夹复制函数和工具类(创建.移动.复制.删除) function recurse_copy($src,$dst) {  // 原目录,复制到的目录 $dir = opend ...

  7. php 简单说明IoC (php 实例+注释)

    简单说明IoC <?php //Ioc ———— 设计方式 //控制反转 Inversion of Control //依赖关系的转移 //依赖抽象而非实践 //用于解决高层应用依赖 底层组件, ...

  8. 基于Bootstrap简单实用的tags标签插件

    http://www.htmleaf.com/jQuery/ jQuery之家 自由分享jQuery.html5和css3的插件库 基于Bootstrap简单实用的tags标签插件

  9. C#_简单实用的翻页

    简单实用的生成翻页HTML辅助类 C# using System.Text; namespace ClassLibrary { /// <summary> /// /// </sum ...

随机推荐

  1. java jdb命令详解

    jdb - Java debugger 功能描述: 通过简单的命令行程序,对本地或远程jvm进程进行调试. 开启jdb会话: 有多种方式可以开启jdb会话. (1)常见的方式是采用Jdb命令打开一个新 ...

  2. centos 安装ffmpeg

    wget http://www.ffmpeg.org/releases/ffmpeg-3.1.tar.gz tar -zxvf ffmpeg-3.1.tar.gz cd ffmpeg-3.1 ./co ...

  3. 使用XmlDataDocument将数据存储到XML文档

    string str = "Data Source=192.168.1.20;Initial Catalog=WebTest;User ID=sa;Password="; SqlC ...

  4. 获取机器安装.NET版本的几种方式

    当调查应用程序问题时,通常需要先确认目标机器所安装的 .NET Framework 的版本.可以通过如下方式来确认版本号: 通过控制面板安装程序查询 通过查询注册表获取版本信息 通过查看安装目录获取版 ...

  5. 基础调试命令 - u/ub/uf

    在调试过程中难免会遇到需要反编译代码来分析逻辑的时候,在windbg中,需要反编译代码就要用到u/ub/uf这三个命令.本文这里分别介绍这三个命令各自的用途. 以下是一个quick sort的实例代码 ...

  6. Javascript函数节流

    最近在做网页的时候有个需求,就是浏览器窗口改变的时候需要改一些页面元素大小,于是乎很自然的想到了window的resize事件,于是乎我是这么写的 <!DOCTYPE html> < ...

  7. web系统架构设计中需要知道的点(前端篇)

    上周没写东西,这周写点互联网系统开发中需要了解的技术点,每个点都可以发散出去,连接更多的知识点,打算做个逐步细化的记录. 一个应用的整个生命周期中(生,老,病,死)都需要有一个整体规划. 前期 评估需 ...

  8. C-Lodop 非典型应用

    Lodop是什么? 有人说她是报表打印工具,因为那个add_print_table语句把报表统计的那点事弄了个明明白白: 有人说她是条码打印工具,因为用了她再也不用后台生成条码图片了,前端一行指令就动 ...

  9. Atitit attilax在自然语言处理领域的成果

    Atitit attilax在自然语言处理领域的成果 1.1. 完整的自然语言架构方案(词汇,语法,文字的选型与搭配)1 1.2. 中文分词1 1.3. 全文检索1 1.4. 中文 阿拉伯文 英文的简 ...

  10. java 线程协作 wait(等待)与 notiy(通知)

    一.wait().notify()和notifyAll() 为了更好的支持多线程之间的协作,JDK提供了三个重要的本地方法 //调用某个对象的wait()方法能让当前线程阻塞,并且当前线程必须拥有此对 ...