Ejemplo n.º 1
0
        public IEntityConfigurationWrapper <T> ToUseNaturalKey <TKey>(Expression <Func <T, TKey> > naturalKeySelector)
        {
            var memberExpression = default(MemberExpression);

            Guard.Against.InvalidMemberExpression(() => naturalKeySelector, out memberExpression);

            var naturalKey = new NaturalKey(typeof(T), memberExpression.Member.Name, typeof(TKey), this.typeAnalyzerService);

            this.entityType.ConfigureNaturalKey(naturalKey);

            return(this);
        }
            public void Scenario(Subject instance, NaturalKey naturalKey)
            {
                "Given a natural key that is a value object"
                .f(() => naturalKey = new NaturalKey("naturalKey"));

                "When an instance of an aggregate root is created with that natural key"
                .f(() => instance = new Subject(naturalKey));

                "Then the natural key of that instance should be the original natural key"
                .f(() => instance.NaturalKey.Should().Be(naturalKey));

                "And the instance should contain a single uncommitted 'NewSubject' event with a natural key value matching the original natural key value"
                .f(() => instance.GetUncommittedEvents().Should().ContainSingle(
                       @event => @event is NewSubject && ((NewSubject)@event).NaturalKeyValue == naturalKey.Value));
            }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="EntityType"/> class.
        /// </summary>
        /// <param name="runtimeType">The runtime type.</param>
        /// <param name="typeAnalyzerService">The type analyzer service.</param>
        public EntityType(Type runtimeType, ITypeAnalyzerService typeAnalyzerService)
            : base(new NaturalKey(typeof(EntityType), "RuntimeType", typeof(Type), DefaultTypeAnalyzerService))
        {
            Guard.Against.Null(() => runtimeType);
            Guard.Against.Null(() => typeAnalyzerService);

            if (!typeAnalyzerService.IsValidEntity(runtimeType))
            {
                throw new BusinessException(
                    string.Format(CultureInfo.InvariantCulture, "The specified runtime type '{0}' is not an entity.", runtimeType));
            }

            this.RuntimeType = runtimeType;
            this.entityNaturalKey = typeAnalyzerService.GetNaturalKey(runtimeType);
            this.Mappings = new MapperCollection();
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="EntityType"/> class.
        /// </summary>
        /// <param name="runtimeType">Type of the runtime.</param>
        /// <param name="entityAnalyzerService">The entity analyzer service.</param>
        /// <param name="baseEntity">The base entity.</param>
        public EntityType(Type runtimeType, ITypeAnalyzerService entityAnalyzerService, EntityType baseEntity)
            : this(runtimeType, entityAnalyzerService)
        {
            Guard.Against.Null(() => runtimeType);
            Guard.Against.Null(() => baseEntity);

            if (baseEntity.RuntimeType != runtimeType.BaseType)
            {
                throw new BusinessException(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "The specified base entity runtime type '{0}' does not match the runtime type base type '{1}'.",
                        baseEntity.RuntimeType,
                        runtimeType.BaseType));
            }

            this.baseEntityNaturalKey = baseEntity.NaturalKey;
        }
 private void Handle(NewSubject @event)
 {
     this.NaturalKey = Map.Event(@event).ToValueObject <NaturalKey>();
 }
                public Subject(NaturalKey key)
                {
                    var @event = Map.ValueObject(key).ToEvent <NewSubject>();

                    this.Apply(@event);
                }
Ejemplo n.º 7
0
        /// <summary>
        /// Configures the natural key for this entity type.
        /// </summary>
        /// <param name="naturalKey">The natural key.</param>
        public void ConfigureNaturalKey(NaturalKey naturalKey)
        {
            Guard.Against.Null(() => naturalKey);

            if (this.entityNaturalKey == naturalKey)
            {
                return;
            }

            if (naturalKey.RuntimeType != this.RuntimeType)
            {
                throw new BusinessException(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "Invalid natural key specified. The natural key must have a declaring type of '{0}'.",
                        this.RuntimeType));
            }

            if (this.entityNaturalKey != null)
            {
                throw new BusinessException(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "Cannot configure the natural key for the entity '{0}' to be the property '{1}' as the natural key is already configured to be the property '{2}'.",
                        this.RuntimeType,
                        naturalKey.PropertyName,
                        this.NaturalKey.PropertyName));
            }

            this.entityNaturalKey = naturalKey;
        }