using Apewer.Internals; using System; using System.Collections.Generic; using System.Text; namespace Apewer.Source { /// 数据库中的表。 /// /// Name: 数据库的表名。 /// Store: 数据存储区名称。 /// [Serializable] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = true)] public sealed class TableAttribute : Attribute { private string _name = null; private string _store = null; /// 标记表属性。 public TableAttribute(string name = null, string store = null) { _name = name; _store = store; } /// 表名。 public string Name { get => _name; } /// 存储名。 public string Store { get => _store; } /// 表的说明信息。(需要数据库客户端支持) public string Description { get; set; } /// 独立结构,不依赖 Record 公共属性。 internal bool Independent { get; set; } /// 使用模型的所有属性,对缺少 Column 特性的属性使用默认参数的 Column 特性。 public bool AllProperties { get; set; } private static Dictionary _tac = new Dictionary(); /// 解析表特性,默认使用缓存以提升性能。 public static TableAttribute Parse(bool useCache = true) where T: class, new() => Parse(typeof(T), useCache); /// 解析表特性,默认使用缓存以提升性能。 public static TableAttribute Parse(Type type, bool useCache = true) { var cacheKey = type.FullName; if (useCache) { var hint = null as TableAttribute; lock (_tac) { if (_tac.ContainsKey(cacheKey)) { hint = _tac[cacheKey]; } } if (hint != null) return hint; } // throw new Exception($"类型 {type.FullName} 不包含 {typeof(TableAttribute).FullName}。"); var tas = type.GetCustomAttributes(typeof(TableAttribute), false); if (tas.LongLength < 1L) return null; var ta = (TableAttribute)tas[0]; if (string.IsNullOrEmpty(ta.Name)) ta._name = type.Name; ta.Independent = RuntimeUtility.Contains(type, true); if (useCache) { lock (_tac) { if (!_tac.ContainsKey(cacheKey)) _tac.Add(cacheKey, ta); } } return ta; } } }