2 // System.Data.ObjectSpaces.ObjectSpace.cs : Handles high-level object persistence interactions with a data source.
\r
5 // Mark Easton (mark.easton@blinksoftware.co.uk)
\r
6 // Tim Coleman (tim@timcoleman.com)
\r
8 // (C) BLiNK Software Ltd. http://www.blinksoftware.co.uk
\r
9 // Copyright (C) Tim Coleman, 2003
\r
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;
\r
37 using System.Data.Mapping;
\r
38 using System.Data.ObjectSpaces.Query;
\r
39 using System.Data.ObjectSpaces.Schema;
\r
41 namespace System.Data.ObjectSpaces
\r
43 public class ObjectSpace
\r
48 ObjectSources sources;
\r
50 CommonObjectContext context;
\r
52 #endregion // Fields
\r
54 #region Constructors
\r
56 private ObjectSpace ()
\r
59 os = new ObjectSchema ();
\r
60 context = new CommonObjectContext (os);
\r
64 public ObjectSpace (MappingSchema map, ObjectSources sources)
\r
68 this.sources = sources;
\r
71 [MonoTODO ("Figure out correct name")]
\r
72 public ObjectSpace (string mapFile, IDbConnection con)
\r
75 map = new MappingSchema (mapFile);
\r
76 sources = new ObjectSources ();
\r
77 sources.Add (map.DataSources [0].Name, con);
\r
80 public ObjectSpace (string mapFile, ObjectSources sources)
\r
83 map = new MappingSchema (mapFile);
\r
84 this.sources = sources;
\r
87 [MonoTODO ("Figure out correct name")]
\r
88 public ObjectSpace (MappingSchema map, IDbConnection con)
\r
92 sources = new ObjectSources ();
\r
93 sources.Add (map.DataSources [0].Name, con);
\r
96 #endregion // Constructors
\r
100 internal ObjectContext ObjectContext {
\r
101 get { return context; }
\r
104 #endregion // Properties
\r
108 public object GetObject (ObjectQuery query, object[] parameters)
\r
110 return GetObject (GetObjectReader (query, parameters));
\r
113 public object GetObject (Type type, string queryString)
\r
115 return GetObject (GetObjectReader (type, queryString));
\r
118 public object GetObject (Type type, string queryString, string relatedSpan)
\r
120 return GetObject (GetObjectReader (type, queryString));
\r
123 private object GetObject (ObjectReader reader)
\r
126 object result = reader.Current;
\r
131 public ObjectReader GetObjectReader (ObjectQuery query, object[] parameters)
\r
133 ObjectExpression oe = OPath.Parse (query, os);
\r
134 CompiledQuery cq = oe.Compile (map);
\r
135 return ObjectEngine.GetObjectReader (sources, context, cq, parameters);
\r
138 public ObjectReader GetObjectReader (Type type, string queryString)
\r
140 return GetObjectReader (new ObjectQuery (type, queryString), null);
\r
143 public ObjectReader GetObjectReader (Type type, string queryString, string relatedSpan)
\r
145 return GetObjectReader (new ObjectQuery (type, queryString, relatedSpan), null);
\r
148 public ObjectSet GetObjectSet (ObjectQuery query, object[] parameters)
\r
150 return GetObjectSet (GetObjectReader (query, parameters));
\r
153 public ObjectSet GetObjectSet (Type type, string queryString)
\r
155 return GetObjectSet (GetObjectReader (type, queryString));
\r
158 public ObjectSet GetObjectSet (Type type, string queryString, string relatedSpan)
\r
160 return GetObjectSet (GetObjectReader (type, queryString, relatedSpan));
\r
163 private ObjectSet GetObjectSet (ObjectReader reader)
\r
165 ObjectSet result = new ObjectSet ();
\r
166 foreach (object o in reader)
\r
172 public void MarkForDeletion (object obj)
\r
174 MarkForDeletion (new object[] {obj});
\r
178 public void MarkForDeletion (ICollection objs) {}
\r
180 public void PersistChanges (object obj)
\r
182 PersistChanges (new object[] {obj}, new PersistenceOptions ());
\r
185 public void PersistChanges (object obj, PersistenceOptions options)
\r
187 PersistChanges (new object[] {obj}, options);
\r
190 public void PersistChanges (ICollection objs)
\r
192 PersistChanges (objs, new PersistenceOptions ());
\r
195 public void PersistChanges (ICollection objs, PersistenceOptions options)
\r
197 ObjectEngine.PersistChanges (map, sources, context, objs, options);
\r
200 public void Resync (object obj, Depth depth)
\r
202 Resync (new object[] {obj}, depth);
\r
205 public void Resync (ICollection objs, Depth depth)
\r
207 ObjectEngine.Resync (map, sources, context, objs, depth);
\r
210 public void StartTracking (object obj, InitialState state)
\r
212 StartTracking (new object[] {obj}, state);
\r
216 public void StartTracking (ICollection objs, InitialState state)
\r
220 #endregion // Methods
\r