Ejemplo n.º 1
0
        /// <summary>
        /// Attempts to add the natural key to the natural key records.
        /// </summary>
        /// <param name="aggregateRootType">Type of the aggregate root.</param>
        /// <param name="serializedNaturalKey">The serialized natural key.</param>
        /// <param name="checkpoint">The checkpoint.</param>
        /// <param name="naturalKeyRecord">The natural key record.</param>
        /// <returns>Returns <c>true</c> if the natural key record was successfully added; otherwise <c>false</c>.</returns>
        public bool TryAddNaturalKey(Type aggregateRootType, object serializedNaturalKey, long checkpoint, out NaturalKeyRecord naturalKeyRecord)
        {
            Guard.Against.Null(() => aggregateRootType);
            Guard.Against.Null(() => serializedNaturalKey);

            if (this.isDisposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            using (new ExclusiveCodeBlock(this.mutex))
            {
                this.Synchronize();

                var naturalKeysType = default(List <MemoryMappedNaturalKey>);
                var maxNaturalKeysTypeCheckpoint = this.naturalKeysTypes.TryGetValue(aggregateRootType, out naturalKeysType)
                    ? naturalKeysType.Max(naturalKey => naturalKey.Checkpoint)
                    : 0L;

                if (maxNaturalKeysTypeCheckpoint != checkpoint)
                {
                    naturalKeyRecord = null;
                    return(false);
                }

                var memoryMappedNaturalKey = new MemoryMappedNaturalKey
                {
                    Identity        = Guid.NewGuid(),
                    Type            = aggregateRootType.GetSerializedName(),
                    SerializedValue = (string)serializedNaturalKey,
                    Checkpoint      = checkpoint + 1,
                    IsRemoved       = false,
                };

                var buffer = Encoding.UTF8.GetBytes(Serializer.Serialize(memoryMappedNaturalKey));

                using (var accessor = this.file.CreateViewAccessor(this.writeOffset, 2 + buffer.Length))
                {
                    accessor.Write(0, (ushort)buffer.Length);
                    accessor.WriteArray(2, buffer, 0, buffer.Length);
                }

                this.writeOffset += 2 + buffer.Length;

                naturalKeyRecord = new NaturalKeyRecord
                {
                    Identity        = memoryMappedNaturalKey.Identity,
                    SerializedValue = memoryMappedNaturalKey.SerializedValue,
                    Checkpoint      = memoryMappedNaturalKey.Checkpoint,
                    IsRemoved       = memoryMappedNaturalKey.IsRemoved,
                };
            }

            return(true);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Attempts to add the natural key to the natural key records.
        /// </summary>
        /// <param name="aggregateRootType">Type of the aggregate root.</param>
        /// <param name="serializedNaturalKey">The serialized natural key.</param>
        /// <param name="checkpoint">The checkpoint.</param>
        /// <param name="naturalKeyRecord">The natural key record.</param>
        /// <returns>Returns <c>true</c> if the natural key record was successfully added; otherwise <c>false</c>.</returns>
        public bool TryAddNaturalKey(Type aggregateRootType, object serializedNaturalKey, long checkpoint, out NaturalKeyRecord naturalKeyRecord)
        {
            Guard.Against.Null(() => aggregateRootType);
            Guard.Against.Null(() => serializedNaturalKey);

            using (new TransactionScope(TransactionScopeOption.Suppress))
                using (var connection = new SqlConnection(this.connectionString))
                    using (var command = connection.CreateCommand())
                    {
                        command.CommandType = CommandType.StoredProcedure;
                        command.CommandText = string.Concat(this.schema, ".TryAddNaturalKey");
                        command.Parameters.Add("@AggregateRootTypeName", SqlDbType.VarChar, 511).Value = aggregateRootType.FullName;
                        command.Parameters.Add("@Value", SqlDbType.VarChar).Value     = serializedNaturalKey;
                        command.Parameters.Add("@Checkpoint", SqlDbType.BigInt).Value = checkpoint;

                        connection.Open();

                        using (var reader = command.ExecuteReader())
                        {
                            if (!reader.Read())
                            {
                                naturalKeyRecord = null;
                                return(false);
                            }

                            naturalKeyRecord = new NaturalKeyRecord
                            {
                                Identity        = new Guid(Convert.ToString(reader["Identity"])),
                                SerializedValue = (string)serializedNaturalKey,
                                Checkpoint      = Convert.ToInt64(reader["Checkpoint"]),
                            };

                            return(true);
                        }
                    }
        }
        /// <summary>
        /// Attempts to add the natural key to the natural key records.
        /// </summary>
        /// <param name="aggregateRootType">Type of the aggregate root.</param>
        /// <param name="serializedNaturalKey">The serialized natural key.</param>
        /// <param name="checkpoint">The checkpoint.</param>
        /// <param name="naturalKeyRecord">The natural key record.</param>
        /// <returns>Returns <c>true</c> if the natural key record was successfully added; otherwise <c>false</c>.</returns>
        public bool TryAddNaturalKey(Type aggregateRootType, object serializedNaturalKey, long checkpoint, out NaturalKeyRecord naturalKeyRecord)
        {
            Guard.Against.Null(() => aggregateRootType);
            Guard.Against.Null(() => serializedNaturalKey);

            using (new TransactionScope(TransactionScopeOption.Suppress))
            using (var connection = new SqlConnection(this.connectionString))
            using (var command = connection.CreateCommand())
            {
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = string.Concat(this.schema, ".TryAddNaturalKey");
                command.Parameters.Add("@AggregateRootTypeName", SqlDbType.VarChar, 511).Value = aggregateRootType.FullName;
                command.Parameters.Add("@Value", SqlDbType.VarChar).Value = serializedNaturalKey;
                command.Parameters.Add("@Checkpoint", SqlDbType.BigInt).Value = checkpoint;

                connection.Open();

                using (var reader = command.ExecuteReader())
                {
                    if (!reader.Read())
                    {
                        naturalKeyRecord = null;
                        return false;
                    }

                    naturalKeyRecord = new NaturalKeyRecord
                    {
                        Identity = new Guid(Convert.ToString(reader["Identity"])),
                        SerializedValue = (string)serializedNaturalKey,
                        Checkpoint = Convert.ToInt64(reader["Checkpoint"]),
                    };

                    return true;
                }
            }
        }