Last active
August 29, 2015 13:57
-
-
Save droyad/9361211 to your computer and use it in GitHub Desktop.
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
| //Psudo C# | |
| public class Program(IDb db) | |
| { | |
| _db = db; | |
| void Main() | |
| { | |
| var results = _db.Query(new FooQuery().From(DateTime.Today).ForUser("doe")); | |
| } | |
| } | |
| public interface IDb | |
| { | |
| IReadOnlyList<T> Query(IQuery<T> query) | |
| void Add<T>(T o) | |
| void Delete(T o) | |
| void SaveChanges() | |
| } | |
| public interface IQuery<T> // Can be extended to have asymetric in/out types to support chaining | |
| { | |
| IQueryable<T> Build(IQueryable<TIn> source); | |
| } | |
| public class InMemoryDb : IDb | |
| { | |
| Dictionary<Type, List<object>> _data = new Dictionary<Type, List<object>>(); | |
| IReadOnlyList<T> Query(IQuery<T> query) | |
| { | |
| return query.Build(_data[typeof(T)].Cast<T>().AsQueryable()).ToArray(); | |
| } | |
| } | |
| public class EFDb : DataContext, IDb | |
| { | |
| IDbSet<Foo> Foos { get; set; } | |
| IReadOnlyList<T> Query(IQuery<T> query) | |
| { | |
| return query.Build(Set<T>()).ToArray(); | |
| } | |
| void Add<T>(T entity) | |
| { | |
| Set<T>().Add(entity); | |
| } | |
| } | |
| public class FooQuery : IQuery<Foo> | |
| { | |
| IQueryable<Foo> Build(IQueryable<TIn> source) | |
| { | |
| return source.Where(f => f.Foo == true); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment