コード例 #1
0
ファイル: DTOGenerator.cs プロジェクト: kzfabi/EntitiesToDTOs
        /// <summary>
        /// Gets the Properties of an EntityDTO to use in a Constructor as parameters, recursively including the Base Class properties.
        /// </summary>
        /// <param name="entityDTO">EntityDTO to get the Properties from.</param>
        /// <returns></returns>
        private static List <DTOClassProperty> GetPropertiesForConstructor(DTOEntity entityDTO)
        {
            LogManager.LogMethodStart();

            var result = new List <DTOClassProperty>();

            result.AddRange(entityDTO.Properties);

            if (entityDTO.BaseDTO != null)
            {
                result.AddRange(DTOGenerator.GetPropertiesForConstructor(entityDTO.BaseDTO));
            }

            LogManager.LogMethodEnd();

            return(result);
        }
コード例 #2
0
ファイル: DTOGenerator.cs プロジェクト: kzfabi/EntitiesToDTOs
        /// <summary>
        /// Gets the objects that represents the DTOs that needs to be generated from the received EDMX Document.
        /// </summary>
        /// <param name="parameters">Parameters for the generation of DTOs.</param>
        /// <returns></returns>
        private static List <DTOEntity> GetEntityDTOs(GenerateDTOsParams parameters)
        {
            LogManager.LogMethodStart();

            // Variables
            string typeName        = null;
            bool   generateType    = false;
            var    complexTypeDTOs = new List <DTOEntity>();
            var    entityTypeDTOs  = new List <DTOEntity>();

            // Get the DTOs for the ComplexType nodes
            foreach (XElement complexType in EdmxHelper.GetComplexTypeNodes(parameters.EDMXDocument))
            {
                typeName = complexType.Attribute(EdmxNodeAttributes.EntityType_Name).Value;

                generateType = parameters.TypesToGenerateFilter.Contains(typeName);

                if (generateType == true)
                {
                    List <EntityNavigation> entityNavigations = null;

                    complexTypeDTOs.Add(new DTOEntity(complexType, parameters, entityNavigations));
                }
            }

            // Set the Complex Types available
            PropertyHelper.SetComplexTypes(complexTypeDTOs);

            // Get Navigation Properties
            List <EntityNavigation> entitiesNavigations = DTOGenerator.GetEntitiesNavigations(parameters);

            // Get the DTOs for the EntityType nodes
            foreach (XElement entityTypeNode in EdmxHelper.GetEntityTypeNodes(parameters.EDMXDocument))
            {
                typeName = entityTypeNode.Attribute(EdmxNodeAttributes.EntityType_Name).Value;

                generateType = parameters.TypesToGenerateFilter.Contains(typeName);

                if (generateType == true)
                {
                    entityTypeDTOs.Add(new DTOEntity(entityTypeNode, parameters, entitiesNavigations));
                }
            }

            foreach (DTOEntity dto in entityTypeDTOs)
            {
                // Set Know Types of DTO
                dto.SetKnownTypes(entityTypeDTOs, parameters.DTOsServiceReady);

                // Set DTOs childs of DTO
                dto.SetChilds(entityTypeDTOs);

                // Set reference to DTO Base Class
                dto.SetDTOBase(entityTypeDTOs);

                // Set Navigation Target DTO references
                foreach (DTOClassProperty dtoProperty in dto.Properties.Where(p => p.IsNavigation == true))
                {
                    DTOEntity dtoTarget = entityTypeDTOs.FirstOrDefault(e => e.NameDTO == dtoProperty.NavigatesToDTOName);

                    if (dtoTarget != null)
                    {
                        dtoProperty.SetNavigatesToDTOReference(dtoTarget);
                    }
                }
            }

            // Get the final result
            var result = new List <DTOEntity>();

            result.AddRange(complexTypeDTOs);
            result.AddRange(entityTypeDTOs);

            LogManager.LogMethodEnd();

            return(result);
        }
