/// <summary> /// Gets the service from the container /// </summary> public async Task <object> Get(ServiceDescriptor descriptor, IServiceContainer services) { if (_scopedServices == null) { _scopedServices = new Dictionary <Type, object>(); } //try to get from created instances bool found = _scopedServices.TryGetValue(descriptor.ServiceType, out object instance); if (found) { return(instance); } //we couldn't find any created instance. create new. instance = await services.CreateInstance(descriptor.ImplementationType, this); if (instance is null) { return(null); } if (descriptor.AfterCreatedMethod != null) { descriptor.AfterCreatedMethod.DynamicInvoke(instance); } if (descriptor.ProxyType != null) { IServiceProxy p = (IServiceProxy)await services.CreateInstance(descriptor.ProxyType, this); instance = p.Proxy(instance); } if (instance != null) { _scopedServices.Add(descriptor.ServiceType, instance); } return(instance); }
private object ApplyAfterGet(BuiltServiceDescriptor descriptor, object service) { if (descriptor.ProxyInstance != null) { service = descriptor.ProxyInstance.Proxy(service); } else if (descriptor.ProxyType != null) { IServiceProxy proxy = (IServiceProxy)Get(descriptor.ProxyType); service = proxy.Proxy(service); } if (descriptor.Implementation == ImplementationType.Singleton) { descriptor.Instance = service; } return(service); }
private async Task <object> Get(ServiceDescriptor descriptor, IContainerScope scope = null) { if (descriptor.IsPool) { IServicePool pool = (IServicePool)descriptor.Instance; PoolServiceDescriptor pdesc = await pool.GetAndLock(scope); if (pdesc == null) { throw new NullReferenceException("Could not get service from container"); } if (pool.Type == ImplementationType.Scoped && scope == null) { throw new InvalidOperationException("Type is registered as Scoped but scope parameter is null for IServiceContainer.Get method"); } if (scope != null) { scope.UsePoolItem(pool, pdesc); } return(pdesc.GetInstance()); } switch (descriptor.Implementation) { //create new instance case ImplementationType.Transient: object transient = await CreateInstance(descriptor.ImplementationType, scope); if (descriptor.AfterCreatedMethod != null) { descriptor.AfterCreatedMethod.DynamicInvoke(transient); } if (descriptor.ProxyType != null) { IServiceProxy p = (IServiceProxy) await CreateInstance(descriptor.ProxyType, scope); return(p.Proxy(transient)); } return(transient); case ImplementationType.Scoped: if (scope == null) { throw new InvalidOperationException("Type is registered as Scoped but scope parameter is null for IServiceContainer.Get method"); } return(await scope.Get(descriptor, this)); case ImplementationType.Singleton: //if instance already created return if (descriptor.Instance != null) { return(descriptor.Instance); } //create instance for first time and set Instance property of descriptor to prevent re-create for next times object instance = await CreateInstance(descriptor.ImplementationType, scope); if (descriptor.AfterCreatedMethod != null) { descriptor.AfterCreatedMethod.DynamicInvoke(instance); } if (descriptor.ProxyType != null) { IServiceProxy p = (IServiceProxy) await CreateInstance(descriptor.ProxyType, scope); object proxyObject = p.Proxy(instance); descriptor.Instance = proxyObject; } else { descriptor.Instance = instance; } return(descriptor.Instance); default: return(null); } }