Exemplo n.º 1
0
        /// <summary>
        /// Try to get the DTO type from the parameters. If the DTO is not found, but an entity type is found, then clone the entity type to create a new DTO type..
        /// </summary>
        /// <param name="dtoTypeName">Name of the dto type.</param>
        /// <param name="dtoName">Name of the dto.</param>
        /// <param name="path">The path.</param>
        /// <param name="entityName">Name of the entity.</param>
        /// <param name="entityTypeName">Name of the entity type.</param>
        /// <returns></returns>
        private static Type GetDtoType(string dtoTypeName, string dtoName, string path, string entityName, string entityTypeName)
        {
            Type result     = null;
            Type entityType = null;

            // Search by type name
            if (!string.IsNullOrEmpty(dtoTypeName))
            {
                result = TypesManager.ResolveType(dtoTypeName);
                if (result != null)
                {
                    return(result);
                }
            }
            ISecurityManager securityManager = IoC.Get <ISecurityManager>();

            // Search by dto name
            if (!string.IsNullOrEmpty(dtoTypeName))
            {
                IMetamodelEntity metamodelEntity = securityManager.MetamodelManager.GetEntityByDtoName(dtoName);
                if (metamodelEntity != null)
                {
                    foreach (Type type in metamodelEntity.DtoTypes)
                    {
                        if (dtoName.ToLower().Equals(type.Name.ToLower()))
                        {
                            return(type);
                        }
                    }
                }
            }
            // search by entity name
            if (!string.IsNullOrEmpty(entityName))
            {
                IMetamodelEntity metamodelEntity = securityManager.MetamodelManager.GetEntityByName(entityName);
                if (metamodelEntity != null)
                {
                    if (metamodelEntity.DtoTypes.Count > 0)
                    {
                        return(metamodelEntity.DtoTypes[0]);
                    }
                    else
                    {
                        entityType = metamodelEntity.EntityType;
                    }
                }
            }
            // search by entity type
            if ((!string.IsNullOrEmpty(entityTypeName)) && (entityType == null))
            {
                entityType = TypesManager.ResolveType(entityTypeName);
                if (entityType != null)
                {
                    IMetamodelEntity metamodelEntity = securityManager.MetamodelManager.GetEntity(entityType);
                    if ((metamodelEntity != null) && (metamodelEntity.DtoTypes.Count > 0))
                    {
                        return(metamodelEntity.DtoTypes[0]);
                    }
                }
            }
            // If not found in own metamodel, because is not loaded into the system... but we can get the entity type by name from the telerik metamodel?
            if ((entityType == null) && (!string.IsNullOrEmpty(entityName)))
            {
                entityType = securityManager.Scanner.FindEntityTypeInMetamodel(entityName);
            }
            if ((entityType == null) && (!string.IsNullOrEmpty(path)))
            {
                entityType = securityManager.Scanner.FindEntityTypeInMetamodel(path);
            }
            if (entityType == null)
            {
                return(null);
            }
            result = TypeBuilderHelper.CloneCommonType(entityType, AppDomain.CurrentDomain, "Supido.Business.Dto", "SupidoDynamicModule", entityType.Name + "Dto");
            securityManager.Scanner.ProcessDynamicDto(result, entityType);
            return(result);
        }