Using Dapper inside a Static Class

dapper micro-orm orm

Question

I'm building synchronization for my system. Now, I need to insert all executed stored procedure into a table. My solution was to add a static class and have Dapper run inside of it. In theory static class/functions are okay to use when they are not changing any object state which is the case. I thought I would be extra careful and ask you guys on what you think. Could it cause any problem down the line?

public static class Model
{            
    public static int ExecuteStoreProcedure(string name, string xml,bool sync=true)
    {
        using (SqlConnection con = new SqlConnection(Strings.ConnectionString))
        {
            //pre

            var result = con.Query<int>(name,
                new { xml = xml },
            commandType: CommandType.StoredProcedure).FirstOrDefault();

            //post
            // insert into new table goes here
        }
     }
}

Expert Answer

You're not accessing any shared state, and not relying on a static connection, so sure - that'll work. There are lots of ways of implementing data access, with different trade-offs, but that should work just fine. Don't fall into the "Java trap" of assuming you need 16 layers of abstraction (including an ISomething, an AbstractSomething, two Somethings, an AbstractSomethingFactory and a SomethingFactoryFactory with 200 lines of configuration in an obscure markup language) to do something simple.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why