Example #1
0
        /// <summary>
        /// Adds the specified <paramref name="contractType"/> and <paramref name="serviceMeta"/> to the mapper.
        /// </summary>
        /// <param name="contractType"></param>
        /// <param name="serviceMeta"></param>
        public void Add(Type contractType, BaseServiceInfo serviceMeta)
        {
            if (serviceMeta.ServiceType != null && !CanTypeCast(contractType, serviceMeta.ServiceType))
            {
                ExceptionHelper.ThrowInvalidServiceType(contractType, serviceMeta.ServiceType);
            }

            if (string.IsNullOrEmpty(serviceMeta.ServiceName)) //if there's no default service
            {
                if (_mapTable.ContainsKey(contractType))
                {
                    if (_mapTable[contractType].DefaultService != null)
                    {
                        var msg = $"An element with the key '{contractType.FullName}' already exists.";
                        ExceptionHelper.ThrowArgumentException(msg);
                    }
                    _mapTable[contractType].DefaultService = serviceMeta;
                }
                else
                {
                    _mapTable.Add(contractType, new ServiceMapInfo()
                    {
                        DefaultService = serviceMeta
                    });
                }
            }
            else
            {
                if (_mapTable.ContainsKey(contractType))
                {
                    _mapTable[contractType].CreateServicesIfNotInitialized();
                    if (_mapTable[contractType].Services.ContainsKey(serviceMeta.ServiceName))
                    {
                        var msg = $"An element with the key '{contractType.FullName}' already exists.";
                        ExceptionHelper.ThrowArgumentException(msg);
                    }
                    _mapTable[contractType].Services.Add(serviceMeta.ServiceName, serviceMeta);
                }
                else
                {
                    _mapTable.Add(contractType, new ServiceMapInfo()
                    {
                        Services = new ServicesPoint()
                        {
                            [serviceMeta.ServiceName] = serviceMeta
                        }
                    });
                }
            }
        }
Example #2
0
        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))
                        }
                    });
                }
            }
        }