Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public abstract class GenericRepository : IRepository {
- public virtual T Get<T>(Identity id) where T : PersistentDocument {
- using (IDbConnection connection = GetConnection())
- using(IDbCommand command = CreateGetCommand(id, connection)) {
- using (IDataReader reader = command.ExecuteReader()) {
- var mapper = DaHelper.GetMapper<T>();
- return mapper.Map(reader);
- }
- }
- }
- protected virtual IDbCommand CreateGetCommand(Identity id, IDbConnection connection) {
- IDbCommand command = connection.CreateCommand();
- command.CommandText = String.Format("SELECT * FROM {0} e WHERE e.id = ?", TableName);
- command.Parameters.Add(id.ToGuid());
- return command;
- }
- protected abstract string TableName { get; }
- }
- public class UserRepository: GenericRepository<User>, IUserRepository
- {
- protected override string TableName { get { return "users"; } }
- public User GetByEmail(string email)
- {
- using (IDbConnection connection = GetConnection())
- using (IDbCommand command = connection.CreateCommand())
- {
- command.CommandText = String.Format("SELECT * FROM {0} e WHERE e.email = ?", TableName);
- command.Parameters.Add(email);
- using (var reader = command.ExecuteReader())
- return DaHelper.GetMapper<T>().Map(reader);
- }
- }
- }
Add Comment
Please, Sign In to add comment