Skip to content

11.4 Refactor CircleDao

  1. Change the first lines of the CircleDao class definition:

    1
    2
    3
    4
    5
    public class CircleDao : AbstractDao
    {
        public CircleDao(DbAccess dbAccess) : base(dbAccess)
        {
        }
    
  2. Add a helper method below SaveCircle:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    private int SaveShape(string color)
    {
        string sql = "INSERT INTO Shape(color) VALUES (@color);";
        int primaryKey = 0;
        try
        {
            var cmd = new MySqlCommand(sql, _dbAccess.GetConnection());
            cmd.Parameters.AddWithValue("@color", color);
            cmd.ExecuteNonQuery();
            cmd.CommandText = "SELECT LAST_INSERT_ID();";
            primaryKey = Convert.ToInt32(cmd.ExecuteScalar());
        }
        catch (MySqlException ex)
        {
            Console.WriteLine(ex);
        }
        return primaryKey;
    }
    
  3. Simplify the SaveCircle method:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    public void SaveCircle(Circle circle)
    {
        int primaryKey = SaveShape(circle.Color);
        string sql = "INSERT INTO Circle VALUES (@id, @radius, @x, @y);";
        try
        {
            var cmd = new MySqlCommand(sql, _dbAccess.GetConnection());
            cmd.Parameters.AddWithValue("@id", primaryKey);
            cmd.Parameters.AddWithValue("@radius", circle.Radius);
            cmd.Parameters.AddWithValue("@x", circle.Center.X);
            cmd.Parameters.AddWithValue("@y", circle.Center.Y);
            cmd.ExecuteNonQuery();
        }
        catch (MySqlException ex)
        {
            Console.WriteLine(ex);
        }
    }
    
  4. Add the following UML class diagram:

    Mermaid diagram Mermaid diagram

    The PointDao and CircleDao classes are subclasses of AbstractDao. Therefore, these classes have access to the attributes of AbstractDao.

    • They also have access to the shared methods used to handle SQL statements.
    • The PointDao class only contains methods that are specific to objects of the Point class.
    • The CircleDao class only contains methods that are specific to objects of the Circle class.
  5. Change the code in your Main method:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    DbAccess dbAccess = new DbAccess("Server=localhost;Database=Shapes;User=userShapes;Password=userShapesPW;");
    dbAccess.OpenConnection();
    
    PointDao pointDao = new PointDao(dbAccess);
    pointDao.SavePoint(new Point(8, -12));
    foreach (Point point in pointDao.GetPoints())
        Console.WriteLine(point);
    
    CircleDao circleDao = new CircleDao(dbAccess);
    circleDao.SaveCircle(new Circle(6.5, new Point(-2.3, -1.3), "purple"));
    
    dbAccess.CloseConnection();
    
  6. Run your program and verify in Workbench that the point and circle were added.