bool TryGetRequest(IdentityTuple key, out Entity value) { Dictionary <PrimaryKey, Entity> dic; if (requests != null && requests.TryGetValue(key.Type, out dic) && dic.TryGetValue(key.Id, out value)) { return(true); } value = null; return(false); }
public T Request <T>(PrimaryKey?id) where T : Entity { if (id == null) { return(null); } IdentityTuple tuple = new IdentityTuple(typeof(T), id.Value); Entity ident; if (entityCache.TryGetValue(tuple, out ident)) { return((T)ident); } if (retrieved.TryGetValue(tuple, out ident)) { return((T)ident); } ICacheController cc = Schema.Current.CacheController(typeof(T)); if (cc != null && cc.Enabled) { T entityFromCache = EntityCache.Construct <T>(id.Value); cc.Complete(entityFromCache, this); retrieved.Add(tuple, entityFromCache); return(entityFromCache); } ident = (T)requests?.TryGetC(typeof(T))?.TryGetC(id.Value); if (ident != null) { return((T)ident); } T entity = EntityCache.Construct <T>(id.Value); if (requests == null) { requests = new Dictionary <Type, Dictionary <PrimaryKey, Entity> >(); } requests.GetOrCreate(tuple.Type).Add(tuple.Id, entity); return(entity); }
public Lite <T> RequestLite <T>(Lite <T> lite) where T : class, IEntity { if (lite == null) { return(null); } IdentityTuple tuple = new IdentityTuple(lite.EntityType, lite.Id); if (liteRequests == null) { liteRequests = new Dictionary <IdentityTuple, List <Lite <IEntity> > >(); } liteRequests.GetOrCreate(tuple).Add(lite); return(lite); }
public void Add(Entity ie) { if (ie == null) { throw new ArgumentNullException("ie"); } var tuple = new IdentityTuple(ie); Entity ident = dic.TryGetC(tuple); if (ident == null) { dic.Add(tuple, ie); } else if (ident != ie) { //Odd but allowed //throw new InvalidOperationException("There's a different instance of the same entity with Type '{0}' and Id '{1}'".FormatWith(ie.GetType().Name, ie.id)); } }
public T Complete <T>(PrimaryKey?id, Action <T> complete) where T : Entity { if (id == null) { return(null); } IdentityTuple tuple = new IdentityTuple(typeof(T), id.Value); Entity result; if (entityCache.TryGetValue(tuple, out result)) { return((T)result); } if (retrieved.TryGetValue(tuple, out result)) { return((T)result); } T entity; if (TryGetRequest(tuple, out result)) { entity = (T)result; requests[typeof(T)].Remove(id.Value); } else { entity = EntityCache.Construct <T>(id.Value); } retrieved.Add(tuple, entity); complete(entity); return(entity); }
public T Request <T>(PrimaryKey?id) where T : Entity { if (id == null) { return(null); } IdentityTuple tuple = new IdentityTuple(typeof(T), id.Value); Entity ident; if (entityCache.TryGetValue(tuple, out ident)) { return((T)ident); } if (retrieved.TryGetValue(tuple, out ident)) { return((T)ident); } ident = (T)requests?.TryGetC(typeof(T))?.TryGetC(id.Value); if (ident != null) { return((T)ident); } T entity = EntityCache.Construct <T>(id.Value); if (requests == null) { requests = new Dictionary <Type, Dictionary <PrimaryKey, Entity> >(); } requests.GetOrCreate(tuple.Type).Add(tuple.Id, entity); return(entity); }
public Lite <T> RequestLite <T>(Lite <T> lite) where T : class, IEntity { if (lite == null) { return(null); } ICacheController cc = Schema.Current.CacheController(lite.EntityType); if (cc != null && cc.Enabled) { lite.SetToString(cc.TryGetToString(lite.Id) ?? ("[" + EngineMessage.EntityWithType0AndId1NotFound.NiceToString().FormatWith(lite.EntityType.NiceName(), lite.Id) + "]")); return(lite); } IdentityTuple tuple = new IdentityTuple(lite.EntityType, lite.Id); if (liteRequests == null) { liteRequests = new Dictionary <IdentityTuple, List <Lite <IEntity> > >(); } liteRequests.GetOrCreate(tuple).Add(lite); return(lite); }
internal bool TryGetValue(IdentityTuple tuple, out Entity result) { return(dic.TryGetValue(tuple, out result)); }