// todo: add in tests public DataStoreInstance AddOrUpdate(DataStoreInstance instance) { // todo: better error handling here // todo: add in test if (instance == null) { throw new Exception("You cannot create a blank instance"); } // todo: better error handling here // todo: add in test if (string.IsNullOrEmpty(instance.Name) || string.IsNullOrEmpty(instance.Description)) { throw new Exception("You need a Name and Description to add an instance"); } if (!instance.Exists(this)) { instance.Add(this); } else { instance.Update(this); } // Ensure that the cached instance reflects any changes made _instanceCache[instance.Name] = instance.Get(this); return GetCachedInstance(instance.Name); }
private DataStoreInstance GetCachedInstance(string name) { if (!_instanceCache.ContainsKey(name)) { var instance = new DataStoreInstance(name).Get(this); _instanceCache.Add(name, instance); } return _instanceCache[name]; }
// CreateInstance() public DataStoreInstance CreateInstance(string name, string description) { var instance = new DataStoreInstance { DateCreated = DateTime.UtcNow, Description = description, Name = name }; AddOrUpdate(instance); return instance.Get(this); }
public void Delete(DataStoreInstance instance) { // todo: better error handling here if (instance == null) { throw new Exception("You need to provide an instance to delete"); } // We need to drop all keys belonning to this instance instance.DeleteAllKeys(this); instance.Delete(this); }
// Helper methods private static void AreTheSame(DataStoreInstance instance1, DataStoreInstance instance2) { Assert.AreEqual(instance1.Description, instance2.Description); Assert.AreEqual(instance1.DateCreated, instance2.DateCreated); Assert.AreEqual(instance1.Id, instance2.Id); Assert.AreEqual(instance1.Name, instance2.Name); }
public void GetAll_Succeeds_WhenCalled() { _dataStore.CreateInstance(GenerateInstanceName(), _instanceDescription); _dataStore.CreateInstance(GenerateInstanceName(), _instanceDescription); _dataStore.CreateInstance(GenerateInstanceName(), _instanceDescription); var instances = new DataStoreInstance().GetAll(_dataStore); Assert.Greater(instances.Count, 3); }