コード例 #3
0
        /// <summary>
        /// Gets the CSharp Type from a EDMX Property.
        /// </summary>
        /// <param name="propertyNode">Type attribute.</param>
        /// <param name="entityOwnerName">Entity owner name.</param>
        /// <returns></returns>
        public static string GetTypeFromEDMXProperty(XElement propertyNode, string entityOwnerName)
        {
            // Get the Type attribute
            XAttribute typeAttribute = propertyNode.Attribute(EdmxNodeAttributes.Property_Type);

            // Check Type attribute exists
            if (typeAttribute == null)
            {
                string propertyName = propertyNode.Attribute(EdmxNodeAttributes.Property_Name).Value;

                throw new ApplicationException(string.Format(Resources.Error_PropertyTypeAttributeMissing,
                                                             entityOwnerName, propertyName));
            }

            // Get the Type value
            string edmxTypeValue = propertyNode.Attribute(EdmxNodeAttributes.Property_Type).Value;

            // Check Type value is not empty
            if (string.IsNullOrWhiteSpace(edmxTypeValue))
            {
                string propertyName = propertyNode.Attribute(EdmxNodeAttributes.Property_Name).Value;

                throw new ApplicationException(string.Format(Resources.Error_PropertyTypeAttributeMissing,
                                                             entityOwnerName, propertyName));
            }

            // Check if it is Nullable
            bool       isNullable        = true;
            XAttribute nullableAttribute = propertyNode.Attribute(EdmxNodeAttributes.Property_Nullable);

            if (nullableAttribute != null)
            {
                isNullable = (nullableAttribute.Value == Resources.XmlBoolTrue);
            }

            // Variables
            string outputType = null;
            bool   outputTypeAdmitsNullable = false;

            // Check if it is a supported type and we got the mapping for it
            if (PropertyHelper.EdmxCSharpMapping.ContainsKey(edmxTypeValue))
            {
                // Do the mapping between the EDMX type and the C# type
                outputType = PropertyHelper.EdmxCSharpMapping[edmxTypeValue].Name;
                outputTypeAdmitsNullable = PropertyHelper.EdmxCSharpMapping[edmxTypeValue].AllowsNullable;
            }
            else
            {
                // Get type name without namespce to check if it is a type defined in the EDMX (ComplexType, EnumType)
                string edmxTypeName = EdmxHelper.GetNameWithoutNamespace(edmxTypeValue);

                // Check if it is a complex type
                DTOEntity complexTypeDTO = PropertyHelper.ComplexTypes.FirstOrDefault(ct => ct.Name == edmxTypeName);

                if (complexTypeDTO != null)
                {
                    // It is a ComplexType
                    outputType = complexTypeDTO.NameDTO;
                    outputTypeAdmitsNullable = true;
                }
                else if (PropertyHelper.EnumTypes.Exists(e => e.Name == edmxTypeName))
                {
                    // It is an EnumType
                    outputType = edmxTypeName;
                    outputTypeAdmitsNullable = true;
                }
                else
                {
                    // Not a supported Type neither a Type defined in the EDMX
                    // Use object type and notify the user
                    outputType = Resources.CSharpTypeObject;
                    outputTypeAdmitsNullable = true;

                    string propertyName = propertyNode.Attribute(EdmxNodeAttributes.Property_Name).Value;

                    // TODO: ffernandez, indicate Project-ProjectItem-Line-Column
                    VisualStudioHelper.AddToErrorList(TaskErrorCategory.Warning,
                                                      string.Format(Resources.Warning_NotSupportedEDMXPropertyType, entityOwnerName, propertyName, edmxTypeValue),
                                                      null, null, null, null);
                }
            }

            // Check if it is Nullable and the Type admits Nullable Types
            if (isNullable && outputTypeAdmitsNullable)
            {
                outputType = string.Format(Resources.CSharpTypeNullableT, outputType);
            }

            return(outputType);
        }