g_b
g_b

Reputation: 12428

Advice on class structure

I have a class Student which needs to be persisted in the database. I have methods that create and update these students (CreateStudent, UpdateStudent) and right now, the structure of this Student class is:

public class Student
{
    public int ID { get; set; }
    public string FirstName { get; set; }
}

Now what I am thinking is my CreateStudent accepts a Student object:

public int CreateStudent(Student newStudent);

However, since this student is new, the ID wouldn't be persisted (or shouldn't be persisted) to the database. But it seems unclear to the user of the method that this is how it works. For example, I used CreateStudent but passed a Student.ID of 6, the CreateStudent method would ignore the ID since this is creating a student. However, I am trying to find something that is clearer. What I want to try now is separating the ID to an interface which would only be available when a Student is already existing in the database. Sort of like this:

public IEntity
{
    int ID { get; set; }
}

public interface IUnknownStudent
{
    string FirstName { get; set; }
}

public interface IStudent : IUnknownStudent, IEntity
{
}

Then when using CreateStudent, I pass a IUnknownStudent (no ID). Only when retrieving or updating will I use the implementation with an ID. But I am not sure if this has any problems since its the first time I'm trying it, and I was wondering if the experienced guys here can give some advice about this.

EDIT:

CreateStudent() is on a separate class, StudentLogic.

Upvotes: 1

Views: 70

Answers (1)

Trey50Daniel
Trey50Daniel

Reputation: 179

One thing I see is that you would probably want a separate class for StudentData in which you have the method

public int createStudent(Student s){
//TODO: Implement method here
}

The reason that I believe that you should have another class to house the method for createStudent is because that you shouldn't have to instantiate a Student to access the createStudent method. Think of it this way, say I create my class Student

public class Student 
{
    public string firstName { get; set; }
    //rest of class
}

When I go to create a Student using the createStudent method inside the class, I would have to say this:

Student s = new Student();
s.createStudent(s);

And that is not really how we want to have to create things, right? That code would be hard to read and understand if you want to keep this updated.

I agree that you'd want to have the ID implemented as a separate class or interface, depending on what you need, and you might want to have a default value for "ID" in the event of an Unknown New Student being created. This way, when you return the ID of the new Student, it's always the same until the user input the new ID for the Student or the next ID was automatically selected. The main thing that I suggest is using a StudentData class to house the createStudent method and house your main function, if you have one. This way, you're using more of an MVC(Model-View-Controller) style of Development, with a Student and their ID as the Model and the View and Controller being handled by the StudentData class. It's just an easy way to structure things once you understand it.

Upvotes: 1

Related Questions