-2

I am new to solid principle. I think i'm violating this kind of implementation:

class Students{
     public void Save(int id){
          if(id !== 0){
               //add students here...
          }else{
               //edit student details here...
          }
     }
}

There are many instances that you call different functionalities in one method. Is this right? or if not, please answer sample code. Thank You

CherryBlossom
  • 393
  • 4
  • 8
  • 3
    Which principle do you think you're violating? – Tudor Jul 13 '18 at 11:26
  • 1
    Can you give a more elaborate example of what to `Save` method would or should do? `if` statements are not wrong, the only thing that is wrong is that a single function does 2 completely different (and perhaps unexpected) things based on a certain condition. The name of the function should always indicate exactly what the method will do, and return. – Kwinten Jul 13 '18 at 11:32
  • 2
    Please insert the complete class as curently it's difficult to see what you are trying to achive. If `Save` is meant to write the current state of the instance, there should be no parameter required. This would make more sense for a create / update method. – Greg Jul 13 '18 at 11:36

1 Answers1

1

There are alot of articles telling what solid does. Found this one with a quick google.

https://www.codeproject.com/Articles/703634/SOLID-architecture-principles-using-simple-Csharp

As for you Students class. It is probably a repository or services to get Students or Save students or to delete student. Only doing stuff around student data. (S(ingle resposibilty))

public void Save(Student studentModel)
{
    //Only save the studentModel
}

I injected the StudentModel in this function the only thing this function need is this model to do his magic. Refering to the D of(D)ependecy injection.

Please refer the link provided. And try to uphold the SOLID principle. Makes your code eventually easier to understand/ maintain. And keep it KISS https://en.wikipedia.org/wiki/KISS_principle

RazorShorts
  • 115
  • 5