2 // System.Data.ObjectSpaces.ObjectSpace.cs : Handles high-level object persistence interactions with a data source.
5 // Mark Easton (mark.easton@blinksoftware.co.uk)
6 // Tim Coleman (tim@timcoleman.com)
8 // (C) BLiNK Software Ltd. http://www.blinksoftware.co.uk
9 // Copyright (C) Tim Coleman, 2003
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System
.Collections
;
37 using System
.Data
.Mapping
;
38 using System
.Data
.ObjectSpaces
.Query
;
39 using System
.Data
.ObjectSpaces
.Schema
;
41 namespace System
.Data
.ObjectSpaces
43 public class ObjectSpace
48 ObjectSources sources
;
50 CommonObjectContext context
;
56 private ObjectSpace ()
59 os
= new ObjectSchema ();
60 context
= new CommonObjectContext (os
);
64 public ObjectSpace (MappingSchema map
, ObjectSources sources
)
68 this.sources
= sources
;
71 [MonoTODO ("Figure out correct name")]
72 public ObjectSpace (string mapFile
, IDbConnection con
)
75 map
= new MappingSchema (mapFile
);
76 sources
= new ObjectSources ();
77 sources
.Add (map
.DataSources
[0].Name
, con
);
80 public ObjectSpace (string mapFile
, ObjectSources sources
)
83 map
= new MappingSchema (mapFile
);
84 this.sources
= sources
;
87 [MonoTODO ("Figure out correct name")]
88 public ObjectSpace (MappingSchema map
, IDbConnection con
)
92 sources
= new ObjectSources ();
93 sources
.Add (map
.DataSources
[0].Name
, con
);
96 #endregion // Constructors
100 internal ObjectContext ObjectContext
{
101 get { return context; }
104 #endregion // Properties
108 public object GetObject (ObjectQuery query
, object[] parameters
)
110 return GetObject (GetObjectReader (query
, parameters
));
113 public object GetObject (Type type
, string queryString
)
115 return GetObject (GetObjectReader (type
, queryString
));
118 public object GetObject (Type type
, string queryString
, string relatedSpan
)
120 return GetObject (GetObjectReader (type
, queryString
));
123 private object GetObject (ObjectReader reader
)
126 object result
= reader
.Current
;
131 public ObjectReader
GetObjectReader (ObjectQuery query
, object[] parameters
)
133 ObjectExpression oe
= OPath
.Parse (query
, os
);
134 CompiledQuery cq
= oe
.Compile (map
);
135 return ObjectEngine
.GetObjectReader (sources
, context
, cq
, parameters
);
138 public ObjectReader
GetObjectReader (Type type
, string queryString
)
140 return GetObjectReader (new ObjectQuery (type
, queryString
), null);
143 public ObjectReader
GetObjectReader (Type type
, string queryString
, string relatedSpan
)
145 return GetObjectReader (new ObjectQuery (type
, queryString
, relatedSpan
), null);
148 public ObjectSet
GetObjectSet (ObjectQuery query
, object[] parameters
)
150 return GetObjectSet (GetObjectReader (query
, parameters
));
153 public ObjectSet
GetObjectSet (Type type
, string queryString
)
155 return GetObjectSet (GetObjectReader (type
, queryString
));
158 public ObjectSet
GetObjectSet (Type type
, string queryString
, string relatedSpan
)
160 return GetObjectSet (GetObjectReader (type
, queryString
, relatedSpan
));
163 private ObjectSet
GetObjectSet (ObjectReader reader
)
165 ObjectSet result
= new ObjectSet ();
166 foreach (object o
in reader
)
172 public void MarkForDeletion (object obj
)
174 MarkForDeletion (new object[] {obj}
);
178 public void MarkForDeletion (ICollection objs
) {}
180 public void PersistChanges (object obj
)
182 PersistChanges (new object[] {obj}
, new PersistenceOptions ());
185 public void PersistChanges (object obj
, PersistenceOptions options
)
187 PersistChanges (new object[] {obj}
, options
);
190 public void PersistChanges (ICollection objs
)
192 PersistChanges (objs
, new PersistenceOptions ());
195 public void PersistChanges (ICollection objs
, PersistenceOptions options
)
197 ObjectEngine
.PersistChanges (map
, sources
, context
, objs
, options
);
200 public void Resync (object obj
, Depth depth
)
202 Resync (new object[] {obj}
, depth
);
205 public void Resync (ICollection objs
, Depth depth
)
207 ObjectEngine
.Resync (map
, sources
, context
, objs
, depth
);
210 public void StartTracking (object obj
, InitialState state
)
212 StartTracking (new object[] {obj}
, state
);
216 public void StartTracking (ICollection objs
, InitialState state
)
220 #endregion // Methods