Quantcast
Channel: URF - Unit of Work & (extensible/generic) Repositories Framework
Viewing all articles
Browse latest Browse all 1539

Edited Unassigned: QueryObject: There's no Method Chaining for Or Operator [1222]

$
0
0
In order to do method chaining with _Or_ operator, it would be necessary to save the current __query_ state in object in _Or_ method.



#QueryObject.cs (current)
```
#region

using System;
using System.Linq.Expressions;
using LinqKit;
using Repository.Pattern.Repositories;

#endregion

namespace Repository.Pattern.Ef6
{
public abstract class QueryObject<TEntity> : IQueryObject<TEntity>
{
private Expression<Func<TEntity, bool>> _query;
public virtual Expression<Func<TEntity, bool>> Query()
{
return _query;
}

public Expression<Func<TEntity, bool>> And(Expression<Func<TEntity, bool>> query)
{
return _query == null ? query : _query.And(query.Expand());
}

public Expression<Func<TEntity, bool>> Or(Expression<Func<TEntity, bool>> query)
{
return _query == null ? query : _query.Or(query.Expand());
}

public Expression<Func<TEntity, bool>> And(IQueryObject<TEntity> queryObject)
{
return And(queryObject.Query());
}

public Expression<Func<TEntity, bool>> Or(IQueryObject<TEntity> queryObject)
{
return Or(queryObject.Query());
}

protected void Add(Expression<Func<TEntity, bool>> predicate)
{
_query = (_query == null) ? predicate : _query.And(predicate.Expand());
}
}
}
```

#QueryObject.cs (proposal)
```
#region

using System;
using System.Linq.Expressions;
using LinqKit;
using Repository.Pattern.Repositories;

#endregion

namespace Repository.Pattern.Ef6
{
public abstract class QueryObject<TEntity> : IQueryObject<TEntity>
{
private Expression<Func<TEntity, bool>> _query;

public virtual Expression<Func<TEntity, bool>> Query()
{
return _query;
}

public Expression<Func<TEntity, bool>> And(Expression<Func<TEntity, bool>> query)
{
return _query = _query == null ? query : _query.And(query.Expand());
}

public Expression<Func<TEntity, bool>> Or(Expression<Func<TEntity, bool>> query)
{
return _query = _query == null ? query : _query.Or(query.Expand());
}

public Expression<Func<TEntity, bool>> And(IQueryObject<TEntity> queryObject)
{
return And(queryObject.Query());
}

public Expression<Func<TEntity, bool>> Or(IQueryObject<TEntity> queryObject)
{
return Or(queryObject.Query());
}
}
}
```
Add Method is not necessary any more; Use And or Or, instead. For example:

```
void Main()
{
var query = new BookQuery()
.OrElseTitleContains("Civitas")
.OrElseEditorContains("Civitas");

var q = new UnitOfWork(this).Repository<Book>().Query(query).Select();

q.Dump();
}

public class BookQuery: QueryObject<Book>
{
public BookQuery OrElseTitleContains(string text)
{
Or(book => book.title.Contains(text));
return this;
}

public BookQuery OrElseEditorContains(string text)
{
Or(book=> book.Editor.editor.Contains(text));
return this;
}
}
```

Viewing all articles
Browse latest Browse all 1539

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>