Example #1
0
        public void SetKey(string instanceName, string key, string value)
        {
            // Find the instance in question
            // todo: clean this method up a bit..
            var instace = GetCachedInstance(instanceName);

            if (instace == null)
            {
                CreateInstance(instanceName, string.Format(
                    "Automatically generated instance for '{0}'",
                    instanceName));

                instace = GetCachedInstance(instanceName);
                if (instace == null)
                {
                    throw new Exception(string.Format(
                        "Unable to find the '{0}' instance", instanceName));
                }
            }

            var dbKey = new DataStoreKey(key, instace.Id)
                .Get(this);

            // If the key is not null, update it
            if (dbKey != null)
            {
                dbKey.DateUpdated = DateTime.UtcNow;
                dbKey.Value = value;
                dbKey.Update(this);
                return;
            }

            // Otherwise create the new key
            new DataStoreKey
            {
                DateCreated = DateTime.UtcNow,
                InstanceId = instace.Id,
                Key = key,
                Value = value
            }.Add(this);
        }
Example #2
0
        public DataStoreKey AddOrUpdate(DataStoreKey keyInstance)
        {
            // todo: better error handling here
            // todo: add in test
            if (keyInstance == null)
            {
                throw new Exception("You cannot create an empty key");
            }

            // todo: better error handling here
            // todo: add in test
            if (string.IsNullOrEmpty(keyInstance.Key))
            {
                throw new Exception("You cannot add a key without a name");
            }

            // todo: better error handling here
            // todo: add in test
            if (keyInstance.InstanceId == 0)
            {
                throw new Exception("Your key needs to belong to an instance");
            }

            if (!keyInstance.Exists(this))
            {
                keyInstance.Add(this);
            }
            else
            {
                keyInstance.Update(this);
            }

            return keyInstance.Get(this);
        }