Ejemplo n.º 1
0
        /// <summary>
        /// build EntitiesInfos
        /// </summary>
        private static void BuildEntitiesInfos(AutumnDataSettings settings, Assembly callingAssembly, string apiVersion)
        {
            var entities  = new Dictionary <Type, EntityInfo>();
            var resources = new Dictionary <Type, ResourceInfo>();

            foreach (var type in (settings.EntityAssembly ?? callingAssembly).GetTypes())
            {
                var resourceAttribute = type.GetCustomAttribute <ResourceAttribute>(false);
                var entityAttribute   = type.GetCustomAttribute <EntityAttribute>(false);
                if (entityAttribute == null && resourceAttribute == null)
                {
                    continue;
                }


                PropertyInfo keyPropertyInfo              = null;
                PropertyInfo createDatePropertyInfo       = null;
                PropertyInfo lastModifiedDatePropertyInfo = null;
                PropertyInfo createByPropertyInfo         = null;
                PropertyInfo lastModifiedByPropertyInfo   = null;
                foreach (var property in type.GetProperties())
                {
                    var keyAttribute = property.GetCustomAttribute <IdAttribute>();
                    if (keyAttribute != null)
                    {
                        keyPropertyInfo = property;
                    }

                    if (property.GetCustomAttribute <CreatedDateAttribute>(true) != null &&
                        IsAuditableDateProperty(property))
                    {
                        createDatePropertyInfo = property;
                    }

                    if (property.GetCustomAttribute <LastModifiedDateAttribute>(true) != null &&
                        IsAuditableDateProperty(property))
                    {
                        lastModifiedDatePropertyInfo = property;
                    }

                    if (property.GetCustomAttribute <CreatedByAttribute>(true) != null &&
                        IsAuditableByProperty(property))
                    {
                        createByPropertyInfo = property;
                    }

                    if (property.GetCustomAttribute <LastModifiedByAttribute>(true) != null &&
                        IsAuditableByProperty(property))
                    {
                        lastModifiedByPropertyInfo = property;
                    }
                }

                entities.Add(type,
                             new EntityInfo(
                                 settings,
                                 type,
                                 entityAttribute,
                                 keyPropertyInfo,
                                 createDatePropertyInfo,
                                 lastModifiedDatePropertyInfo,
                                 createByPropertyInfo,
                                 lastModifiedByPropertyInfo));

                if (resourceAttribute != null)
                {
                    var proxyTypes = DataModelHelper.BuildModelsRequestTypes(type);
                    resources.Add(type, new ResourceInfo(
                                      settings,
                                      entities[type], resourceAttribute.Version ?? apiVersion, proxyTypes, resourceAttribute));
                }
            }

            Mapper.Reset();

            Mapper.Initialize(c =>
            {
                foreach (var value in resources.Values)
                {
                    foreach (var proxyType in value.ProxyRequestTypes)
                    {
                        c.CreateMap(proxyType.Value, value.EntityInfo.EntityType);
                    }
                }
            });

            settings.EntitiesInfos = new ReadOnlyDictionary <Type, EntityInfo>(entities);
            settings.ResourceInfos = new ReadOnlyDictionary <Type, ResourceInfo>(resources);
            settings.ApiVersions   = new ReadOnlyCollection <string>(settings.ResourceInfos.Values
                                                                     .Select(e => e.ApiVersion)
                                                                     .Distinct().OrderBy(e => e).ToList());
        }