//MVC Controller dan önce olmalı. public static void RegisterRoutes(Api_Models_Assemblies ent, Type pureApiControllerBaseType, Action<Action<HttpConfiguration>> configure)//configure:GlobalConfiguration.Configure { if (null == ent) throw new ArgumentNullException(nameof(ent)); if (null == pureApiControllerBaseType) throw new ArgumentNullException(nameof(pureApiControllerBaseType)); if (null == configure) throw new ArgumentNullException(nameof(configure)); CreateDynamicApis(ent, pureApiControllerBaseType); //MVC den önce configure(DefaultRegisterMethod); }
public static void RegisterTypes(Api_Models_Assemblies ent, Func<IEnumerable<Type>, ICachedMetaData> providerFunc) { if (null != ent && null != providerFunc) { List<Type> cachedTypesList = new List<Type>(16); foreach (Type modelType in ent.ModelsTypes) { if (!modelType.IsAbstract) { var cmdAttr = modelType.GetCustomAttribute(CacheMetaDataAttributeType); if (null != cmdAttr) { cachedTypesList.Add(modelType); } } } metaData = providerFunc(cachedTypesList); } }
//Api Controlller isim Pattern i: "Api_{EntityName}Controller" private static void CreateDynamicApis(Api_Models_Assemblies ent, Type pureApiControllerBaseType) { //Elle Yazılmış Api ler. Dictionary<string, Type> userApis = new Dictionary<string, Type>(); Type baseApiControllerType = typeof(ApiController); foreach (Type type in ent.ApiTypes)//Standart Elle Yazılmış Api ler burada olmalı. { if (baseApiControllerType.IsAssignableFrom(type))//Elle Yazılmış api controller lar yükleniyor. Eğer Var ise sonradan custumize edilmiş ise biz yazmayacağız ek olarak. { if (!type.IsAbstract) { userApis.Add(type.Name, type); // RouteApi(routes, type); //WebApiConfig için değiştirildi. Bu Eklendiği için pasifleştirildi. } } } // const string dynamicAssemblyName = "ApiGenericControllers"; //Dinamik Assembly Oluşturuluyor. AssemblyName asmName = new AssemblyName(dynamicAssemblyName); AssemblyBuilder asmBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.Run); ModuleBuilder moduleBuilder = asmBuilder.DefineDynamicModule("core"); // HashSet<string> apiNames = new HashSet<string>();//Api Name GenerateApiControllerAttribute.Name ile mükrerrer eklenecek kontroller için eklendi. foreach (Type type in ent.ModelsTypes)//Burada Model Üzerinde GenerateApiControllerAttribute ile etiketlenmiş Model tipleri için //Dinamik Oluşturulacak REST Api ler olduğundan Model Tiplerine Bakılacak. { var att = type.GetCustomAttribute<GenerateApiControllerAttribute>(); if (att != null)//Oluşturulacaksa { string apiName = !String.IsNullOrEmpty(att.Name) ? att.Name : type.Name; // Sadece ram de oluşan assembly string proxyTypeName = String.Format("{0}Controller", apiName);//İşte Api Oluşturma Şablonu bu. Tablo adı + Api if (!userApis.ContainsKey(proxyTypeName))//Yani Developer aynı şekilde implement etmediyse. { if (!apiNames.Contains(proxyTypeName))//Örneğin GenerateApiControllerAttribute.Name aynı isimde iki Model Tip belirtilmiş ise. { Type genericType = pureApiControllerBaseType.MakeGenericType(type); TypeBuilder typeBuilder = moduleBuilder.DefineType( dynamicAssemblyName + "." + proxyTypeName, TypeAttributes.Public); typeBuilder.SetParent(genericType); typeBuilder.CreateType();//Bu Olmaz ise Tip Oluşturulmaz. //Type dynamicApiControllerType = typeBuilder.CreateType(); // RouteApi(routes, dynamicApiControllerType); //WebApiConfig için değiştirildi apiNames.Add(proxyTypeName); } else throw new InvalidOperationException(String.Format("Auto Generated Api Contoller conflicts GenerateApiController.Name. Name: {0}", proxyTypeName)); } else throw new InvalidOperationException(String.Format("User Defined Api Contoller conflicts GenerateApiController. Type: {0}", proxyTypeName)); } } _DynamicApiAssembly = asmBuilder; }