1
\$\begingroup\$

I'm creating a little proposal for the data access layer using Dapper ORM.

The code is based in an generic interface IStore:

public interface IStored
{
    int Execute(string Name);
    int Execute<Output>(string Name, Output Parameters) where Output : class;
    IEnumerable<Output> ExecuteReader<Output>(string Name) where Output : class;
    IEnumerable<Output> ExecuteReader<Input, Output>(string Name, Input Parameters)
        where Output : class
        where Input : class;
}

This is the implementation:

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> ExecuteReader<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> ExecuteReader<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;
            }
        }
    }

Is this code a good proposal? What can I do to improve it?

\$\endgroup\$
  • \$\begingroup\$ Could you tell us what DataBaseContext is? \$\endgroup\$ – t3chb0t Jul 14 '18 at 19:23
  • \$\begingroup\$ Consider using an abstract class instead of an interface. It looks like you will have many repos with this same functionality. \$\endgroup\$ – Plixxer May 15 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.