Пример #1
0
 public void Add <T>(object id, T value, Dictionary <Type, Dictionary <string, string> > persistentValues)
 {
     lock (_lockObject)
     {
         var idString    = GetIdString(id);
         var stringValue = JsonConvert.SerializeObject(value, JsonSettings.JsonSerializerSettings);
         SetPersistedValue(value, idString, stringValue);
         base.Add(id, value);
         _documentUpdated.OnNext(new DocumentUpdated(idString, value));
     }
 }
Пример #2
0
        public void Add <T>(object id, T value, Dictionary <Type, Dictionary <string, string> > persistentValues)
        {
            EnsureInitialized();

            string idString = GetIdString(id);

            EnsureTypeRegistered(value.GetType());
            using (var connection = OpenSession())
            {
                using (var command = connection.CreateCommand())
                {
                    command.CommandType = CommandType.Text;

                    command.CommandText += @"INSERT INTO Store(Id, ValueTypeId, Value) VALUES(@Id, @ValueTypeId, @Value)";

                    command.Parameters.Add(new SqlParameter("Id", idString));
                    command.Parameters.Add(new SqlParameter("ValueTypeId", KnownTypes[value.GetType()]));

                    var stringValue = JsonConvert.SerializeObject(value, Formatting.None, _jsonSettings);
                    command.Parameters.Add(new SqlParameter("Value", stringValue));

                    _documentUpdated.OnNext(new DocumentUpdated(idString, value));

                    persistentValues.GetOrAddDefault(value.GetType())[idString] = stringValue;
                    try
                    {
                        command.ExecuteNonQuery();
                    }
                    catch (SqlException e)
                    {
                        if (e.Number == UniqueConstraintViolationErrorNumber)
                        {
                            throw new AttemptToSaveAlreadyPersistedValueException(id, value);
                        }
                        throw;
                    }
                }
            }
        }