Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / ActiveRecord / Castle.ActiveRecord / Attributes / AnyAttribute.cs
blob44e4480644b4bd7e73c98fd34591738427f453f1
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.ActiveRecord
17 using System;
19 /// <summary>
20 /// This attribute is used to create &lt;any/&gt; assoication, a polymorphic assoication to classes that
21 /// do not share a common base class.
22 /// <example>
23 /// Assuming we have two classes that implement IPayment, CreditCard and BankAccount, and we want a property
24 /// that can point ot either one of them. We can map it like this:
25 /// <code>
26 /// [Any(typeof (long), MetaType=typeof (string),
27 /// TypeColumn="BILLING_DETAILS_TYPE",
28 /// IdColumn="BILLING_DETAILS_ID",
29 /// Cascade=CascadeEnum.SaveUpdate)]
30 /// [Any.MetaValue("CREDIT_CARD", typeof (CreditCard))]
31 /// [Any.MetaValue("BANK_ACCOUNT", typeof (BankAccount))]
32 /// public IPayment Payment { get { ... } set { ... } }
33 /// </code>
34 /// The [Any] attribute specify that the id type is long, that the meta type (the type that specify the type of
35 /// the class) is string.
36 /// The TypeColumn = "BILLING_DETAILS_TYPE" means that Active Record will look in this column to figure out what the type
37 /// of the associated entity is.
38 /// The IdColumn = "BILLING_DETAILS_ID" means that Active Record will use this column in conjuction with the type of the entity
39 /// to find the relevant entity. This is the id of the associated entity (which can point to either back account or credit card).
40 /// Cascade has the usual semantics.
41 ///
42 /// [Any.MetaValue("CREDIT_CARD", typeof (CreditCard))] - means that when Active Record encounters a "CREDIT_CARD" value in
43 /// the "BILLING_DETAILS_TYPE", is assumes that the id in the "BILLING_DETAILS_ID" is the id of a CreditCard entity.
44 ///
45 /// [Any.MetaValue("BANK_ACCOUNT", typeof (BankAccount))] - same, just for "BANK_ACCOUNT" meaning that the id in "BILLING_DETAILS_ID"
46 /// is an id of a bank account.
47 /// </example>
48 /// </summary>
49 /// <remarks>
50 /// This is supplied for advanced sceanrios.
51 /// </remarks>
52 [AttributeUsage(AttributeTargets.Property, AllowMultiple=false), Serializable]
53 public class AnyAttribute : WithAccessAttribute
55 private CascadeEnum cascade;
56 private Type idType;
57 private Type metaType;
58 private string typeColumn, idColumn;
59 private string index;
60 private bool insert = true, update = true;
61 private bool notNull = false;
63 /// <summary>
64 /// Initializes a new instance of the <see cref="AnyAttribute"/> class.
65 /// </summary>
66 /// <remarks>Using this constructor defaults the idType to Int32</remarks>
67 public AnyAttribute()
68 : this(typeof(Int32))
72 /// <summary>
73 /// Initializes a new instance of the <see cref="AnyAttribute"/> class.
74 /// </summary>
75 /// <param name="idType">Type of the id.</param>
76 public AnyAttribute(Type idType)
78 this.idType = idType;
82 /// <summary>
83 /// Gets or sets the type of the id.
84 /// </summary>
85 /// <value>The type of the id.</value>
86 public Type IdType
88 get { return idType; }
89 set { idType = value; }
92 /// <summary>
93 /// Gets or sets the type of the meta column
94 /// </summary>
95 /// <value>The type of the meta.</value>
96 public Type MetaType
98 get { return metaType; }
99 set { metaType = value; }
102 /// <summary>
103 /// Gets or sets the cascade options
104 /// </summary>
105 /// <value>The cascade.</value>
106 public CascadeEnum Cascade
108 get { return cascade; }
109 set { cascade = value; }
112 /// <summary>
113 /// Gets or sets the type column name
114 /// </summary>
115 /// <value>The type column.</value>
116 public string TypeColumn
118 get { return typeColumn; }
119 set { typeColumn = value; }
122 /// <summary>
123 /// Gets or sets the id column name
124 /// </summary>
125 /// <value>The id column.</value>
126 public string IdColumn
128 get { return idColumn; }
129 set { idColumn = value; }
132 /// <summary>
133 /// Gets or sets the index column name
134 /// </summary>
135 /// <value>The index.</value>
136 public string Index
138 get { return index; }
139 set { index = value; }
142 /// <summary>
143 /// Gets or sets a value indicating whether the column should be inserted when inserting.
144 /// </summary>
145 /// <value><c>true</c> if should insert; otherwise, <c>false</c>.</value>
146 public bool Insert
148 get { return insert; }
149 set { insert = value; }
152 /// <summary>
153 /// Gets or sets a value indicating whether the column should be is updated when updating.
154 /// </summary>
155 /// <value><c>true</c> if should update; otherwise, <c>false</c>.</value>
156 public bool Update
158 get { return update; }
159 set { update = value; }
163 /// <summary>
164 /// Gets or sets a value indicating whether this property cannot be null.
165 /// </summary>
166 /// <value><c>true</c> if this property cannot be null; otherwise, <c>false</c>.</value>
167 public bool NotNull
169 get { return notNull; }
170 set { notNull = value; }
174 /// <summary>
175 /// Avoids the AnyAttribute.MetaValue syntax
176 /// </summary>
177 public class Any
179 /// <summary>
180 /// This is used to specify a meta value in an [Any] assoication
181 /// Any.MetaValue is used to connect a value (such as "CREDIT_CARD") to its type ( typeof(CreditCard) ).
182 /// </summary>
183 [AttributeUsage(AttributeTargets.Property, AllowMultiple=true), Serializable]
184 public class MetaValueAttribute : Attribute, IComparable
186 private string value;
187 private Type clazz;
189 /// <summary>
190 /// Initializes a new instance of the <see cref="MetaValueAttribute"/> class.
191 /// </summary>
192 /// <param name="value">The value.</param>
193 /// <param name="clazz">The clazz.</param>
194 public MetaValueAttribute(string value, Type clazz)
196 this.value = value;
197 this.clazz = clazz;
200 /// <summary>
201 /// Gets or sets the value for this class
202 /// </summary>
203 /// <value>The value.</value>
204 public string Value
206 get { return value; }
207 set { this.value = value; }
210 /// <summary>
211 /// Gets or sets the class that match this value
212 /// </summary>
213 /// <value>The class.</value>
214 public Type Class
216 get { return clazz; }
217 set { clazz = value; }
220 /// <summary>
221 /// This is here so the XmlGenerationVisitor will always
222 /// output the meta-values in consistent order, to aid the tests.
223 /// </summary>
224 int IComparable.CompareTo(object obj)
226 MetaValueAttribute other = (MetaValueAttribute)obj;
227 return Class.FullName.CompareTo(other.Class.FullName);