/// <summary> /// Gets an instance by key /// </summary> private object GetInstance(Type type, object key, int keyIndex, CycleCache cache) { var obj = cache.CheckKey(type, key); if (obj != null) { return(obj); } if (keyIndex < 0) { return(null); } obj = this.Driver.Load(type, key, keyIndex, cache); return(obj); }
/// <summary> /// Load it without knowledge of the key type /// </summary> /// <param name="type">The type to load</param> /// <param name="key">The key</param> /// <param name="cache">Cache queue</param> /// <returns>The instance</returns> public object Load(Type type, object key, CycleCache cache) { var newType = type; var assignable = false; var keyIndex = -1; if (!TableDefinitions.ContainsKey(type)) { // check if type is a base type foreach (var t in TableDefinitions.Keys.Where(type.IsAssignableFrom)) { assignable = true; lock (TableDefinitions[t]) { keyIndex = TableDefinitions[t].Keys.GetIndexForKey(key); } if (keyIndex < 0) { continue; } newType = t; break; } } else { lock (TableDefinitions[newType]) { keyIndex = TableDefinitions[newType].Keys.GetIndexForKey(key); } } if (!assignable) { if (!TableDefinitions.ContainsKey(type)) { throw new SterlingTableNotFoundException(type, Name); } } if (keyIndex < 0) { return(null); } var obj = cache.CheckKey(newType, key); if (obj != null) { return(obj); } BinaryReader br = null; MemoryStream memStream = null; try { br = Driver.Load(newType, keyIndex); var serializationHelper = new SerializationHelper(this, Serializer, SterlingFactory.GetLogger(), s => Driver.GetTypeIndex(s), i => Driver.GetTypeAtIndex(i)); if (_byteInterceptorList.Count > 0) { var bytes = br.ReadBytes((int)br.BaseStream.Length); bytes = _byteInterceptorList.ToArray().Reverse().Aggregate(bytes, (current, byteInterceptor) => byteInterceptor.Load(current)); memStream = new MemoryStream(bytes); br.Close(); br = new BinaryReader(memStream); } obj = serializationHelper.Load(newType, key, br, cache); } finally { if (br != null) { br.Close(); } if (memStream != null) { memStream.Flush(); memStream.Close(); } } _RaiseOperation(SterlingOperation.Load, newType, key); return(obj); }