/// <summary> /// Creates a lambdaexpression that returnes the id propterty with the value contained by the property. The ID is provided by the typedefinitionfactory and is based on common conventions. /// x => x.Property == value /// </summary> /// <typeparam name="T">The object type that the satement is created with</typeparam> /// <param name="property">The property returning the value in the object</param> /// <returns>A expression providing a equality statement</returns> public static Expression <Func <T, bool> > CreateEqualityExpression <T>(Expression <Func <T> > property) { // get the field that represents the primary key var fields = TypeDefinitionFactory.GetFieldDefinitions <T>(); var pk = fields.FirstOrDefault(f => f.IsPrimaryKey); if (pk == null) { return(null); } var obj = property.Compile().Invoke(); var value = pk.PropertyInfo.GetValue(obj); ParameterExpression pe = Expression.Parameter(typeof(T), "exp"); // x => (x.Property == value) // Create an expression tree that represents the expression 'x.Property == value'. var left = Expression.Property(pe, pk.PropertyInfo); var right = Expression.Constant(value); var expression = Expression.Equal(left, right); return(Expression.Lambda <Func <T, bool> >(expression, new ParameterExpression[] { Expression.Parameter(typeof(T), null) })); }
/// <summary> /// Maps the resultset to a POCO /// </summary> /// <typeparam name="T">The type to map to</typeparam> /// <param name="reader">The datareader with the result</param> ///// <param name="compiledQuery">The querytree</param> /// <returns></returns> public IEnumerable <T> Map <T>(IDataReader reader /*, CompiledQuery compiledQuery*/) { // orig //var fields = TypeDefinitionFactory.GetFieldDefinitions<T>(compiledQuery.QueryParts, !typeof(T).IsAnonymousType()).ToArray(); var fields = TypeDefinitionFactory.GetFieldDefinitions <T>().ToArray(); return(Map <T>(reader, fields)); }
public void ClassDefinition(string className, TypeDefinitionFactory factory) { var syntaxToken = CreateSyntaxIdentifierToken(className); var classDeclaration = SyntaxFactory.ClassDeclaration(new SyntaxList <AttributeListSyntax>(), new SyntaxTokenList(), syntaxToken, null, null, new SyntaxList <TypeParameterConstraintClauseSyntax>(), new SyntaxList <MemberDeclarationSyntax>()); Assert.Throws <NotImplementedException>(() => factory.CreateTypeDefinitionFromSyntax(classDeclaration)); }
public void InterfaceDefinition(string interfaceName, string[] methodNames, string[] attributeNames, [Frozen] Mock <IMethodDefinitionFactory> methodDefinitionFactoryMock, [Frozen] Mock <IAttributeDefinitionFactory> attributeDefinitionFactoryMock, TypeDefinitionFactory factory) { Expression <Func <IMethodDefinitionFactory, MethodDefinition> > createMethodExpression = e => e.CreateMethodFromSyntax(It.IsAny <MethodDeclarationSyntax>()); Expression <Func <IAttributeDefinitionFactory, AttributeDefinition> > createAttributeExpression = e => e.CreateAttributeFromSyntax(It.IsAny <AttributeSyntax>()); var syntaxToken = SyntaxFactory.Identifier(new SyntaxTriviaList(), SyntaxKind.AbstractKeyword, interfaceName, interfaceName, new SyntaxTriviaList()); var interfaceDeclaration = SyntaxFactory.InterfaceDeclaration(new SyntaxList <AttributeListSyntax>(), new SyntaxTokenList(), syntaxToken, null, null, new SyntaxList <TypeParameterConstraintClauseSyntax>(), new SyntaxList <MemberDeclarationSyntax>()).AddMembers(methodNames.Select(CreateMethod).ToArray()).AddAttributeLists(CreateAttributeList(attributeNames)); methodDefinitionFactoryMock.Setup(createMethodExpression).Returns(() => null).Verifiable(); attributeDefinitionFactoryMock.Setup(createAttributeExpression).Returns(() => null).Verifiable(); var result = factory.CreateTypeDefinitionFromSyntax(interfaceDeclaration); var interfaceDefinition = Assert.IsType <InterfaceDefinition>(result); Assert.Equal(interfaceName, interfaceDefinition.Name); Assert.Empty(interfaceDefinition.Usings); Assert.Equal(methodNames.Length, interfaceDefinition.Functions.Length); Assert.Equal(attributeNames.Length, interfaceDefinition.Attributes.Length); methodDefinitionFactoryMock.Verify(createMethodExpression, Times.Exactly(methodNames.Length)); attributeDefinitionFactoryMock.Verify(createAttributeExpression, Times.Exactly(attributeNames.Length)); }