How can I simplify these two methods to prevent code repetition?
Method 1, with the first expression having a generic property of type IEnumerable < >:
public GenericDAO<TModel> LeftJoin<TProperty>(
Expression<Func<TModel, IEnumerable<TProperty>>> joinMember,
Expression<Func<TProperty, TModel, bool>> filter)
where TProperty : class
{
var joinMemberMetadata = new Mapping.MapperClass<TProperty>().DoMapper();
var joinMemberExpression = joinMember.Body as dynamic;
var filterExpression = filter as dynamic;
var paramThisModel = filterExpression.Parameters[0].ToString();
var paramJoinMember = filterExpression.Parameters[1].ToString();
_query = _query.Trim() + " left join " + joinMemberMetadata.TableName + " as " +
joinMemberExpression.Member.Name + " on ";
_query += filterExpression.Body.ToString()
.Replace(paramThisModel, _metadataOfClass.TableName)
.Replace(paramJoinMember, joinMemberExpression.Member.Name)
.Replace("==", "=")
.Replace("&&", "and");
return this;
}
With this method I can use lambda expression in a property of type list, like this:
libroDAO.Query().LeftJoin(livro => livro.Autores, ...)
Method 2, the first expression of this method no longer has the
public GenericDAO<TModel> LeftJoin<TProperty>(Expression<Func<TModel, TProperty>>
joinMember, Expression<Func<TProperty, TModel, bool>> filter)
where TProperty : class
{
var joinMemberMetadata = new Mapping.MapperClass<TProperty>().DoMapper();
var joinMemberExpression = joinMember.Body as dynamic;
var filterExpression = filter as dynamic;
var paramThisModel = filterExpression.Parameters[0].ToString();
var paramJoinMember = filterExpression.Parameters[1].ToString();
_query = _query.Trim() + " left join " + joinMemberMetadata.TableName + " as " +
joinMemberExpression.Member.Name + " on ";
_query += filterExpression.Body.ToString()
.Replace(paramThisModel, _metadataOfClass.TableName)
.Replace(paramJoinMember, joinMemberExpression.Member.Name)
.Replace("==", "=")
.Replace("&&", "and");
return this;
}
With this method I can do the same thing for properties of simple classes:
livroDAO.Query().LeftJoin(livro => livro.Editora, ...)