Пример #1
0
        public void UseSpecificationLogicAndOperatorTest()
        {
            //Arrange
            DirectSpecification <SampleEntity> leftAdHocSpecification;
            DirectSpecification <SampleEntity> rightAdHocSpecification;

            var identifier = Guid.NewGuid();
            Expression <Func <SampleEntity, bool> > leftSpec  = s => s.Id == identifier;
            Expression <Func <SampleEntity, bool> > rightSpec = s => s.SampleProperty.Length > 2;

            //Act
            leftAdHocSpecification  = new DirectSpecification <SampleEntity>(leftSpec);
            rightAdHocSpecification = new DirectSpecification <SampleEntity>(rightSpec);

            ISpecification <SampleEntity> andSpec = leftAdHocSpecification & rightAdHocSpecification;

            //Assert

            var list    = new List <SampleEntity>();
            var sampleA = new SampleEntity()
            {
                SampleProperty = "1"
            };

            sampleA.ChangeCurrentIdentity(identifier);

            var sampleB = new SampleEntity()
            {
                SampleProperty = "the sample property"
            };

            sampleB.GenerateNewIdentity();

            var sampleC = new SampleEntity()
            {
                SampleProperty = "the sample property"
            };

            sampleC.ChangeCurrentIdentity(identifier);

            list.AddRange(
                new SampleEntity[]
            {
                sampleA,
                sampleB,
                sampleC
            });

            var result = list.AsQueryable().Where(andSpec.SatisfiedBy()).ToList();

            Assert.IsTrue(result.Count == 1);
        }
Пример #2
0
      public void ChangeIdentitySetNewIdentity()
      {
         //Arrange
         var entity = new SampleEntity();
         entity.GenerateNewIdentity();

         //act
         var expected = entity.Id;
         entity.ChangeCurrentIdentity(Guid.NewGuid());

         //assert
         Assert.AreNotEqual(expected, entity.Id);
      }
Пример #3
0
        public void CreateOrSpecificationTest()
        {
            //Arrange
            DirectSpecification <SampleEntity> leftAdHocSpecification;
            DirectSpecification <SampleEntity> rightAdHocSpecification;

            var identifier = Guid.NewGuid();
            Expression <Func <SampleEntity, bool> > leftSpec  = s => s.Id == identifier;
            Expression <Func <SampleEntity, bool> > rightSpec = s => s.SampleProperty.Length > 2;

            leftAdHocSpecification  = new DirectSpecification <SampleEntity>(leftSpec);
            rightAdHocSpecification = new DirectSpecification <SampleEntity>(rightSpec);

            //Act
            var composite = new OrSpecification <SampleEntity>(leftAdHocSpecification, rightAdHocSpecification);

            //Assert
            Assert.IsNotNull(composite.SatisfiedBy());
            ReferenceEquals(leftAdHocSpecification, composite.LeftSideSpecification);
            ReferenceEquals(rightAdHocSpecification, composite.RightSideSpecification);

            var list = new List <SampleEntity>();

            var sampleA = new SampleEntity()
            {
                SampleProperty = "1"
            };

            sampleA.ChangeCurrentIdentity(identifier);

            var sampleB = new SampleEntity()
            {
                SampleProperty = "the sample property"
            };

            sampleB.GenerateNewIdentity();

            list.AddRange(
                new SampleEntity[]
            {
                sampleA,
                sampleB
            });

            var result = list.AsQueryable().Where(composite.SatisfiedBy()).ToList();

            Assert.IsTrue(result.Count() == 2);
        }
Пример #4
0
        public void SameIdentityProduceEqualsTrueTest()
        {
            //Arrange
            Guid id = Guid.NewGuid();

            var entityLeft  = new SampleEntity();
            var entityRight = new SampleEntity();

            entityLeft.ChangeCurrentIdentity(id);
            entityRight.ChangeCurrentIdentity(id);

            //Act
            bool resultOnEquals   = entityLeft.Equals(entityRight);
            bool resultOnOperator = entityLeft == entityRight;

            //Assert
            Assert.IsTrue(resultOnEquals);
            Assert.IsTrue(resultOnOperator);
        }
Пример #5
0
        public void DDDUseSpecificationBitwiseOrOperatorTest()
        {
            //Arrange
            DirectSpecification <SampleEntity> leftAdHocSpecification;
            DirectSpecification <SampleEntity> rightAdHocSpecification;

            var identifier = Guid.NewGuid();
            Expression <Func <SampleEntity, bool> > leftSpec  = s => s.EntityId == identifier;
            Expression <Func <SampleEntity, bool> > rightSpec = s => s.SampleProperty.Length > 2;


            //Act
            leftAdHocSpecification  = new DirectSpecification <SampleEntity>(leftSpec);
            rightAdHocSpecification = new DirectSpecification <SampleEntity>(rightSpec);

            var orSpec = leftAdHocSpecification | rightAdHocSpecification;


            //Assert
            var list = new List <SampleEntity>();

            var sampleA = new SampleEntity()
            {
                SampleProperty = "1"
            };

            sampleA.ChangeCurrentIdentity(identifier);

            var sampleB = new SampleEntity()
            {
                SampleProperty = "the sample property"
            };

            sampleB.GenerateNewIdentity();

            list.AddRange(new SampleEntity[] { sampleA, sampleB });

            var result = list.AsQueryable().Where(orSpec.SatisfiedBy()).ToList();
        }