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. \(\S1\) 描述性统计

    在认识客观世界的过程中,统计学的思想和方法经常起着不可替代的作用.在许多工程及自然科学的专业领域中,包括可靠性分析.质量控制.生物信息.脑科学.心理分析.经济分析.金融风险管理.社会科学推断.行为科学 ...

  2. note of introduction of Algorithms(Lecture 3 - Part1)

    Lecture 3(part 1) Divide and conquer 1. the general paradim of algrithm as bellow: 1. divide the pro ...

  3. dojo/request模块整体架构解析

    总体说明 做前端当然少不了ajax的使用,使用dojo的童鞋都知道dojo是基于模块化管理的前端框架,其中对ajax的处理位于dojo/request模块.一般情况下我们使用ajax请求只需要引入do ...

  4. 浅谈Excel开发:五 Excel RTD函数

        上文介绍了Excel中的UDF函数,本文介绍一下同样重要的RTD函数.从Excel 2002开始,Excel引入了一种新的查看和更新实时数据的机制,即real-time data简称RTD函数 ...

  5. Linux gzip、gunzip

    200 ? "200px" : this.width)!important;} --> 介绍 gzip是linux自带的压缩文件命令,它的压缩比大概能达到60%-70%,比z ...

  6. jQuery的attr与prop

    jQuery1.6中新添加了一个prop方法,看起来和用起来都和attr方法一样,这两个方法有什么区别呢?这要从HTMl 的attribute与property区别说起,attr与prop正是这两个东 ...

  7. Qt 设置应用程序图标

    Qt4设置应用程序图标 将一个ico图标放在资源文件夹下; 然后建立txt,输入 IDI_ICON1 DISCARABLE "myico.ico"; 保存文件,将其后缀改为.rc; ...

  8. java source not found

    今天的挑战是,为什么与源码明明放在本机,调试的时候却说source not found呢? 这个是因为,我重新建立了debug,然后没有选择对应的项目. 另外一个问题是,如果给底层的jar附上源码呢?

  9. Java基础之网络编程

    网络编程:1.网络编程概述 (1)网络模型 OSI参考模型 TCP/IP参考模型 (2)网络通讯要素 IP地址 端口号 传输协议 (3)网络通讯前提: **找到对方IP **数据要发送到指定端口.为了 ...

  10. lua table序列化和反序列化

    function serialize(obj) local lua = "" local t = type(obj) if t == "number" then ...