/// <summary> /// Adds the configuration to the Configuration cache. If there is an existing configuration with the same name it will be overwritten. /// </summary> /// <param name="configuration"></param> public static void Register(DataTablesConfiguration <T> configuration) { lock (_configurationCacheLock) { string cacheKey = configuration.Name ?? ""; _configurationCache[cacheKey] = configuration; } }
/// <summary> /// Creates a Configuration by analyzing the generic Type parameter /// </summary> /// <param name="configurationName"></param> /// <returns></returns> public static DataTablesConfiguration <T> Create(string configurationName) { Type baseType = typeof(T); //Check if there is any configuration attribute with the specified configuration name //if there is not, we return early bool isDefined = baseType.CustomAttributes.OfType <DataTablesAttribute>() .Concat(baseType.GetMembers().SelectMany(m => m.GetCustomAttributes(true) .OfType <DataTablesAttribute>())) .Any(attr => attr.ConfigurationName == configurationName); if (!isDefined) { return(null); } var dtAttribute = baseType.GetCustomAttributes(true).OfType <DataTablesAttribute>()//.FirstOrDefault(); .FirstOrDefault(attr => attr.ConfigurationName == configurationName); DataTablesConfiguration <T> configuration = new DataTablesConfiguration <T>(); configuration.Name = configurationName; configuration.Table = dtAttribute?.Table ?? baseType.Name; var propMembers = baseType.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance) .Select(p => new { p.Name, Type = p.PropertyType, CustomAttributes = p.GetCustomAttributes(true) }); var fieldMembers = baseType.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance) .Select(f => new { f.Name, Type = f.FieldType, CustomAttributes = f.GetCustomAttributes(true) }); var propAndFields = propMembers.Concat(fieldMembers); foreach (var member in propAndFields) { //if the member is annotated with the IgnoreDataTablesMapping attribute then we skip it if (member.CustomAttributes.OfType <IgnoreDataTablesMappingAttribute>().Any(attr => attr.ConfigurationName == configurationName || attr.IgnoreAlways)) { continue; } DataTablesColumnConfiguration colConfiguration = new DataTablesColumnConfiguration(); var colDtAttribute = member.CustomAttributes.OfType <DataTablesAttribute>().FirstOrDefault(attr => attr.ConfigurationName == configurationName); colConfiguration.Field = member.Name; colConfiguration.Column = colDtAttribute?.Column ?? member.Name; colConfiguration.Title = colDtAttribute?.Title ?? Utils.StringExtensions.SplitUppercase(member.Name); colConfiguration.Sortable = colDtAttribute?.Sortable ?? false; colConfiguration.Searchable = colDtAttribute?.Searchable ?? false; colConfiguration.GloballySearchable = colDtAttribute?.GloballySearchable ?? false; colConfiguration.Visible = colDtAttribute?.Visible ?? true; colConfiguration.Type = member.Type; configuration.Columns.Add(colConfiguration); } return(configuration); }
/// <summary> /// Gets a configuration for the given name from the cache. If the configuration is not found, /// then a configuration is created and added to the cache, then returned to the user. /// </summary> /// <param name="configurationName"></param> /// <returns></returns> public static DataTablesConfiguration <T> Get(string configurationName) { DataTablesConfiguration <T> config = null; bool configExists = false; lock (_configurationCacheLock) { configExists = _configurationCache.TryGetValue(configurationName, out config) && config != null; if (!configExists) { config = Create(configurationName); _configurationCache[configurationName] = config; } } return(config); }
/// <summary> /// Removes the specified configuration from the cache /// </summary> /// <param name="configuration"></param> public static void Remove(DataTablesConfiguration <T> configuration) { Remove(configuration.Name); }