Пример #1
0
        private void CreateFields(Type sourceType, TypeBuilder typeBuilder)
        {
            var fieldMap = SnapshotableField.GetMap(sourceType);

            foreach (var pair in fieldMap)
            {
                typeBuilder.DefineField(pair.Key, pair.Value.FieldType, FieldAttributes.Public);
            }
        }
Пример #2
0
        private void TransferState(DynamicSnapshotBase snapshot, AggregateRoot aggregate, TransferDirection direction)
        {
            if (snapshot == null)
            {
                throw new ArgumentNullException("snapshot");
            }
            if (aggregate == null)
            {
                throw new ArgumentNullException("source");
            }

            var aggregateFieldMap = SnapshotableField.GetMap(aggregate.GetType());
            var snapshotFields    = snapshot.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public);

            Action <object, FieldInfo, object, FieldInfo> doTransfer = null;

            if (direction == TransferDirection.ToAggregateRoot)
            {
                doTransfer = (source, sourceField, destination, destinationField)
                             => destinationField.SetValue(destination, sourceField.GetValue(source));
            }
            else
            {
                doTransfer = (destination, destinationField, source, sourceField)
                             => destinationField.SetValue(destination, sourceField.GetValue(source));
            }

            foreach (var snapshotField in snapshotFields)
            {
                FieldInfo aggregateField;
                if (aggregateFieldMap.TryGetValue(snapshotField.Name, out aggregateField))
                {
                    doTransfer(snapshot, snapshotField, aggregate, aggregateField);
                }
                else
                {
                    // TODO: No field found; throw?
                }
            }
        }