标签:
Asp.net 5的依赖注入注入系列可以参考链接: [Asp.net 5] DependencyInjection项目代码分析-目录
我们在之前讲微软的实现时,对于OpenIEnumerableService与ClosedIEnumerableService抛下没讲,现在我们就将该部分补充完整。
我们回忆ServiceProvider类的构造函数(对外部使用的)中,注册了IEnumerable<>、new OpenIEnumerableService(_table)的关系。
public ServiceProvider(IEnumerable<ServiceDescriptor> serviceDescriptors) { _root = this; _table = new ServiceTable(serviceDescriptors); _table.Add(typeof(IServiceProvider), new ServiceProviderService()); _table.Add(typeof(IServiceScopeFactory), new ServiceScopeService()); _table.Add(typeof(IEnumerable<>), new OpenIEnumerableService(_table)); }
因为IEnumerable是泛型,所以我们可以推断OpenIEnumerableService类应该实现IGenericService接口。那么如果我们想查找IEnumerable<T>的实现类中间会有怎样的过程呢?
OpenIEnumerableService与ClosedIEnumerableService
由于OpenIEnumerableService实现IGenericService接口,所以会返回IService类型的对象,该对象是ClosedIEnumerableService类型。ClosedIEnumerableService类型内部实际上返回的是ServiceTable中_services所有T的注册项,之后以IEnumerable<T>类型返回。
internal class OpenIEnumerableService : IGenericService { private readonly ServiceTable _table; public OpenIEnumerableService(ServiceTable table) { _table = table; } public ServiceLifetime Lifetime { get { return ServiceLifetime.Transient; } } public IService GetService(Type closedServiceType) { var itemType = closedServiceType.GetTypeInfo().GenericTypeArguments[0]; ServiceEntry entry; return _table.TryGetEntry(itemType, out entry) ? new ClosedIEnumerableService(itemType, entry) : null; } }
internal class ClosedIEnumerableService : IService { private readonly Type _itemType; private readonly ServiceEntry _serviceEntry; public ClosedIEnumerableService(Type itemType, ServiceEntry entry) { _itemType = itemType; _serviceEntry = entry; } public IService Next { get; set; } public ServiceLifetime Lifetime { get { return ServiceLifetime.Transient; } } public IServiceCallSite CreateCallSite(ServiceProvider provider, ISet<Type> callSiteChain) { var list = new List<IServiceCallSite>(); for (var service = _serviceEntry.First; service != null; service = service.Next) { list.Add(provider.GetResolveCallSite(service, callSiteChain)); } return new CallSite(_itemType, list.ToArray()); } private class CallSite : IServiceCallSite { private readonly Type _itemType; private readonly IServiceCallSite[] _serviceCallSites; public CallSite(Type itemType, IServiceCallSite[] serviceCallSites) { _itemType = itemType; _serviceCallSites = serviceCallSites; } public object Invoke(ServiceProvider provider) { var array = Array.CreateInstance(_itemType, _serviceCallSites.Length); for (var index = 0; index != _serviceCallSites.Length; ++index) { array.SetValue(_serviceCallSites[index].Invoke(provider), index); } return array; } public Expression Build(Expression provider) { return Expression.NewArrayInit( _itemType, _serviceCallSites.Select(callSite => Expression.Convert( callSite.Build(provider), _itemType))); } } }
[之前我们介绍ServiceEntry时,明确指出是链表结构,而不是单独存放一个值;其应用在这进行了淋漓尽致的表现]
[Asp.net 5] DependencyInjection项目代码分析4-微软的实现(5)(IEnumerable<>补充)
标签:
原文地址:http://www.cnblogs.com/watermoon2/p/4558837.html