lundi 30 mai 2022

Is it right to write DB Operations in a POJO or DTO Class?

Let's say we have a Class Customer

Class Customer
{
    String name, emailId;

    public void setName(String name)
    {
       this.name = name;
    }
    public String getName()
    {
       return this.name;
    }
  
    //Getter and Setter for Email ID

    public void addNewCustomer()
    {
        //DB Operations
    } 
}

Now I have to persist a new row to the Database table Customer.

Do I add a method addNewCustomer as above and access it as below ?

Customer customer = new Customer();
customer.addNewCustomer();

Or

I should have a separate Class say,

Class CustomerUtil
{
     public static void addNewCustomer(Customer customer)
     {
        //DB Operations
     }
}

Is there any standards that we could follow here ?

Aucun commentaire:

Enregistrer un commentaire