コード例 #1
0
        public void VisitorShouldResolveMethodName()
        {
            // arrange
            Expression <Func <Entity, object> > expression = o => o.AuditInfo("test");
            var visitor = new PropertyPathVisitor();

            // act
            visitor.Visit(expression);

            // assert
            Assert.Equal(nameof(Entity.AuditInfo), visitor.Name);
        }
コード例 #2
0
        public void VisitorShouldResolvePropertyNameWithIndexer()
        {
            // arrange
            Expression <Func <Entity, object> > expression = o => o.Items[0];
            var visitor = new PropertyPathVisitor();

            // act
            visitor.Visit(expression);

            // assert
            Assert.Equal(nameof(Entity.Items), visitor.Name);
        }
コード例 #3
0
        public void VisitorShouldResolvePropertyNameFromPath()
        {
            // arrange
            Expression <Func <Entity, object> > expression = o => o.Items.Count;
            var visitor = new PropertyPathVisitor();

            // act
            visitor.Visit(expression);

            // assert
            Assert.Equal(nameof(IList <object> .Count), visitor.Name);
        }
コード例 #4
0
        public void VisitorShouldGenerateKeyWithIndexer()
        {
            // arrange
            Expression <Func <Entity, object> > expression = o => o.Items[0];
            var visitor  = new PropertyPathVisitor();
            var expected = $"{typeof( Entity ).FullName}.{nameof( Entity.Items )}_0";

            // act
            visitor.Visit(expression);
            var actual = visitor.GenerateKey(typeof(Entity).FullName);

            // assert
            Assert.Equal(expected, actual);
        }
コード例 #5
0
        public void VisitorShouldGenerateKeyWithParameter()
        {
            // arrange
            Expression <Func <Entity, object> > expression = o => o.AuditInfo("test");
            var visitor  = new PropertyPathVisitor();
            var expected = $"{typeof( Entity ).FullName}.{nameof( Entity.AuditInfo )}_test";

            // act
            visitor.Visit(expression);
            var actual = visitor.GenerateKey(typeof(Entity).FullName);

            // assert
            Assert.Equal(expected, actual);
        }
コード例 #6
0
        internal static string GetInstanceAnnotationKey <TObject, TProperty>(this Expression <Func <TObject, TProperty> > propertyExpression, out string name)
        {
            Contract.Requires(propertyExpression != null);
            Contract.Ensures(!string.IsNullOrEmpty(Contract.Result <string>()));

            var visitor = new PropertyPathVisitor();

            // visit the expression and get the name of the first member (e.g. property or field)
            visitor.Visit(propertyExpression);

            // the expression wasn't valid; throw meaningful exception
            if (string.IsNullOrEmpty(name = visitor.Name))
            {
                throw new ArgumentException(string.Format(CurrentCulture, InvalidAnnotationPropertyExpression, propertyExpression), nameof(propertyExpression));
            }

            // build unique key
            return(visitor.GenerateKey(typeof(TObject).FullName));
        }