Intro#
在上一篇文章中主要介绍了一下要做的依赖注入的整体设计和大概编程体验,这篇文章要开始写代码了,开始实现自己的依赖注入框架。
类图#
首先来温习一下上次提到的类图
服务生命周期#
服务生命周期定义:
Copy
public enum ServiceLifetime : sbyte
{
///
/// Specifies that a single instance of the service will be created.
///
Singleton = 0,
///
/// Specifies that a new instance of the service will be created for each scope.
///
Scoped = 1,
///
/// Specifies that a new instance of the service will be created every time it is requested.
///
Transient = 2,
}
服务定义#
服务注册定义:
Copy
public class ServiceDefinition
{
// 服务生命周期
public ServiceLifetime ServiceLifetime { get; }
// 实现类型
public Type ImplementType { get; }
// 服务类型
public Type ServiceType { get; }
// 实现实例
public object ImplementationInstance { get; }
// 实现工厂
public Func ImplementationFactory { get; }
// 获取真实的实现类型
public Type GetImplementType()
{
if (ImplementationInstance != null)
return ImplementationInstance.GetType();
if (ImplementationFactory != null)
return ImplementationFactory.Method.DeclaringType;
if (ImplementType != null)
return ImplementType;
return ServiceType;
}
public ServiceDefinition(object instance, Type serviceType)
{
ImplementationInstance = instance;
ServiceType = serviceType;
ServiceLifetime = ServiceLifetime.Singleton;
}
public ServiceDefinition(Type serviceType, ServiceLifetime serviceLifetime) : this(serviceType, serviceType, serviceLifetime)
{
}
public ServiceDefinition(Type serviceType, Type implementType, ServiceLifetime serviceLifetime)
{
ServiceType = serviceType;
ImplementType = implementType ?? serviceType;
ServiceLifetime = serviceLifetime;
}
public ServiceDefinition(Type serviceType, Func factory, ServiceLifetime serviceLifetime)
{
ServiceType = serviceType;
ImplementationFactory = factory;
ServiceLifetime = serviceLifetime;
}
}
为了使用起来更方便添加了一些静态方法
Copy
public static ServiceDefinition Singleton(Func factory)
{
return new ServiceDefinition(typeof(TService), factory, ServiceLifetime.Singleton);
}
public static ServiceDefinition Scoped(Func factory)
{
return new ServiceDefinition(typeof(TService), factory, ServiceLifetime.Scoped);
}
public static ServiceDefinition Transient(Func factory)
{
return new ServiceDefinition(typeof(TService), factory, ServiceLifetime.Transient);
}
public static ServiceDefinition Singleton()
{
return new ServiceDefinition(typeof(TService), ServiceLifetime.Singleton);
}
public static ServiceDefinition Scoped()
{
return new ServiceDefinition(typeof(TService), ServiceLifetime.Scoped);
}
public static ServiceDefinition Transient()
{
return new ServiceDefinition(typeof(TService), ServiceLifetime.Transient);
}
public static ServiceDefinition Singleton() where TServiceImplement : TService
{
return new ServiceDefinition(typeof(TService), typeof(TServiceImplement), ServiceLifetime.Singleton);
}
public static ServiceDefinition Scoped() where TServiceImplement : TService
{
return new ServiceDefinition(typeof(TService), typeof(TServiceImplement), ServiceLifetime.Scoped);
}
public static ServiceDefinition Transient() where TServiceImplement : TService
{
return new ServiceDefinition(typeof(TService), typeof(TServiceImplement), ServiceLifetime.Transient);
}
ServiceContainer#
serviceContainer v1#
Copy
public class ServiceContainer : IServiceContainer
{
internal readonly List _services;
private readonly ConcurrentDictionary _singletonInstances;
private readonly ConcurrentDictionary _scopedInstances;
private readonly List