private void MapWithExistingService(ServiceAttribute serviceAttribute, ServiceMapTable services, Type interfaceType, Type serviceType) { var mapInfo = services[interfaceType]; //Duplicate Service exception check for default service if (_strictMode && string.IsNullOrEmpty(serviceAttribute.Name) && mapInfo.DefaultService != null) { ExceptionHelper.ThrowDuplicateServiceException($"{serviceType.FullName} implements {interfaceType.FullName}"); } //Set default service if service name is not set if (string.IsNullOrEmpty(serviceAttribute.Name)) { mapInfo.DefaultService = new ServiceInfo(serviceType, BaseServiceInfo.GetDecorators(interfaceType)); } else //named service { if (mapInfo.Services == null) { mapInfo.Services = new ServicesPoint(); } if (_strictMode) { //Duplicate Service exception check for named services if (mapInfo.Services.ContainsKey(serviceAttribute.Name)) { ExceptionHelper.ThrowDuplicateServiceException($"{serviceType.FullName} implements {interfaceType.FullName}"); } } mapInfo.Services[serviceAttribute.Name] = new ServiceInfo(serviceType, BaseServiceInfo.GetDecorators(interfaceType)); } }
private void ReplaceServiceInternal <T>(Type service, string serviceName) where T : class { Type interfaceType = typeof(T); ServicesMapTable.AddOrReplace(interfaceType, new ServiceInfo(service, BaseServiceInfo.GetDecorators(interfaceType), serviceName)); //send update to observer ContractObserver.Update(interfaceType); }
private void ReplaceServiceInternal <TC>(Expression <Func <TC> > expression, string serviceName) where TC : class { Type interfaceType = typeof(TC); var serviceMeta = new ServiceInfo <TC, TC>(expression, BaseServiceInfo.GetDecorators(interfaceType), serviceName); ServicesMapTable.AddOrReplace(interfaceType, serviceMeta); //send update to observer ContractObserver.Update(interfaceType); }
private IRootContainer AddInternal <TC>(Func <TC> serviceAction, string serviceName) where TC : class { Type interfaceType = typeof(TC); var serviceMeta = new ServiceInfo <TC, TC>(serviceAction, BaseServiceInfo.GetDecorators(interfaceType), serviceName); ServicesMapTable.Add(interfaceType, serviceMeta); //send update to observer ContractObserver.Update(interfaceType); return(this); }
private void MapService(ServiceMapTable services, Type interfaceType, Type serviceType) { /*Check for ServiceAttribute, if not found, we don't need to process */ var serviceAttribute = serviceType.GetTypeInfo().GetCustomAttributes <ServiceAttribute>().FirstOrDefault(); if (serviceAttribute == null) { return; } /*if the contract already exists in the services collection, try to update the services. * if strict mode is enabled and service already exists then it throw an exception, otherwise it updates. * if service name is not specified then it will be consider as DefaultService */ if (services.ContainsKey(interfaceType)) { MapWithExistingService(serviceAttribute, services, interfaceType, serviceType); } else { //If service name is not specified then add it as default service if (string.IsNullOrEmpty(serviceAttribute.Name)) { services.Add(interfaceType, new ServiceMapInfo() { DefaultService = new ServiceInfo(serviceType, BaseServiceInfo.GetDecorators(interfaceType)) }); } else { services.Add(interfaceType, new ServiceMapInfo() { Services = new ServicesPoint() { [serviceAttribute.Name] = new ServiceInfo(serviceType, BaseServiceInfo.GetDecorators(interfaceType)) } }); } } }
private void ReplaceSingletonServiceInternal <TC, TS>(string serviceName) where TC : class where TS : TC { Type interfaceType = typeof(TC); var serviceMeta = new ServiceInfo <TC, TS>(isReusable: true, decorators: BaseServiceInfo.GetDecorators(interfaceType), serviceName: serviceName); ServicesMapTable.AddOrReplace(interfaceType, serviceMeta); //send update to observer ContractObserver.Update(interfaceType); }