5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
\r
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
\r
8 // of this software and associated documentation files (the "Software"), to deal
\r
9 // in the Software without restriction, including without limitation the rights
\r
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
11 // copies of the Software, and to permit persons to whom the Software is
\r
12 // furnished to do so, subject to the following conditions:
\r
14 // The above copyright notice and this permission notice shall be included in
\r
15 // all copies or substantial portions of the Software.
\r
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
28 using System.Collections.Generic;
\r
32 using System.Data.Linq.Identity;
\r
34 using DbLinq.Data.Linq.Identity;
\r
38 namespace System.Data.Linq.Implementation
\r
40 namespace DbLinq.Data.Linq.Implementation
\r
44 /// List of entities, with their corresponding state (to insert, to watch, to delete)
\r
46 internal class EntityTracker : IEntityTracker
\r
49 /// Entities being watched
\r
51 private readonly List<EntityTrack> entities = new List<EntityTrack>();
\r
54 /// Entities currently being watched and to be updated
\r
56 private readonly IDictionary<IdentityKey, EntityTrack> entitiesByKey = new Dictionary<IdentityKey, EntityTrack>();
\r
59 /// Finds an entity tracking info by object reference
\r
61 /// <param name="entity"></param>
\r
62 /// <returns></returns>
\r
63 private EntityTrack FindByReference(object entity)
\r
65 // return (from e in entities where e.Entity == entity select e).FirstOrDefault();
\r
66 return this.entities.Find(e => object.ReferenceEquals(entity, e.Entity));
\r
70 /// Finds entity by key (PK)
\r
72 /// <param name="identityKey"></param>
\r
73 /// <returns></returns>
\r
74 public EntityTrack FindByIdentity(IdentityKey identityKey)
\r
76 EntityTrack entityTrack;
\r
77 entitiesByKey.TryGetValue(identityKey, out entityTrack);
\r
82 /// Returns true if the list contains the entity
\r
84 /// <param name="entity"></param>
\r
85 /// <returns></returns>
\r
86 public bool ContainsReference(object entity)
\r
88 return FindByReference(entity) != null;
\r
92 /// Registers an entity to be inserted
\r
94 /// <param name="entity"></param>
\r
95 public void RegisterToInsert(object entity)
\r
98 throw new ArgumentNullException("entity");
\r
100 var entityTrack = FindByReference(entity);
\r
101 if (entityTrack == null)
\r
103 entityTrack = new EntityTrack(entity, EntityState.ToInsert);
\r
104 entities.Add(entityTrack);
\r
108 switch (entityTrack.EntityState)
\r
110 // if already registered for insert/update, then this is an error
\r
111 case EntityState.ToInsert:
\r
112 case EntityState.ToWatch:
\r
113 throw new InvalidOperationException();
\r
114 // whenever the object is registered for deletion, the fact of
\r
115 // registering it for insertion sets it back to watch
\r
116 case EntityState.ToDelete:
\r
117 entityTrack.EntityState = EntityState.ToWatch;
\r
118 entitiesByKey[entityTrack.IdentityKey] = entityTrack;
\r
121 throw new ArgumentOutOfRangeException();
\r
127 /// Registers an entity to be watched
\r
129 /// <param name="entity"></param>
\r
130 /// <param name="identityKey"></param>
\r
131 public void RegisterToWatch(object entity, IdentityKey identityKey)
\r
133 var entityTrack = FindByReference(entity);
\r
134 if (entityTrack == null)
\r
136 entityTrack = new EntityTrack(entity, EntityState.ToWatch) { IdentityKey = identityKey };
\r
137 entities.Add(entityTrack);
\r
138 entitiesByKey[identityKey] = entityTrack;
\r
142 // changes the state of the current entity
\r
143 switch (entityTrack.EntityState)
\r
145 case EntityState.ToInsert:
\r
146 entityTrack.EntityState = EntityState.ToWatch;
\r
147 entityTrack.IdentityKey = identityKey;
\r
148 entitiesByKey[identityKey] = entityTrack;
\r
150 // watched entities should not be registered again
\r
151 case EntityState.ToWatch:
\r
152 case EntityState.ToDelete:
\r
153 throw new InvalidOperationException();
\r
155 throw new ArgumentOutOfRangeException();
\r
161 /// Registers entity to be deleted
\r
163 /// <param name="entity"></param>
\r
164 public void RegisterToDelete(object entity)
\r
166 if (entity == null)
\r
167 throw new ArgumentNullException("entity");
\r
169 var entityTrack = FindByReference(entity);
\r
170 if (entityTrack == null)
\r
172 entityTrack = new EntityTrack(entity, EntityState.ToDelete);
\r
173 entities.Add(entityTrack);
\r
177 // changes the state of the current entity
\r
178 switch (entityTrack.EntityState)
\r
180 // if entity was to be inserted, we just remove it from the list
\r
181 // as if it never came here
\r
182 case EntityState.ToInsert:
\r
183 entities.Remove(entityTrack);
\r
185 // watched entities are registered to be removed
\r
186 case EntityState.ToWatch:
\r
187 entityTrack.EntityState = EntityState.ToDelete;
\r
188 entitiesByKey.Remove(entityTrack.IdentityKey);
\r
190 case EntityState.ToDelete:
\r
191 throw new InvalidOperationException();
\r
193 throw new ArgumentOutOfRangeException();
\r
199 /// Unregisters the entity after deletion
\r
201 /// <param name="entity"></param>
\r
202 public void RegisterDeleted(object entity)
\r
204 // TODO: we could require an index
\r
205 var entityTrack = FindByReference(entity);
\r
206 if (entityTrack == null)
\r
208 throw new ArgumentException("entity");
\r
210 // changes the state of the current entity
\r
211 switch (entityTrack.EntityState)
\r
213 case EntityState.ToDelete:
\r
214 entities.Remove(entityTrack);
\r
216 case EntityState.ToInsert:
\r
217 case EntityState.ToWatch:
\r
218 throw new InvalidOperationException();
\r
220 throw new ArgumentOutOfRangeException();
\r
225 /// Enumerates all registered entities
\r
227 /// <returns></returns>
\r
228 public IEnumerable<EntityTrack> EnumerateAll()
\r