I am writing a proposal to replace the data access layer of the company where I work, for this I am using a generic interface to execute stored procedures using Dapper ORM.
My question is how could the code be improved?
This is the interface:
public interface IStored
{
int Execute(string Name);
int Execute<Output>(string Name, Output Parameters) where Output : class;
IEnumerable<Output> Execute<Output>(string Name) where Output : class;
IEnumerable<Output> Execute<Input, Output>(string Name, Input Parameters)
where Output : class
where Input : class;
}
And this is the implementation of the interface:
public class Stored : DataBaseContext, IStored
{
public Stored() : base() { }
public int Execute(string Name)
{
try
{
Connection.Open();
var result = Connection.Execute(Name, commandType: CommandType.StoredProcedure);
Connection.Close();
return result;
}
catch (Exception ex)
{
if (Connection.State == ConnectionState.Open)
Connection.Close();
return -1;
}
}
public int Execute<T>(string Name, T Parameters) where T : class
{
try
{
Connection.Open();
var result = Connection.Execute(Name, Parameters, commandType: CommandType.StoredProcedure);
Connection.Close();
return result;
}
catch (Exception ex)
{
if (Connection.State == ConnectionState.Open)
Connection.Close();
return -1;
}
}
public IEnumerable<Output> Execute<Output>(string Name) where Output : class
{
try
{
Connection.Open();
var result = Connection.Query<Output>(Name, commandType: CommandType.StoredProcedure);
Connection.Close();
return result;
}
catch (Exception ex)
{
if (Connection.State == ConnectionState.Open)
Connection.Close();
return null;
}
}
public IEnumerable<Output> Execute<Input, Output>(string Name, Input Parameters)
where Output : class
where Input : class
{
try
{
Connection.Open();
var result = Connection.Query<Output>(Name, Parameters, commandType: CommandType.StoredProcedure);
Connection.Close();
return result;
}
catch (Exception ex)
{
if (Connection.State == ConnectionState.Open)
Connection.Close();
return null;
}
}
}
The stored class inherits from the DataBaseContext class, which is responsible for obtaining the connection in the web.config