0

I ask if the following (example-class) code-snippets (having both the INTERFACE and the functional-CLASS-code in the same source-file) is good programming practice -OR- should the functional CLASS-code be moved to a separate code-file? The "IStudentService.cs" is the name of the combined source-file.

Based on the limitation imposed by SO moderator(s), to clarify my question -- it is related to the repository-pattern. Many Blazor/Maui tutorials and blogs suggest the repository-pattern. Since we are a small development team with many DB-tables and related services and repositories, I find it cumbersome to have so many source-files related to the application's functionality. I agree that (in the future) should we ever move to another implementation of the database tables we would then separate the functional-code (implementation) into its own source-file and place the additional implementation into its own source file. Comments on this clarification?

namespace Application.Services {
   public interface IStudentService {
      public Task<IEnumerable<Student>> GetAllStudents();
      public Task<Student> GetStudentByUID(int uidStudent);
      public Task<IEnumerable<Student>> GetStudentsByClassUID(int uidClass);
      public Task<IEnumerable<Student>> GetStudentsByMajorUID(int uidMajor);
   }

   // functional-CLASS-code.
   public class StudentService : IStudentService{
      public async Task<IEnumerable<MOTRIP>> GetAllStudents() {...}
      public async Task<Student> GetStudentByUID(int uidStudent) {...}
      public async Task<IEnumerable<Student>> GetStudentsByClassUID(int uidClass) {...}
      public async Task<IEnumerable<Student>> GetStudentsByMajorUID(int uidMajor) {...}    
  }
}

Thank you.

John D
  • 517
  • 7
  • 22
  • I think Interfaces should be in the same modules as the class code they are using. – jdweng May 15 '23 at 14:37
  • 1
    It depends. Personally lately I prefer either move interfaces and DTOs into a separate project called something like "Contracts" or do not use interfaces at all. – Guru Stron May 15 '23 at 14:41
  • the whole concept behind using the interface is when you have more than one implementation and at runtime you will decide which one should take place so having them in different directories like Contracts makes your code more readable and you should always avoid declaring unnecessary interfaces when you have only one implementation – Khashayar Pakkhesal May 15 '23 at 14:57

1 Answers1

0

when separate interface from classes gives you the advantage of better maintainability let's just think of a scenario when you need interface segregation and you have to separate properties of an interface or even if you have more than one implementation of an interface which is the whole concern of creating an interface where you are going to put the new implementation? surely you can't put it in the same file as the first implementation so you end up having 2 files one includes only implementation and the other includes implementation and interface that's when you realize this interface doesn't belong here so in my opinion separating them gives you the advantage of better maintainability and more readable code because no one expects two service files that one of them includes an interface