Пример #1
0
        /// <summary>
        /// build routes
        /// </summary>
        /// <returns></returns>
        private static void BuildRoutes(AutumnDataSettings settings)
        {
            var routes           = new Dictionary <Type, AttributeRouteModel>();
            var ignoreOperations = new Dictionary <string, IReadOnlyList <HttpMethod> >();

            foreach (var entityType in settings.ResourceInfos.Keys)
            {
                var info = settings.ResourceInfos[entityType];
                var name = info.Name;
                if (settings.NamingStrategy != null)
                {
                    name = settings.NamingStrategy.GetPropertyName(name, false);
                }

                if (settings.PluralizeController && !name.EndsWith("s"))
                {
                    name = string.Concat(name, "s");
                }

                name = $"{info.ApiVersion}/{name}";
                var entityKeyType  = info.EntityInfo.KeyInfo.PropertyType;
                var controllerType = settings.RepositoryControllerType.MakeGenericType(
                    info.EntityInfo.EntityType,
                    info.ProxyRequestTypes[HttpMethod.Post],
                    info.ProxyRequestTypes[HttpMethod.Put],
                    entityKeyType);
                var attributeRouteModel = new AttributeRouteModel(new RouteAttribute(name));
                routes.Add(controllerType, attributeRouteModel);
                ignoreOperations.Add("/" + name, info.IgnoreOperations);
            }

            settings.Routes           = new ReadOnlyDictionary <Type, AttributeRouteModel>(routes);
            settings.IgnoreOperations =
                new ReadOnlyDictionary <string, IReadOnlyList <HttpMethod> >(ignoreOperations);
        }
Пример #2
0
 public EntityInfo(
     AutumnDataSettings dataSettings,
     Type entityType,
     EntityAttribute entityAttribute,
     PropertyInfo keyInfo,
     PropertyInfo createdDateInfo,
     PropertyInfo lastModifiedDateInfo,
     PropertyInfo createdByInfo,
     PropertyInfo lastModifiedByInfo)
 {
     Settings             = dataSettings ?? throw new ArgumentNullException(nameof(dataSettings));
     EntityType           = entityType ?? throw new ArgumentNullException(nameof(entityType));
     Name                 = entityAttribute?.Name ?? entityType.Name;
     KeyInfo              = keyInfo;
     CreatedDateInfo      = createdDateInfo;
     LastModifiedDateInfo = lastModifiedDateInfo;
     CreatedByInfo        = createdByInfo;
     LastModifiedByInfo   = lastModifiedByInfo;
 }
Пример #3
0
        public ResourceInfo(
            AutumnDataSettings dataSettings,
            EntityInfo entityInfo,
            string defaultApiVersion,
            IReadOnlyDictionary <HttpMethod, Type> proxyRequestTypes,
            ResourceAttribute resourceAttribute)
        {
            EntityInfo        = entityInfo;
            ProxyRequestTypes = proxyRequestTypes ?? throw new ArgumentNullException(nameof(proxyRequestTypes));
            ApiVersion        =
                Regex.Match(resourceAttribute.Version ?? string.Empty, "v[0-9]+", RegexOptions.IgnoreCase).Success
                    ? resourceAttribute.Version
                    : defaultApiVersion;
            Name = resourceAttribute.Name ?? entityInfo.EntityType.Name;
            if (dataSettings.Parent.NamingStrategy != null)
            {
                ControllerName = dataSettings.Parent.NamingStrategy.GetPropertyName(Name, false);
            }
            if (dataSettings.PluralizeController && !ControllerName.EndsWith("s"))
            {
                ControllerName = string.Concat(ControllerName, "s");
            }
            var ignoreOperations = new List <HttpMethod>();

            if (!resourceAttribute.Insertable)
            {
                ignoreOperations.Add(HttpMethod.Post);
            }
            if (!resourceAttribute.Updatable)
            {
                ignoreOperations.Add(HttpMethod.Put);
            }
            if (!resourceAttribute.Deletable)
            {
                ignoreOperations.Add(HttpMethod.Delete);
            }
            IgnoreOperations = new ReadOnlyCollection <HttpMethod>(ignoreOperations);
        }
Пример #4
0
 public AutumnDataSettingsBuilder(AutumnDataSettings settings, Assembly callingAssembly)
 {
     _settings        = settings ?? throw new ArgumentNullException(nameof(settings));
     _callingAssembly = callingAssembly;
 }
Пример #5
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());
        }