mercredi 27 avril 2016

Design pattern for saving entities into database

I have a class similar to the below (C#):

public class Product {

    public int ID {get;set;}
    public string Name {get;set;}
    public double Price {get;set;}

    public void Save() {
        string sql = "INSERT INTO Product.....";
        Database.Execute(sql);
    }

    public void Delete() {
        string sql = "DELETE Product WHERE.....";
        Database.Execute(sql);
    }
}

My main concern is that the code above violates SOLID principles, since it takes responsibility for creating and deleting itself.

Perhaps these Save and Delete methods should be placed somewhere outside the Product entity (Factory/Repository maybe?).

Aucun commentaire:

Enregistrer un commentaire