前言
IOC个人之前一直搞不明白,不够理解,写这篇文章也是为了加强学习。
一、IOC和DIP
1.IOC
IOC中文叫做控制反转:一般开发,上端都依赖下端的对象,类似于在类中new一个其他类对象。这样上端对下端会有很强的依赖。控制反转就是将对下端对象的依赖转移到第三方容器,能够使程序拥有更好的扩展性。
2.DIP
说到IOC这里要提到依赖倒置原则DIP:系统架构时,高层模块不该依赖于底层模块,二者通过抽象来依赖,依赖抽象,而不是具体对象。
二、写一个超简单的IOC容器实例
1.代码准备
类:Person,依赖House、Car两个类,House、Car依赖Money类:
//Money
public interface IMoney
{
bool IsSoMuch { get; set; }
}
public class Money : IMoney
{
public bool IsSoMuch { get; set; }
public Money()
{
Console.WriteLine("Money已被构造!!!");
}
}
//Car 依赖 Money
public interface ICar
{
IMoney Money { get; set; }
void Run();
}
public class Car : ICar
{
public IMoney Money { get; set; }
public Car(IMoney money)
{
this.Money = money;
Console.WriteLine("Car已被构造!");
}
public void Run()
{
throw new NotImplementedException();
}
}
//House 依赖 Money
public interface IHouse
{
IMoney Money { get; set; }
void Live();
}
public class House : IHouse
{
public IMoney Money { get; set; }
public House(IMoney money)
{
this.Money = money;
Console.WriteLine("House已被构造!!!");
}
public void Live() { }
}
//Person 依赖 House、Car
public interface IPerson
{
IHouse House { get; set; }
ICar Car { get; set; }
void Eat();
void Sleep();
}
public class Person :IPerson
{
public IHouse House { get; set; }
public ICar Car { get; set; }
public Person(IHouse house,ICar car)
{
House = house;
Car = car;
Console.WriteLine("Person已被构造!!!");
}
public void Eat() { }
public void Sleep(){ }
}
2.IOC容器
代码如下(示例):
public interface IMyIOCContainer
{
void ResolveType<TFrom,TTo>();
T Resolve<T>();
}
public class MyIOCContainer : IMyIOCContainer
{
private readonly Dictionary<string, Type> MyContianerDictionary = new Dictionary<string, Type>();
/// <summary>
/// 保存将抽象类型名称和实体类类型保存到字典
/// </summary>
/// <typeparam name="TFrom"></typeparam>
/// <typeparam name="TTo"></typeparam>
public void ResolveType<TFrom, TTo>()
{
MyContianerDictionary.Add(typeof(TFrom).FullName,typeof(TTo));
}
/// <summary>
/// 创建对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public T Resolve<T>()
{
Type type = MyContianerDictionary[typeof(T).FullName];
return (T)CreateObject(type);
}
public object CreateObject(Type type)
{
//获取构造函数
ConstructorInfo[] cons = type.GetConstructors();
List<object> paraList = new List<object>();
//如果构造函数数量大于0
if (cons.Count() > 0)
{
//选择参数数量最多的构造函数
ConstructorInfo con = cons.OrderByDescending(c => c.GetParameters().Length).FirstOrDefault();
foreach (ParameterInfo para in con.GetParameters())
{
Type paraType = para.ParameterType;
//字典容器查询出具体的参数对象类型
Type targetType = MyContianerDictionary[paraType.FullName];
//递归实例化所有参数对象,以及其依赖的对象,并添加到数组中
paraList.Add(this.CreateObject(targetType));
}
}
//返回对象
return Activator.CreateInstance(type, paraList.ToArray());
}
}
3.调用
代码如下(示例):
class Program
{
static void Main(string[] args)
{
//关联依赖的对象不在上端生成,而是通过容器来生成
IMyIOCContainer myIOC = new MyIOCContainer();
myIOC.ResolveType<IPerson, Person>();
myIOC.ResolveType<IHouse, House>();
myIOC.ResolveType<ICar, Car>();
myIOC.ResolveType<IMoney, Money>();
IPerson person = myIOC.Resolve<IPerson>();
Console.ReadKey();
}
}
结果
总结
实现了一个极简单的IOC容器,写的不好之处请指教。一般在项目中,用第三方容器较多,比如MEF、Unity等。