组合两个转换表达式
Combine two Convert Expressions
场景:
一些实体类:
public class BookEntity
{
public int IdBook { get; set; }
public string Title { get; set; }
}
public class StudentEntity
{
public int IdStudent { get; set; }
public string Name { get; set; }
}
public class LoanEntity
{
public int IdLoan { get; set; }
public StudentEntity Student { get; set; }
public BookEntity Book { get; set; }
}
还有一些数据传输对象类:
public class BookDTO
{
public int IdBook { get; set; }
public string Title { get; set; }
}
public class StudentDTO
{
public int IdStudent { get; set; }
public string Name { get; set; }
}
public class LoanDTO
{
public int IdLoan { get; set; }
public StudentDTO Student { get; set; }
public BookDTO Book { get; set; }
}
我已经有了这个表达式(在 dto 中转换实体):
Expression<Func<BookEntity, BookDTO>> pred1 = e => new BookDTO
{
IdBook = e.IdBook,
Title = e.Title
};
Expression<Func<StudentEntity, StudentDTO>> pred2 = e => new StudentDTO
{
IdStudent = e.IdStudent,
Name = e.Name
};
目标:
现在我想创建一个表达式,将 LoanEntity
转换为 LoanDTO
。类似于:
Expression<Func<LoanEntity, LoanDTO>> pred3 = e => new LoanDTO
{
IdLoan = e.IdLoan,
Book = new BookDTO
{
IdBook = e.Book.IdBook,
Title = e.Book.Title
},
Student = new StudentDTO
{
IdStudent = e.Student.IdStudent,
Name = e.Student.Name
}
};
问题:
如果您注意到 pred3
表达式是由 pred1
和 pred2
表达式的相同代码组成的。
那么是否可以使用 pred1
和 pred2
创建 pred3
来避免代码重复?
是可能的,但您必须在表达式 pred1
和 pred2
中调用 Compile()
方法来调用它:
Expression<Func<LoanEntity, LoanDTO>> pred3 = e => new LoanDTO
{
IdLoan = e.IdLoan,
Book = pred1.Compile()(e.Book),
Student = pred2.Compile()(e.Student)
};
但您只能使用 Func<,>
:
Func<BookEntity, BookDTO> pred1 = e => new BookDTO
{
IdBook = e.IdBook,
Title = e.Title
};
Func<StudentEntity, StudentDTO> pred2 = e => new StudentDTO
{
IdStudent = e.IdStudent,
Name = e.Name
};
那么,你可以这样使用它:
Func<LoanEntity, LoanDTO> pred3 = e => new LoanDTO
{
IdLoan = e.IdLoan,
Book = pred1(e.Book),
Student = pred2(e.Student)
};
您尝试过使用 Install-Package AutoMapper
吗?它是一个完全解决您问题的库。
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<LoanEntity, LoanDTO>();
cfg.CreateMap<BookEntity, BookDTO>();
cfg.CreateMap<StudentEntity, StudentDTO>();
});
var mapper = new ExpressionBuilder(config);
Expression<Func<LoanEntity, LoanDTO>> mappingExpression = mapper.CreateMapExpression<LoanEntity, LoanDTO>();
场景:
一些实体类:
public class BookEntity
{
public int IdBook { get; set; }
public string Title { get; set; }
}
public class StudentEntity
{
public int IdStudent { get; set; }
public string Name { get; set; }
}
public class LoanEntity
{
public int IdLoan { get; set; }
public StudentEntity Student { get; set; }
public BookEntity Book { get; set; }
}
还有一些数据传输对象类:
public class BookDTO
{
public int IdBook { get; set; }
public string Title { get; set; }
}
public class StudentDTO
{
public int IdStudent { get; set; }
public string Name { get; set; }
}
public class LoanDTO
{
public int IdLoan { get; set; }
public StudentDTO Student { get; set; }
public BookDTO Book { get; set; }
}
我已经有了这个表达式(在 dto 中转换实体):
Expression<Func<BookEntity, BookDTO>> pred1 = e => new BookDTO
{
IdBook = e.IdBook,
Title = e.Title
};
Expression<Func<StudentEntity, StudentDTO>> pred2 = e => new StudentDTO
{
IdStudent = e.IdStudent,
Name = e.Name
};
目标:
现在我想创建一个表达式,将 LoanEntity
转换为 LoanDTO
。类似于:
Expression<Func<LoanEntity, LoanDTO>> pred3 = e => new LoanDTO
{
IdLoan = e.IdLoan,
Book = new BookDTO
{
IdBook = e.Book.IdBook,
Title = e.Book.Title
},
Student = new StudentDTO
{
IdStudent = e.Student.IdStudent,
Name = e.Student.Name
}
};
问题:
如果您注意到 pred3
表达式是由 pred1
和 pred2
表达式的相同代码组成的。
那么是否可以使用 pred1
和 pred2
创建 pred3
来避免代码重复?
是可能的,但您必须在表达式 pred1
和 pred2
中调用 Compile()
方法来调用它:
Expression<Func<LoanEntity, LoanDTO>> pred3 = e => new LoanDTO
{
IdLoan = e.IdLoan,
Book = pred1.Compile()(e.Book),
Student = pred2.Compile()(e.Student)
};
但您只能使用 Func<,>
:
Func<BookEntity, BookDTO> pred1 = e => new BookDTO
{
IdBook = e.IdBook,
Title = e.Title
};
Func<StudentEntity, StudentDTO> pred2 = e => new StudentDTO
{
IdStudent = e.IdStudent,
Name = e.Name
};
那么,你可以这样使用它:
Func<LoanEntity, LoanDTO> pred3 = e => new LoanDTO
{
IdLoan = e.IdLoan,
Book = pred1(e.Book),
Student = pred2(e.Student)
};
您尝试过使用 Install-Package AutoMapper
吗?它是一个完全解决您问题的库。
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<LoanEntity, LoanDTO>();
cfg.CreateMap<BookEntity, BookDTO>();
cfg.CreateMap<StudentEntity, StudentDTO>();
});
var mapper = new ExpressionBuilder(config);
Expression<Func<LoanEntity, LoanDTO>> mappingExpression = mapper.CreateMapExpression<LoanEntity, LoanDTO>();