Should I use the static DbUtil class for the Internet, is it dangerous?

Is it possible to use something like this on the Internet: (my application is on asp.net mvc)

public static class DbUtil
{  
      public static int Insert(object o, string cs)
        {
            using (var conn = new SqlConnection(cs))
            using (var cmd = conn.CreateCommand())
            {
              ...

                conn.Open();
                return Convert.ToInt32(cmd.ExecuteScalar());
            }
        }
}

using:

public class Repository<T> : IRepository<T>
{
       public virtual int Insert(T o)
       {
            return DbUtil.Insert(o, Cs);
       }
}

and after injection of the designer into the service or controller

    public MyController(
        IRepository<Organization> organizationRepository)
    {
        this.organizationRepository = organizationRepository;
    }
+3
source share
2 answers

It is absolutely normal to use this static class while it is hidden and wrapped in a repository, as is the case in your case. This allows a weaker connection between the controller logic using the repository and data access. The static method you showed looks great reentrant , which makes it thread safe.

+2

, concurrency, , .

, , , . , , .

+1

Source: https://habr.com/ru/post/1767871/


All Articles