AR-131 Applied patch Dave Godfrey
[castle.git] / ActiveRecord / Castle.ActiveRecord / Attributes / OneToOneAttribute.cs
blob3b660baf562d482689f23e4cd7c98f5653cc5fda
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 /// Associates a foreign table where the current class
21 /// and the target class share their primary key.
22 /// </summary>
23 /// <example>
24 /// The following code exemplifies two classes that maps
25 /// to two tables sharing the primary key:
26 /// <code>
27 /// [ActiveRecord("Employee")]
28 /// public class Employee : ActiveRecordBase
29 /// {
30 /// private int id;
31 /// private Award award;
32 ///
33 /// [PrimaryKey(PrimaryKeyType.Native, "EmployeeID")]
34 /// public int ID
35 /// {
36 /// get { return this.id; }
37 /// set { this.id = value; }
38 /// }
39 ///
40 /// [OneToOne]
41 /// public Award Award
42 /// {
43 /// get { return this.award; }
44 /// set { this.award = value; }
45 /// }
46 /// }
47 ///
48 /// [ActiveRecord("Award")]
49 /// public class Award : ActiveRecordBase
50 /// {
51 /// private Employee employee;
52 /// private int id;
53 ///
54 /// public Award()
55 /// {
56 /// }
57 ///
58 /// public Award(Employee employee)
59 /// {
60 /// this.employee = employee;
61 /// }
62 ///
63 /// [OneToOne]
64 /// public Employee Employee
65 /// {
66 /// get { return this.employee; }
67 /// set { this.employee = value; }
68 /// }
69 ///
70 /// [PrimaryKey(PrimaryKeyType.Foreign, "EmployeeID")]
71 /// public int ID
72 /// {
73 /// get { return this.id; }
74 /// set { this.id = value; }
75 /// }
76 ///
77 /// public static Award[] FindAll()
78 /// {
79 /// return ((Award[]) (ActiveRecordBase.FindAll(typeof(Award))));
80 /// }
81 ///
82 /// public static void DeleteAll()
83 /// {
84 /// ActiveRecordBase.DeleteAll( typeof(Award) );
85 /// }
86 /// }
87 /// Employee emp = new Employee();
88 /// emp.Name = "john doe";
89 /// emp.Save();
90 ///
91 /// Award award = new Award(emp);
92 /// award.Description = "Invisible employee";
93 /// award.Save();
94 /// </code>
95 /// </example>
96 /// <remarks>
97 /// Usually classes that uses the primary key
98 /// generated elsewhere (foreign) uses the PrimaryKey attribute with the
99 /// generator type <c>PrimaryKeyType.Foreign</c>
100 /// </remarks>
101 [AttributeUsage(AttributeTargets.Property, AllowMultiple=false), Serializable]
102 public class OneToOneAttribute : WithAccessAttribute
104 private CascadeEnum cascade = CascadeEnum.None;
105 private FetchEnum fetch = FetchEnum.Unspecified;
106 private String propertyRef;
107 private Type mapType;
108 private bool constrained;
110 /// <summary>
111 /// Allows one to reference a different type
112 /// than the property type
113 /// </summary>
114 public Type MapType
116 get { return mapType; }
117 set { mapType = value; }
120 /// <summary>
121 /// From NHibernate docs: specifies which operations should be
122 /// cascaded from the parent object to the associated object.
123 /// </summary>
124 public CascadeEnum Cascade
126 get { return cascade; }
127 set { cascade = value; }
130 /// <summary>
131 /// From NHibernate docs: Chooses between outer-join fetching
132 /// or sequential select fetching.
133 /// </summary>
134 /// <remarks>
135 /// Defaults to <see cref="FetchEnum.Select"/>
136 /// </remarks>
137 public FetchEnum Fetch
139 get { return fetch; }
140 set { fetch = value; }
143 /// <summary>
144 /// From NHibernate docs: The name of a property of the
145 /// associated class that is joined to the primary key
146 /// of this class. If not specified, the primary key of
147 /// the associated class is used.
148 /// </summary>
149 public string PropertyRef
151 get { return propertyRef; }
152 set { propertyRef = value; }
155 /// <summary>
156 /// From NHibernate docs: specifies that a foreign key
157 /// constraint on the primary key of the mapped table
158 /// references the table of the associated class.
159 /// This option affects the order in which Save() and
160 /// Delete() are cascaded (and is also used by the
161 /// schema export tool).
162 /// </summary>
163 public bool Constrained
165 get { return constrained; }
166 set { constrained = value; }