AR-131 Applied patch Dave Godfrey
[castle.git] / ActiveRecord / Castle.ActiveRecord / Attributes / BelongsToAttribute.cs
blobf6b3ff914ccafb5bbe5797c14f36e25b73cda19b
1 // Copyright 2004-2007 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 /// Define how broken relations should be handled.
21 /// </summary>
22 [Serializable]
23 public enum NotFoundBehaviour
25 /// <summary>
26 /// Throw an exception when the relation is broken.
27 /// </summary>
28 Default,
30 /// <summary>
31 /// Throw an exception when the relation is broken.
32 /// </summary>
33 /// <remarks>this is the default behaviour</remarks>
34 Exception,
36 /// <summary>
37 /// Ignore the broken relation and update
38 /// the FK to null on the next save.
39 /// </summary>
40 Ignore
43 /// <summary>
44 /// Maps a one to one association.
45 /// </summary>
46 /// <example>
47 /// <code>
48 /// public class Post : ActiveRecordBase
49 /// {
50 /// ...
51 ///
52 /// [BelongsTo("blogid")]
53 /// public Blog Blog
54 /// {
55 /// get { return _blog; }
56 /// set { _blog = value; }
57 /// }
58 /// </code>
59 /// </example>
60 /// <remarks>
61 /// Please note that the 'blogid' foreign key lies on the 'Post' table.
62 /// </remarks>
63 [AttributeUsage(AttributeTargets.Property, AllowMultiple=false), Serializable]
64 public class BelongsToAttribute : WithAccessAttribute
66 private Type type;
67 private String column;
68 private String[] compositeKeyColumns;
69 private bool update = true;
70 private bool insert = true;
71 private bool notnull;
72 private bool unique;
73 private FetchEnum fetchMethod = FetchEnum.Unspecified;
74 private CascadeEnum cascade = CascadeEnum.None;
75 private NotFoundBehaviour notFoundBehaviour = NotFoundBehaviour.Default;
77 /// <summary>
78 /// Initializes a new instance of the <see cref="BelongsToAttribute"/> class.
79 /// </summary>
80 public BelongsToAttribute()
84 /// <summary>
85 /// Indicates the name of the column to be used on the association.
86 /// Usually the name of the foreign key field on the underlying database.
87 /// </summary>
88 public BelongsToAttribute(String column)
90 this.column = column;
93 /// <summary>
94 /// Defines the target type of the association. It's usually inferred from the property type.
95 /// </summary>
96 public Type Type
98 get { return type; }
99 set { type = value; }
102 /// <summary>
103 /// Defines the column used by association (usually a foreign key)
104 /// </summary>
105 public String Column
107 get { return column; }
108 set { column = value; }
111 /// <summary>
112 /// Defines the Composite Key columns used by association (aka Natural Keys).
113 /// </summary>
114 public String[] CompositeKeyColumns
116 get { return compositeKeyColumns; }
117 set { compositeKeyColumns = value; }
120 /// <summary>
121 /// Defines the cascading behavior of this association.
122 /// </summary>
123 public CascadeEnum Cascade
125 get { return cascade; }
126 set { cascade = value; }
129 /// <summary>
130 /// Defines the outer join behavior of this association.
131 /// NHibernate has deprecated the outer-join attribute so this property is
132 /// marked obsolete - it now converts to and from the fetch value.
133 /// </summary>
134 [Obsolete("Use the Fetch property instead")]
135 public OuterJoinEnum OuterJoin
139 OuterJoinEnum returnValue = OuterJoinEnum.Auto;
141 switch (fetchMethod)
143 case FetchEnum.Unspecified:
144 returnValue = OuterJoinEnum.Auto;
145 break;
146 case FetchEnum.Join:
147 returnValue = OuterJoinEnum.True;
148 break;
149 case FetchEnum.Select:
150 returnValue = OuterJoinEnum.False;
151 break;
154 return returnValue;
158 switch (value)
160 case OuterJoinEnum.Auto:
161 fetchMethod = FetchEnum.Unspecified;
162 break;
163 case OuterJoinEnum.True:
164 fetchMethod = FetchEnum.Join;
165 break;
166 case OuterJoinEnum.False:
167 fetchMethod = FetchEnum.Select;
168 break;
173 /// <summary>
174 /// Chooses between outer-join fetching
175 /// or sequential select fetching.
176 /// </summary>
177 public FetchEnum Fetch
179 get { return fetchMethod; }
180 set { fetchMethod = value; }
183 /// <summary>
184 /// Set to <c>false</c> to ignore this association when updating entities of this ActiveRecord class.
185 /// </summary>
186 public bool Update
188 get { return update; }
189 set { update = value; }
192 /// <summary>
193 /// Set to <c>false</c> to ignore this association when inserting entities of this ActiveRecord class.
194 /// </summary>
195 public bool Insert
197 get { return insert; }
198 set { insert = value; }
201 /// <summary>
202 /// Indicates whether this association allows nulls or not.
203 /// </summary>
204 public bool NotNull
206 get { return notnull; }
207 set { notnull = value; }
210 /// <summary>
211 /// Indicates whether this association is unique.
212 /// </summary>
213 public bool Unique
215 get { return unique; }
216 set { unique = value; }
219 /// <summary>
220 /// Gets or sets the way broken relations are handled.
221 /// </summary>
222 /// <value>The behaviour.</value>
223 public NotFoundBehaviour NotFoundBehaviour
225 get { return notFoundBehaviour; }
226 set { notFoundBehaviour = value; }