code_junkie

.net architecture: Creating a IRepository<T> generically

Nov 12th, 2011
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. public class OracleRepository<T> : IRepository<T> where T : new()
  2. {
  3. public IQueryable<T> GetAll()
  4. {
  5. if (typeof(T) == typeof(Object1))
  6. {
  7. return (IQueryable<T>)DataAccess.GetAllObject1().AsQueryable();
  8.  
  9. }
  10.  
  11. if (typeof(T) == typeof(Object2))
  12. {
  13. return (IQueryable<T>)DataAccess.GetAllObject2().AsQueryable();
  14. }
  15.  
  16. throw new NotImplementedException();
  17. }
  18. }
  19.  
  20. private static readonly IDictionary<Type, Func<object>> Fetchers =
  21. new Dictionary<Type, Func<object>>();
  22. {
  23. (typeof(Object1), () => DataAccess.GetAllObject1().AsQueryable()),
  24. (typeof(Object2), () => DataAccess.GetAllObject2().AsQueryable()),
  25. // etc
  26. };
  27.  
  28. public IQueryable<T> GetAll()
  29. {
  30. Func<object> func = Fetchers[typeof(T)];
  31. if (func == null)
  32. {
  33. throw new NotImplementedException();
  34. }
  35. return (IQueryable<T>) func();
  36. }
Add Comment
Please, Sign In to add comment