mercredi 27 octobre 2021

Which one of the SOLID Principles are the most violated? [closed]

This question was asked to me by a colleague and we are divided between Single Resonsability and Depency Inversion. Which of the solid principles do you think is the most violated in this ActiveRecord pattern?

public class WorkItem{
     
        private string _id;
        private string _name;

        public void Save(){
            SqlConnection cnx = new SqlConnection(ConfigurationManager.ConnectionStrings["database"]);
            cnx.Open();

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = cnx;
            if (_id){
                  cmd.CommandText = "INSERT INTO WorkItem (Id, Name) VALUES ('";
            }else{
                  cmd.CommandText = "UPDATE WorkItem SET Id ...'";
            }

            cmd.CommandText += _id + "','" + _name + "')";
            cmd.ExecuteNonQuery();
            cnx.Close();
        }
        
        public void GetById(string id){
            SqlConnection cnx = new SqlConnection(ConfigurationManager.ConnectionStrings["database"]);
            cnx.Open();            
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = cnx;
            cmd.CommandText = "SELECT Id, Name FROM WorkItem where Id = '" + id + "'";

            SqlDataReader dr = command.ExecuteReader();
            
            if (dr.Read()){
                _id = dr["id"].ToString();
                _name = dr["name"].ToString();
            }else{
                return null;
            }
        }
    }

Aucun commentaire:

Enregistrer un commentaire