1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
20 /// This attribute is used to create <any/> assoication, a polymorphic assoication to classes that
21 /// do not share a common base class.
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:
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 { ... } }
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.
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.
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.
50 /// This is supplied for advanced sceanrios.
52 [AttributeUsage(AttributeTargets
.Property
, AllowMultiple
=false), Serializable
]
53 public class AnyAttribute
: WithAccessAttribute
55 private CascadeEnum cascade
;
57 private Type metaType
;
58 private string typeColumn
, idColumn
;
60 private bool insert
= true, update
= true;
61 private bool notNull
= false;
64 /// Initializes a new instance of the <see cref="AnyAttribute"/> class.
66 /// <remarks>Using this constructor defaults the idType to Int32</remarks>
73 /// Initializes a new instance of the <see cref="AnyAttribute"/> class.
75 /// <param name="idType">Type of the id.</param>
76 public AnyAttribute(Type idType
)
83 /// Gets or sets the type of the id.
85 /// <value>The type of the id.</value>
88 get { return idType; }
89 set { idType = value; }
93 /// Gets or sets the type of the meta column
95 /// <value>The type of the meta.</value>
98 get { return metaType; }
99 set { metaType = value; }
103 /// Gets or sets the cascade options
105 /// <value>The cascade.</value>
106 public CascadeEnum Cascade
108 get { return cascade; }
109 set { cascade = value; }
113 /// Gets or sets the type column name
115 /// <value>The type column.</value>
116 public string TypeColumn
118 get { return typeColumn; }
119 set { typeColumn = value; }
123 /// Gets or sets the id column name
125 /// <value>The id column.</value>
126 public string IdColumn
128 get { return idColumn; }
129 set { idColumn = value; }
133 /// Gets or sets the index column name
135 /// <value>The index.</value>
138 get { return index; }
139 set { index = value; }
143 /// Gets or sets a value indicating whether the column should be inserted when inserting.
145 /// <value><c>true</c> if should insert; otherwise, <c>false</c>.</value>
148 get { return insert; }
149 set { insert = value; }
153 /// Gets or sets a value indicating whether the column should be is updated when updating.
155 /// <value><c>true</c> if should update; otherwise, <c>false</c>.</value>
158 get { return update; }
159 set { update = value; }
164 /// Gets or sets a value indicating whether this property cannot be null.
166 /// <value><c>true</c> if this property cannot be null; otherwise, <c>false</c>.</value>
169 get { return notNull; }
170 set { notNull = value; }
175 /// Avoids the AnyAttribute.MetaValue syntax
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) ).
183 [AttributeUsage(AttributeTargets
.Property
, AllowMultiple
=true), Serializable
]
184 public class MetaValueAttribute
: Attribute
, IComparable
186 private string value;
190 /// Initializes a new instance of the <see cref="MetaValueAttribute"/> class.
192 /// <param name="value">The value.</param>
193 /// <param name="clazz">The clazz.</param>
194 public MetaValueAttribute(string value, Type clazz
)
201 /// Gets or sets the value for this class
203 /// <value>The value.</value>
206 get { return value; }
207 set { this.value = value; }
211 /// Gets or sets the class that match this value
213 /// <value>The class.</value>
216 get { return clazz; }
217 set { clazz = value; }
221 /// This is here so the XmlGenerationVisitor will always
222 /// output the meta-values in consistent order, to aid the tests.
224 int IComparable
.CompareTo(object obj
)
226 MetaValueAttribute other
= (MetaValueAttribute
)obj
;
227 return Class
.FullName
.CompareTo(other
.Class
.FullName
);