Created
February 6, 2015 06:15
-
-
Save droyad/44caec54af007300f742 to your computer and use it in GitHub Desktop.
Query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
IDb db; | |
new PersonNameProjection( | |
new PersonQuery(db.Table<Person>()).ByName("Tod") | |
).Execute(); | |
} | |
public interface IDb | |
{ | |
IQueryable<T> Table<T>(); | |
} | |
public class Query<T, TQuery> where TQuery : Query<T,TQuery> | |
{ | |
IEnumerable<T> _query; | |
public Query(IQueryable<T> query) | |
{ | |
_query = query; | |
} | |
protected TQuery Where(Func<T,bool> clause) | |
{ | |
_query = _query.Where (clause); | |
return (TQuery) this; | |
} | |
public IEnumerable<T> Query | |
{ | |
get{ | |
return _query; | |
} | |
} | |
public IReadOnlyList<T> Execute() | |
{ | |
return _query.ToArray(); | |
} | |
} | |
public class Person | |
{ | |
public string Name { get; set;} | |
} | |
public class PersonQuery : Query<Person, PersonQuery> | |
{ | |
public PersonQuery(IQueryable<Person> query) : base(query) | |
{ | |
} | |
public PersonQuery ByName(string name) | |
{ | |
return Where(p => p.Name == name); | |
} | |
} | |
public class PersonNameProjection | |
{ | |
private PersonQuery _query | |
public PersonNameProjection(PersonQuery query) | |
{ | |
_query = query; | |
} | |
public string[] Execute() | |
{ | |
return _query.Query.Select(p => p.Name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment