Improved for Interface of stored with Dapper

0

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

    
asked by betoramiz 26.08.2016 в 19:32
source

1 answer

0

Because Dapper and not Entity Framework

Code First Insert / Update / Delete Stored Procedures

with this you could map procedure to entities and also if you want you can some entity let the ORM itself generate the queries.

What if I do not recommend that you generate a class such as Stored , but you should implement the pattern Repository

[Entity Framework] [Code First] Create simple entity

Then you can generate a RepositoryBase with common functionality, but you can also extend it, but the design is done according to the entity that represents your business model.

    
answered by 26.08.2016 / 22:51
source