Revert 132601, 132602 as it made the csharp console stop working
[mono.git] / mcs / class / System.Data.Linq / src / DbLinq / Data / Linq / EntityRef.cs
1 #region MIT license\r
2 //\r
3 // EntityRef.cs\r
4 //\r
5 // Author:\r
6 //   Atsushi Enomoto  <atsushi@ximian.com>\r
7 //   Pablo Íñigo Blasco <pibgeus@gmail.com>\r
8 //\r
9 // Copyright (C) 2008 Novell, Inc.\r
10 //\r
11 \r
12 //\r
13 // Permission is hereby granted, free of charge, to any person obtaining\r
14 // a copy of this software and associated documentation files (the\r
15 // "Software"), to deal in the Software without restriction, including\r
16 // without limitation the rights to use, copy, modify, merge, publish,\r
17 // distribute, sublicense, and/or sell copies of the Software, and to\r
18 // permit persons to whom the Software is furnished to do so, subject to\r
19 // the following conditions:\r
20 // \r
21 // The above copyright notice and this permission notice shall be\r
22 // included in all copies or substantial portions of the Software.\r
23 // \r
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
31 //\r
32 #endregion\r
33 \r
34 using System;\r
35 using System.Collections.Generic;\r
36 using DbLinq;\r
37 \r
38 #if MONO_STRICT\r
39 namespace System.Data.Linq\r
40 #else\r
41 namespace DbLinq.Data.Linq\r
42 #endif\r
43 {\r
44     public struct EntityRef<TEntity> where TEntity : class\r
45     {\r
46         private TEntity entity;\r
47         private bool hasLoadedOrAssignedValue;\r
48         private IEnumerable<TEntity> source;\r
49 \r
50         public EntityRef(TEntity entity)\r
51         {\r
52             this.source = null;\r
53             this.entity = entity;\r
54             hasLoadedOrAssignedValue = true;\r
55         }\r
56 \r
57         public EntityRef(IEnumerable<TEntity> source)\r
58         {\r
59             this.source = source;\r
60             hasLoadedOrAssignedValue = false;\r
61             entity = null;\r
62         }\r
63 \r
64         public EntityRef(EntityRef<TEntity> entityRef)\r
65         {\r
66             this.entity = entityRef.entity;\r
67             if (entityRef.entity == null && entityRef.source is ICloneable)\r
68             {\r
69                 source = (IEnumerable<TEntity>)((ICloneable)entityRef.source).Clone();\r
70             }\r
71             else\r
72                 source = null;\r
73             hasLoadedOrAssignedValue = entityRef.hasLoadedOrAssignedValue;\r
74         }\r
75 \r
76         public TEntity Entity\r
77         {\r
78             get \r
79             { \r
80                 if (source != null) {\r
81                     foreach (var s in source) {\r
82                         if (entity != null)\r
83                             throw new InvalidOperationException ("Sequence contains more than one element");\r
84                         entity = s;\r
85                     }\r
86                     source = null;\r
87                 }\r
88                 return entity; \r
89             }\r
90             set\r
91             {\r
92                 entity = value;\r
93                 hasLoadedOrAssignedValue = true;\r
94             }\r
95         }\r
96 \r
97         public bool HasLoadedOrAssignedValue\r
98         {\r
99             get\r
100             {\r
101                 return hasLoadedOrAssignedValue;\r
102             }\r
103         }\r
104     }\r
105 }\r