Db Attribute(Custom ORM ) Usage
来源:互联网 发布:易分销 源码 编辑:程序博客网 时间:2024/05/29 06:30
Db Attribute Usage
1. Attribute Defination
Db
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = true)] public class DataBaseAttribute : System.Attribute { public string Name { get; private set; } public DataBaseAttribute(string name) { Name = name; } }
DataTable
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = true)] public class DataTableAttribute : System.Attribute { public string Name { get; private set; } public DataTableAttribute(string name) { Name = name; } }
Primary Key
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false,Inherited = false)] public class PrimaryKeyAttribute : System.Attribute { public bool IsAutoIncreased { get; private set; } public string Name { get; private set; } public PrimaryKeyAttribute(string name,bool isAutoIncreased) { IsAutoIncreased = isAutoIncreased; Name = name; } }
2. Use Attribute to Entity
Db
[DataBase("northwnd")] public class IoriRepository<T> : IRepository<T> where T : new()
DataTable And Primary Key
[DataTable("Products")] public class ProductEntity { [PrimaryKey("ProductId",true)] public int ProductId { get; set; } public string ProductName { get; set; } public int SupplierId { get; set; } public int CategoryId { get; set; } public string QuantityPerUnit { get; set; } public decimal UnitPrice { get; set; } public Int16 UnitsInStock { get; set; } public Int16 UnitsOnOrder { get; set; } public Int16 ReorderLevel { get; set; } public bool Discontinued { get; set; } }
3. Get Attribute Implementation
public static class AttributeHelper { public static TAttrType GetFirstAttr<TAttrType, TClassType>(bool isInherit) where TAttrType : System.Attribute { var attrs = (TAttrType[])(typeof(TClassType).GetCustomAttributes(typeof(TAttrType), isInherit)); return attrs.FirstOrDefault(); } }
////get db name var dbAttr = AttributeHelper.GetFirstAttr<DataBaseAttribute, IoriRepository<T>>(false); if (dbAttr == null) throw new Exception("failed to get Database name , check if you forgot to set Database Attribute for Repository<T>");
Similarly , Can use the same method to get DataTable Attribute And Primary Key Attribute
- Db Attribute(Custom ORM ) Usage
- 用attribute实现ORM
- orm db config
- Berkeley DB——Usage
- Custom usage of %w in Ruby
- Android ORM DB使用心得
- ORM DB for nodejs--sequelize
- .NET Custom Attribute Learning and Practice
- [Magento] Add custom attribute by SQL
- 自己动手打造ORM(三) —自定义Attribute
- D2010 RTTI + Attribute 简单实现ORM
- D2010 RTTI + Attribute 简单实现ORM
- D2010 RTTI + Attribute 简单实现ORM
- Delphi2010 RTTI + Attribute 简单实现ORM实例
- android---pull解析<db:attribute name="isbn10">7111337271</db:attribute>
- DB Usage Complete / ADB 用法大全(转自gitHub)
- 给ADF BC EO Row或Attribute添加Custom异常
- Part 82 - Creating custom validation attribute in asp.net mvc
- mybatis 学习2
- IOS之Delegate
- 查看linux是32位还是64位
- linux 命令整理
- extjs4 appendChild 添加节点后不显示问题的解决
- Db Attribute(Custom ORM ) Usage
- 分层架构下的纯JDBC事务控制简单解决方案
- ffmpeg源代码简单分析 :avcodec_decode_video2()
- android中用Spannable在TextView中设置超链接、颜色、字体
- Ubuntu的IP地址设置和查看
- 另一个随机数
- MFC双击按钮之后出现“cannot add new member”
- BeanUtils
- ActionBarSherlock+SlidingMenu打造侧边栏