Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / ActiveRecord / Castle.ActiveRecord / Attributes / OneToOneAttribute.cs
blob64b74382ca9d6e1edec696d5f64aa97c3b046616
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 /// 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;
109 private string foreignKey;
111 /// <summary>
112 /// Allows one to reference a different type
113 /// than the property type
114 /// </summary>
115 public Type MapType
117 get { return mapType; }
118 set { mapType = value; }
121 /// <summary>
122 /// From NHibernate docs: specifies which operations should be
123 /// cascaded from the parent object to the associated object.
124 /// </summary>
125 public CascadeEnum Cascade
127 get { return cascade; }
128 set { cascade = value; }
131 /// <summary>
132 /// From NHibernate docs: Chooses between outer-join fetching
133 /// or sequential select fetching.
134 /// </summary>
135 /// <remarks>
136 /// Defaults to <see cref="FetchEnum.Select"/>
137 /// </remarks>
138 public FetchEnum Fetch
140 get { return fetch; }
141 set { fetch = value; }
144 /// <summary>
145 /// From NHibernate docs: The name of a property of the
146 /// associated class that is joined to the primary key
147 /// of this class. If not specified, the primary key of
148 /// the associated class is used.
149 /// </summary>
150 public string PropertyRef
152 get { return propertyRef; }
153 set { propertyRef = value; }
156 /// <summary>
157 /// From NHibernate docs: specifies that a foreign key
158 /// constraint on the primary key of the mapped table
159 /// references the table of the associated class.
160 /// This option affects the order in which Save() and
161 /// Delete() are cascaded (and is also used by the
162 /// schema export tool).
163 /// </summary>
164 public bool Constrained
166 get { return constrained; }
167 set { constrained = value; }
170 /// <summary>
171 /// Gets or sets the name of the foreign key constraint generated for
172 /// an association. NHibernate will only use the ForeignKey name one
173 /// the inherited class and Constrained = true.
174 /// </summary>
175 public string ForeignKey
177 get { return foreignKey; }
178 set { foreignKey = value; }