示例#1
0
        public static DnServicoTransacionalBase GetServiceInstanceByServiceType(this Type serviceType, SessaoDeRequisicaoDoUsuario SessionRequest)
        {
            if (serviceType == null)
            {
                throw new ArgumentNullException(nameof(serviceType));
            }
            if (SessionRequest == null)
            {
                throw new ArgumentNullException(nameof(SessionRequest));
            }
            if (serviceType.Name == "DnDynamicProxy")
            {
                serviceType = serviceType.BaseType;
            }
            if (serviceType == null)
            {
                throw new ArgumentNullException(nameof(serviceType));
            }

            if (!serviceType.IsSubclassOf(typeof(DnServicoTransacionalBase)))
            {
                throw new DesenvolvimentoIncorretoException($"The service instance attempt using the {nameof(GetServiceInstanceByServiceType)} method failed because the passed type is not a {nameof(DnServicoTransacionalBase)}");
            }

            if (SessionRequest.Servicos.TryGetValue(serviceType, out var ser))
            {
                return(ser as DnServicoTransacionalBase);
            }

            var service = FabricaDeServico.Criar(serviceType, SessionRequest.LocalHttpContext, SessionRequest);

            SessionRequest.Servicos.Add(serviceType, service);

            return(service);
        }
示例#2
0
        internal static async Task <List <DnServicoTransacionalBase> > ExecuteEntityAndCompositions <T>(this DnValidacao <T> validation, object entity, MethodInfo method) where T : EntidadeBase
        {
            if (validation is null)
            {
                throw new ArgumentNullException("validation");
            }
            if (method is null)
            {
                throw new ArgumentNullException("method");
            }

            var tasks = new List <Task>();
            var t1    = method.MakeGenericMethod(typeof(T)).Invoke(null, new object[] { validation, entity, null, null }).DnCast <Task>();

            if (t1 != null)
            {
                tasks.Add(t1);
            }

            List <DnServicoTransacionalBase> anotherServices = new List <DnServicoTransacionalBase>();

            if (entity != null)
            {
                var properties = entity.GetType().GetProperties().ToList().Where(x => x.IsDefined(typeof(DnComposicaoAtributo))).ToList();
                foreach (var property in properties)
                {
                    var entityCompositionValue = property.GetValue(entity);
                    var entityType             = property.PropertyType.GetListTypeNonNull();
                    if (!entityType.IsDnEntity())
                    {
                        continue;
                    }

                    var service = FabricaDeServico.Criar(entityType, validation.SessaoDaRequisicao.LocalHttpContext, "For multiple validation");
                    anotherServices.Add(service);

                    if (property.PropertyType.IsList())
                    {
                        if (!(entityCompositionValue is IEnumerable collection))
                        {
                            continue;
                        }

                        int i = 0;
                        foreach (var item in collection)
                        {
                            var compositionPropertyName = $"{property.GetJsonPropertyName()}[{i}]";
                            var compositionFieldName    = $"{property.GetUiPropertyName()}[{i}]";

                            var t2 = method.MakeGenericMethod(entityType).Invoke(null, new object[] { service.Validacao, item, compositionPropertyName, compositionFieldName }).DnCast <Task>();
                            if (t2 != null)
                            {
                                tasks.Add(t2);
                            }
                            i++;
                        }
                    }
                    else
                    {
                        var compositionPropertyName = property.GetJsonPropertyName();
                        var compositionFieldName    = property.GetUiPropertyName();

                        var t2 = method.MakeGenericMethod(entityType).Invoke(null, new object[] { service.Validacao, entityCompositionValue, compositionPropertyName, compositionFieldName }).DnCast <Task>();
                        if (t2 != null)
                        {
                            tasks.Add(t2);
                        }
                    }
                }
            }

            await Task.WhenAll(tasks.ToArray());

            return(anotherServices);
        }