public bool Remove(string name, out IStoredObject RemovedItem) { bool success = _StoredObjects.TryRemove(name, out RemovedItem); TryDispose(RemovedItem); return(success); }
public bool GetByName <T>(string name, out IStoredObject <T> result) where T : class { IStoredObject return_value = null; if (!_StoredObjects.TryGetValue(name, out return_value)) { result = null; return(false); } else { if (return_value is IStoredObject <T> ) { result = (IStoredObject <T>)return_value; return(true); } else if (return_value.Object is T && return_value is IUnregisterAware) { result = Wrap <T>(name, (T)return_value.Object, ((IUnregisterAware)return_value).OnUnregister); return(true); } else { result = null; return(false); } } }
public bool GetByName(string name, out IStoredObject result) { IStoredObject <object> temp_result = null; bool success = GetByName <object>(name, out temp_result); result = temp_result as IStoredObject; return(success); }
public bool AddOrUpdate(string name, object Object, Type objectType, out IStoredObject StoredObject, Action OnUnregistered = null) { var parameters = new object[] { name, Object, null, OnUnregistered }; var mi = _GenericAddOrUpdateMi.MakeGenericMethod(objectType); bool result = (bool)mi.Invoke(this, parameters); StoredObject = (IStoredObject)parameters[2]; // the out IStoredObject<T> parameter return(result); }
private void TryDispose(IStoredObject storedObject) { IDisposable disposable = storedObject as IDisposable; if (disposable != null) { disposable.Dispose(); } }
/// <summary> /// This constructor can be called when creating a new StoredObject which is intended to replace the OldObject in the store. /// The constructor will ensure the new object's versioning carries forward the old version's versioning. /// </summary> /// <param name="Name">Name under which the object is added to the store</param> /// <param name="Object">The actual (new) object</param> /// <param name="OldObject">The old object which is being replaced</param> internal StoredObject(string Name, T Object, IStoredObject OldObject, Action OnUnregister) { this.Name = Name; this.OnUnregister = OnUnregister; int initial_version = (OldObject == null) ? 0 : OldObject.Version; this.m_VersionProvider = new ConstantVersionProvider(initial_version); // we'll start with a ConstantVersionProvider to supply the initial version. Each time this.Objec is set (such as later in this constructor), this.m_VersionProvider will be replaced with a provider that actually suits the runtime type anyway. this.Object = Object; }
/// <summary> /// Gets the object from the store /// </summary> /// <typeparam name="T">Type of the object to retrieve</typeparam> /// <param name="HandleName">Name from which to retrieve the object</param> /// <returns>Object from the store</returns> /// <exception cref="ArgumentException">Thrown when the HandleName is (empty)</exception> /// <exception cref="Exception">Thrown when the given Handle is not found in the store</exception> private static T GetFromStoreOrThrow <T>(string HandleName, out IStoredObject <T> storedObj) where T : class { HandleName = HandleNames.GetNameFrom(HandleName); if (String.IsNullOrWhiteSpace(HandleName)) { throw new ArgumentException($"Invalid Handle name {HandleName}", nameof(HandleName)); } if (!m_ObjectStore.GetByName <T>(HandleName, out storedObj)) { throw new Exception($"No object {typeof(T).FullName} with handle named {HandleName} found"); } return(storedObj.Object); }
public static void CheckedAddToStore(ObjectStore store, string key, IStoredType obj) { bool added = store.AddOrUpdate(key, obj); Assert.IsTrue(added, "Failed to add object to the object store"); bool exists = store.Exists(key); Assert.IsTrue(exists, "Object was not found in the object store"); IStoredObject <IStoredType> retrievedObj = null; bool retrievedSuccess = store.GetByName <IStoredType>(key, out retrievedObj); Assert.IsTrue(retrievedSuccess, "Object could not be retrieved from the object store"); bool retrievedObjectEqualsOriginal = retrievedObj.Object.Equals(obj); Assert.IsTrue(retrievedObjectEqualsOriginal, "Retrieved object does not equal original object"); }
public static object Deserialize( [ExcelArgument("Data input. Can either be the raw data as it was generated by ExcelScript.Serialize(), or can be a filepath from which to load the data")] string input) { var obj = InternalDeserialize(input); IStoredObject storedObj = (IStoredObject)obj; // Re-inject dependencies into the object var underlyingObj = storedObj.Object; InjectDependencies(underlyingObj); Type objType = storedObj.GetType().GetGenericArguments().Single(); IStoredObject reStoredObj; // todo: maybe reflect & call correct generic method? if (!m_ObjectStore.AddOrUpdate(storedObj.Name, underlyingObj, objType, out reStoredObj, () => TryDispose(underlyingObj))) { throw new InvalidOperationException("Object could not be re-added to the data store"); } return(HandleNames.ToHandle(reStoredObj)); }
public bool AddOrUpdate <T>(string name, T Object, out IStoredObject <T> StoredObject, Action OnUnregistered = null) where T : class { StoredObject = (IStoredObject <T>)_StoredObjects.AddOrUpdate( name, // AddValueFactory (_name) => { return(Wrap(name, Object, OnUnregistered)); }, // UpdateValueFactory (_name, _oldValue) => { // When updating, we must ensure to carry on the version of the previously existing object var result = Wrap(name, Object, _oldValue, OnUnregistered); TryDispose(_oldValue); return(result); } ); return(true); }
/// <summary> /// Adds the given object to the object store, and returns its handle name (name:version). /// Throws exceptions if anything goes wrong. /// </summary> /// <typeparam name="T">Type of the object to add</typeparam> /// <param name="HandleName">Name under which the object shall be stored</param> /// <param name="obj">The object which shall be stored</param> /// <param name="storedObject">[out] the stored object</param> /// <returns>The Handle (name:version) under which the object was stored</returns> private static string AddOrUpdateInStoreOrThrow <T>(string HandleName, T obj, out IStoredObject <T> storedObject, Action OnUnregister = null) where T : class { // Remove any invalid parts from the input HandleName = HandleNames.GetNameFrom(HandleName); if (String.IsNullOrWhiteSpace(HandleName)) { throw new ArgumentException($"Invalid Handle name {HandleName}", nameof(HandleName)); } if (!m_ObjectStore.AddOrUpdate <T>(HandleName, obj, out storedObject, OnUnregister)) { throw new Exception("Could not add script to the object store"); } return(HandleNames.ToHandle(storedObject)); }
private static readonly Regex InvalidChars = new Regex("[^0-9a-zA-Z_]"); // only allow alphabetic characters, letters and underscores public static string ToHandle(IStoredObject storedObject) { return($"{storedObject.Name}:{storedObject.Version}"); }
private IStoredObject <T> Wrap <T>(string Name, T Item, IStoredObject oldItem, Action OnUnregistered) where T : class { return(new StoredObject <T>(Name, Item, oldItem, OnUnregistered)); }