code_junkie

Repository Pattern without LINQ or other ORM

Nov 13th, 2011
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. public abstract class GenericRepository : IRepository {
  2. public virtual T Get<T>(Identity id) where T : PersistentDocument {
  3. using (IDbConnection connection = GetConnection())
  4. using(IDbCommand command = CreateGetCommand(id, connection)) {
  5. using (IDataReader reader = command.ExecuteReader()) {
  6. var mapper = DaHelper.GetMapper<T>();
  7. return mapper.Map(reader);
  8. }
  9. }
  10. }
  11.  
  12. protected virtual IDbCommand CreateGetCommand(Identity id, IDbConnection connection) {
  13. IDbCommand command = connection.CreateCommand();
  14. command.CommandText = String.Format("SELECT * FROM {0} e WHERE e.id = ?", TableName);
  15. command.Parameters.Add(id.ToGuid());
  16. return command;
  17. }
  18.  
  19. protected abstract string TableName { get; }
  20. }
  21.  
  22. public class UserRepository: GenericRepository<User>, IUserRepository
  23. {
  24. protected override string TableName { get { return "users"; } }
  25.  
  26. public User GetByEmail(string email)
  27. {
  28. using (IDbConnection connection = GetConnection())
  29. using (IDbCommand command = connection.CreateCommand())
  30. {
  31. command.CommandText = String.Format("SELECT * FROM {0} e WHERE e.email = ?", TableName);
  32. command.Parameters.Add(email);
  33. using (var reader = command.ExecuteReader())
  34. return DaHelper.GetMapper<T>().Map(reader);
  35. }
  36. }
  37. }
Add Comment
Please, Sign In to add comment