0
How can I simplify these two methods in order 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 list type property, like this:
libroDAO.Query().LeftJoin(livro => livro.Autores, ...)
Method 2, the first expression of this method no longer has Ienumerable<>:
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 simple class properties:
livroDAO.Query().LeftJoin(livro => livro.Editora, ...)
And why not register? It’s so easy, nor need to fill out any registration.
– Maniero