Created
October 30, 2018 14:33
-
-
Save angusbreno/860931917168bc6f63963e2fa3455ec3 to your computer and use it in GitHub Desktop.
Generic Procedurer Service
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
public interface IDataProcedureService | |
{ | |
Procedure LoadProcedure(string procedure); | |
IEnumerable<T> Execute<T>(Procedure procedure); | |
} | |
public class Procedure | |
{ | |
public Procedure(IDataProcedureService dataProcedureService) | |
{ | |
this.dataProcedureService = dataProcedureService; | |
} | |
private Dictionary<string, object> parameters; | |
private readonly IDataProcedureService dataProcedureService; | |
public Procedure WithParameter(string parameter, object value) | |
{ | |
parameters.Add(parameter, value); | |
return this; | |
} | |
public IEnumerable<T> Execute<T>() | |
{ | |
return dataProcedureService.Execute<T>(this); | |
} | |
} | |
public class EntityFrameworkProcedureService : IDataProcedureService | |
{ | |
private readonly DbContext dbContext; | |
public EntityFrameworkProcedureService(DbContext dbContext) | |
{ | |
this.dbContext = dbContext; | |
} | |
public IEnumerable<T> Execute<T>(Procedure procedure) | |
{ | |
var cmd = dbContext.Database.GetDbConnection().CreateCommand(); | |
//cmd. monta o command aqui bem bonitinho | |
using (var reader = cmd.ExecuteReader()) | |
{ | |
return reader.Cast<T>(); | |
} | |
} | |
public Procedure LoadProcedure(string procedure) | |
{ | |
return new Procedure(this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment