Attributes.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Diagnostics.CodeAnalysis;
  5. namespace System.Data.Linq.Mapping {
  6. /// <summary>
  7. /// Attribute placed on a method mapped to a User Defined Function.
  8. /// </summary>
  9. [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
  10. public sealed class FunctionAttribute : Attribute {
  11. string name;
  12. bool isComposable;
  13. public FunctionAttribute() {
  14. }
  15. public string Name {
  16. get { return this.name; }
  17. set { this.name = value; }
  18. }
  19. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Composable", Justification="Spelling is correct.")]
  20. public bool IsComposable {
  21. get { return this.isComposable; }
  22. set { this.isComposable = value; }
  23. }
  24. }
  25. /// <summary>
  26. /// This attribute is applied to functions returning multiple result types,
  27. /// to declare the possible result types returned from the function. For
  28. /// inheritance types, only the root type of the inheritance hierarchy need
  29. /// be specified.
  30. /// </summary>
  31. [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
  32. public sealed class ResultTypeAttribute : Attribute {
  33. Type type;
  34. public ResultTypeAttribute(Type type) {
  35. this.type = type;
  36. }
  37. [SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods", Justification = "The contexts in which this is available are fairly specific.")]
  38. public Type Type {
  39. get { return this.type; }
  40. }
  41. }
  42. [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.ReturnValue, AllowMultiple = false)]
  43. public sealed class ParameterAttribute : Attribute {
  44. string name;
  45. string dbType;
  46. public ParameterAttribute() {
  47. }
  48. public string Name {
  49. get { return this.name; }
  50. set { this.name = value; }
  51. }
  52. [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Db", Justification = "Conforms to legacy spelling.")]
  53. public string DbType {
  54. get { return this.dbType; }
  55. set { this.dbType = value; }
  56. }
  57. }
  58. [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
  59. public sealed class DatabaseAttribute : Attribute {
  60. string name;
  61. public DatabaseAttribute() {
  62. }
  63. public string Name {
  64. get { return this.name; }
  65. set { this.name = value; }
  66. }
  67. }
  68. [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
  69. public sealed class TableAttribute : Attribute {
  70. string name;
  71. public TableAttribute() {
  72. }
  73. public string Name {
  74. get { return this.name; }
  75. set { this.name = value; }
  76. }
  77. }
  78. /// <summary>
  79. /// Class attribute used to describe an inheritance hierarchy to be mapped.
  80. /// For example,
  81. ///
  82. /// [Table(Name = "People")]
  83. /// [InheritanceMapping(Code = "P", Type = typeof(Person), IsDefault=true)]
  84. /// [InheritanceMapping(Code = "C", Type = typeof(Customer))]
  85. /// [InheritanceMapping(Code = "E", Type = typeof(Employee))]
  86. /// class Person { ... }
  87. ///
  88. /// </summary>
  89. [AttributeUsage(AttributeTargets.Class, AllowMultiple=true, Inherited = false)]
  90. public sealed class InheritanceMappingAttribute : Attribute {
  91. private object code;
  92. private Type type;
  93. private bool isDefault;
  94. /// <summary>
  95. /// Discriminator value in store column for this type.
  96. /// </summary>
  97. public object Code {
  98. get { return this.code; }
  99. set { this.code = value; }
  100. }
  101. /// <summary>
  102. /// Type to instantiate when Key is matched.
  103. /// </summary>
  104. [SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods", Justification="The contexts in which this is available are fairly specific.")]
  105. public Type Type {
  106. get { return this.type; }
  107. set { this.type = value; }
  108. }
  109. /// <summary>
  110. /// If discriminator value in store column is unrecognized then instantiate this type.
  111. /// </summary>
  112. public bool IsDefault {
  113. get { return this.isDefault; }
  114. set { this.isDefault = value; }
  115. }
  116. }
  117. public abstract class DataAttribute : Attribute {
  118. string name;
  119. string storage;
  120. protected DataAttribute() { }
  121. public string Name {
  122. get { return this.name; }
  123. set { name = value; }
  124. }
  125. public string Storage {
  126. get { return this.storage; }
  127. set { this.storage = value; }
  128. }
  129. }
  130. public enum UpdateCheck {
  131. Always,
  132. Never,
  133. WhenChanged
  134. }
  135. /// <summary>
  136. /// Used to specify for during insert and update operations when
  137. /// a data member should be read back after the operation completes.
  138. /// </summary>
  139. public enum AutoSync {
  140. Default = 0, // Automatically choose
  141. Always = 1,
  142. Never = 2,
  143. OnInsert = 3,
  144. OnUpdate = 4
  145. }
  146. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
  147. public sealed class ColumnAttribute : DataAttribute {
  148. string dbtype;
  149. string expression;
  150. bool isPrimaryKey;
  151. bool isDBGenerated;
  152. bool isVersion;
  153. bool isDiscriminator;
  154. bool canBeNull = true;
  155. UpdateCheck check;
  156. AutoSync autoSync = AutoSync.Default;
  157. bool canBeNullSet = false;
  158. public ColumnAttribute() {
  159. check = UpdateCheck.Always;
  160. }
  161. [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Db", Justification="Conforms to legacy spelling.")]
  162. public string DbType {
  163. get { return this.dbtype; }
  164. set { this.dbtype = value; }
  165. }
  166. public string Expression {
  167. get { return this.expression; }
  168. set { this.expression = value; }
  169. }
  170. public bool IsPrimaryKey {
  171. get { return this.isPrimaryKey; }
  172. set { this.isPrimaryKey = value; }
  173. }
  174. [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Db", Justification = "Conforms to legacy spelling.")]
  175. public bool IsDbGenerated {
  176. get { return this.isDBGenerated; }
  177. set { this.isDBGenerated = value; }
  178. }
  179. public bool IsVersion {
  180. get { return this.isVersion; }
  181. set { this.isVersion = value; }
  182. }
  183. public UpdateCheck UpdateCheck {
  184. get { return this.check; }
  185. set { this.check = value; }
  186. }
  187. public AutoSync AutoSync {
  188. get { return this.autoSync; }
  189. set { this.autoSync = value; }
  190. }
  191. public bool IsDiscriminator {
  192. get { return this.isDiscriminator; }
  193. set { isDiscriminator = value; }
  194. }
  195. public bool CanBeNull {
  196. get {return this.canBeNull;}
  197. set {
  198. this.canBeNullSet = true;
  199. this.canBeNull = value;
  200. }
  201. }
  202. internal bool CanBeNullSet {
  203. get {return this.canBeNullSet;}
  204. }
  205. }
  206. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
  207. public sealed class AssociationAttribute : DataAttribute {
  208. string thisKey;
  209. string otherKey;
  210. bool isUnique;
  211. bool isForeignKey;
  212. bool deleteOnNull;
  213. string deleteRule;
  214. public AssociationAttribute() { }
  215. public string ThisKey {
  216. get { return this.thisKey; }
  217. set { this.thisKey = value; }
  218. }
  219. public string OtherKey {
  220. get { return this.otherKey; }
  221. set { this.otherKey = value; }
  222. }
  223. public bool IsUnique {
  224. get { return this.isUnique; }
  225. set { this.isUnique = value; }
  226. }
  227. public bool IsForeignKey {
  228. get { return this.isForeignKey; }
  229. set { this.isForeignKey = value; }
  230. }
  231. public string DeleteRule {
  232. get { return this.deleteRule; }
  233. set { this.deleteRule = value; }
  234. }
  235. public bool DeleteOnNull {
  236. get { return this.deleteOnNull; }
  237. set { this.deleteOnNull = value; }
  238. }
  239. }
  240. [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
  241. public sealed class ProviderAttribute : Attribute {
  242. Type providerType;
  243. public ProviderAttribute() {
  244. }
  245. public ProviderAttribute(Type type) {
  246. this.providerType = type;
  247. }
  248. [SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods", Justification = "The contexts in which this is available are fairly specific.")]
  249. public Type Type {
  250. get { return this.providerType; }
  251. }
  252. }
  253. }