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.
CreateStudentjust straight-up imply that the student is not created and therefore the ID will be obtained from the database? No need for separate interfaces. Otherwise I guess you will need aIKnownStudentwhich is more work and silly