Created
July 25, 2017 16:25
-
-
Save filipececcon/71ced3d22cc8032deeefb6f1a2afbbd6 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
using System; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.Data.SqlClient; | |
using System.Linq.Expressions; | |
using Domain.Contracts; | |
using Domain.Entities; | |
using Dommel; | |
namespace Repository | |
{ | |
public abstract class RepositoryBase<TEntity> : IRepositoryBase<TEntity> where TEntity : BaseEntity | |
{ | |
protected readonly string ConnectionString; | |
protected RepositoryBase() | |
{ | |
ConnectionString = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString; | |
} | |
public virtual IEnumerable<TEntity> GetAll() | |
{ | |
using (var db = new SqlConnection(ConnectionString)) | |
{ | |
return db.GetAll<TEntity>(); | |
} | |
} | |
public virtual TEntity GetById(int id) | |
{ | |
using (var db = new SqlConnection(ConnectionString)) | |
{ | |
return db.Get<TEntity>(id); | |
} | |
} | |
public virtual void Insert(ref TEntity entity) | |
{ | |
using (var db = new SqlConnection(ConnectionString)) | |
{ | |
var id = db.Insert(entity); | |
entity = GetById(id); | |
} | |
} | |
public virtual bool Update(TEntity entity) | |
{ | |
using (var db = new SqlConnection(ConnectionString)) | |
{ | |
return db.Update(entity); | |
} | |
} | |
public virtual bool Delete(Int32 id) | |
{ | |
using (var db = new SqlConnection(ConnectionString)) | |
{ | |
var entity = GetById(id); | |
if (entity == null) throw new Exception("Registro não encontrado"); | |
return db.Delete(entity); | |
} | |
} | |
public virtual IEnumerable<TEntity> GetList(Expression<Func<TEntity, bool>> predicate) | |
{ | |
using (var db = new SqlConnection(ConnectionString)) | |
{ | |
return db.Select(predicate); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment