7 Replies - 1598 Views - Last Post: 16 August 2016 - 11:41 AM Rate Topic: -----

#1 fearfulsc2  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 169
  • Joined: 25-May 16

Would multiple interfaces be necessary?

Posted 14 August 2016 - 08:31 PM

I am new to the .NET framework and C# in general and am working on a small project that works with databases through Entity Framework.

The idea is to add and remove classes as well as add and remove students. The student can add a class(put on a shopping cart pretty much) and can later choose to register for that class. I won't talk about the logic too much.

But the way I see it, there are people who create the courses and can add/remove students and there are students who can look at their schedule, add, and remove classes from their schedule.

Would it be a good idea to implement 2 different interfaces, or would that be redundant?

This is a sample of the program I have for a few of the things for test purposes
namespace RegistrationApp.ConsoleClient
{
  class Program
  {
    public static void Main(string[] args)
    {
      //GetStudentsDisc();
      //GetAllStudentsEF();
      //GetAllDepartmentsEF();
      //GetAllStudentInfoView();
      AddCourseSP();
      //AddStudentSP();
      Console.ReadLine();
    }

    public static void GetStudentsDisc()
    {
      AdoData ad = new AdoData();
      ad.GetAllStudents_Disconnected();
    }

    public static void GetAllStudentsEF()
    {
      EFData ed = new EFData();
      ed.GetAllStudents();
    }

    public static void GetAllDepartmentsEF()
    {
      EFData ed = new EFData();
      ed.GetAllDepartments();
    }

    public static void GetAllStudentInfoView()
    {
      EFData ed = new EFData();
      foreach(var item in ed.GetAllStudentInfo())
      {
        Console.WriteLine(item.FirstName + ' ' + item.LastName);
      }
    }

    public static void AddCourseSP()
    {
      var data = new EFData();
      string answer = "y";

      while (answer != "n")
      {
        try
        {
          Console.WriteLine("Enter Course Name You Want To Create");
          string CourseName = Console.ReadLine();

          Console.WriteLine("Enter how many the hours the course is between 1 and 2 hours");
          int hours = Convert.ToInt32(Console.ReadLine());

          Console.WriteLine("What department is the class in?");
          int department = Convert.ToInt32(Console.ReadLine());

          var starttime = new TimeSpan();
          var endtime = new TimeSpan(); 

          do
          {
            Console.WriteLine("What time does the class start?");
            var temp = Console.ReadLine();
            var newhours = temp.Split(':');
            starttime = new TimeSpan(Int32.Parse(newhours[0]), Int32.Parse(newhours[1]), 0);

            Console.WriteLine("What time does the class end?");
            temp = Console.ReadLine();
            var newhours1 = temp.Split(':');
            endtime = new TimeSpan(Int32.Parse(newhours1[0]), Int32.Parse(newhours1[1]), 0);
            
            if (endtime <= starttime)
            {
              Console.WriteLine("An Error Happened");
            }
          } while (endtime <= starttime);
          data.AddCourse(CourseName, hours, department, starttime, endtime);

        }
        catch (Exception)
        {
          Console.WriteLine("You made a mistake somewhere. Let's try again.");
          continue;
        }

        finally
        {
          Console.WriteLine("Want to enter another course? y/n: ");
          answer = Console.ReadLine();
        }

        while((answer != "y" && answer != "n"))
        {
          Console.WriteLine("Invalid input. Please enter y or n");
          answer = Console.ReadLine();
        }

      }
    }




And this is the part of the code that talks with the database
namespace RegistrationApp.DataAccess
{
  public class EFData
  {
    private RegistrationDBEntities4 db = new RegistrationDBEntities4();

    public void GetAllStudents()
    {
      var student = db.Students.ToList();
      foreach(var item in student)
      {
        Console.WriteLine(item.FirstName + ' ' + item.LastName);
      }
    }

    public void GetAllDepartments()
    {
      var dept = db.Departments.ToList();
      foreach(var item in dept)
      {
        Console.WriteLine(item.DepartmentName);
      }
    }

    public List<vw_StudentList> GetAllStudentInfo()
    {
      return db.vw_StudentList.ToList();
    }

    public bool AddCourse(string Name, int Hours, int Department, TimeSpan StartTime, TimeSpan EndTime)
    {
      return db.sp_AddCourse(Name, Hours, Department, StartTime, EndTime) != 0;
    }



So as you can see, the code is getting pretty big and I am wondering if it would be better if I implemented interfaces or maybe use delegates for the try/catch parts of the code since I am using them quite often.

My biggest issue I am having is making the design for the code as I do not know exactly where to start and I end up making code that takes up a lot of memory and takes a longer time to get things done. What is your take on this? I also plan on adding a GUI to this later.

Is This A Good Question/Topic? 0
  • +

Replies To: Would multiple interfaces be necessary?

#2 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,373
  • Joined: 02-June 10

Re: Would multiple interfaces be necessary?

Posted 15 August 2016 - 03:04 AM

Remember the point of an interface is to give common required termonology/functionality to multiple class that inherit it.
So where do you see multiple classes that would have enough in common to need it?


As I see it you'd have an interface for a person: IPerson
That has all the common things to all people: NameFirst, NameLast, etc.
It can be applied to a Student class object or a Faculty class object or a FamilyMember object: All things you would store in a school database.
Then again... If you just inherit from a Person class you can include a lot of shared functionality that actual *does* something where in an interface it just *specifies* a requirement that you still have to implement in the class. Save(), Update() etc.

Avoid the word 'Class' as its a C# keyword.. Maybe 'Course' instead.

In something as simple as this homework assignment I don't really see an need for interfaces. What 1 or 2 do you think you need?

None of the code you're making is going to take up much memory. Data on the other hand... If you try to load 20,000 student records into memory (silly)... That's another story. Your code however isnt' much at all.
Was This Post Helpful? 2
  • +
  • -

#3 fearfulsc2  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 169
  • Joined: 25-May 16

Re: Would multiple interfaces be necessary?

Posted 15 August 2016 - 06:08 AM

I was thinking a person interface and a courses interface as there are different type of people such as student, professor, admin. And then there are different functionalities that a course must have such as capacity and start-time/end-time where a course is closed when completely full and also cannot be registered for if there is a time conflict. But I was also thinking of case-switches and do things in the more "inefficient way" of doing things.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,770
  • Joined: 12-December 12

Re: Would multiple interfaces be necessary?

Posted 15 August 2016 - 12:14 PM

One thing I suggest you do is to remove methods from your Program class other than the static Main. You might consider a School or College class to hold methods such as GetAllCourses.

You Main method could, and probably should, be your test base, creating instances of your classes, calling methods on them, and outputting something to confirm that they are working.

The above suggestions have particular relevance given that you intend to build a GUI. cf modularity

Quote

But the way I see it, there are people who create the courses and can add/remove students ..

Who are these people? Are they Administrators, Faculty Members, and/or Teachers with particular permissions? Presumably you need room for them in your application, and they will want to login?

These are the bigger questions I would want to spend time on. Who will use the application and what will they want to do with it?

Quote

..and there are students who can look at their schedule, add, and remove classes from their schedule.

There is a big difference between creating/adding courses and enrolling on them. There is no commonality between these two features other than the courses themselves. Students only need to see a list of courses to then decide if they want to, and are eligible to, enrol on them. They are (probably for the good ;) ) unlikely to have much of a say on the particular courses that are offered.

Quote

And then there are different functionalities that a course must have such as capacity and start-time/end-time where a course is closed when completely full and also cannot be registered for if there is a time conflict.

These are statements about what Courses are, and how they work (within the application), there is nothing here that suggests the need for, or usefulness of, an interface.

Re-read tlhIn`toq's post.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,770
  • Joined: 12-December 12

Re: Would multiple interfaces be necessary?

Posted 15 August 2016 - 12:46 PM

I'll move this to the C# forum as it doesn't have the broader scope of the .NET Framework.

(I would also remove all the EF suffixes. And SP, what's SP? Stored procedure? These details should be transparent.)
Was This Post Helpful? 0
  • +
  • -

#6 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1233
  • View blog
  • Posts: 1,759
  • Joined: 24-August 11

Re: Would multiple interfaces be necessary?

Posted 16 August 2016 - 08:05 AM

It's easy for beginners (and even intermediate) programmers to fall into the trap of adding needless complexity to an application simply because they know that, in a vague sense, programming to an interface is the way you're supposed to do stuff. At this stage in your career, unless it's an explicit requirement of your homework, I wouldn't worry about interfaces and base classes up front; rather, build your application, and then examine your objects to see if you can find an interface or base class to extract. Do students, faculty, and administrators all have a Login() function, and a first and last name so that it can say "Hello, Joe Blow" at the top of the screen? Okay, maybe now an IPerson interface makes sense. Do they all have the exact same Login() function? Maybe an abstract Person base class makes more sense, so you can define the method exactly once and it can be called from any of the inheriting classes (to tlhIn`toq's earlier point). Are students, faculty, and administrators all user types but they don't have anything in common or really do any of the same things when they're using the system? Then there's no real need to create an interface or base class for them to share, because it is serving no real purpose and adds needless complexity.

Ultimately, you will probably end up with one: the IPerson interface or the abstract Person base class, depending on your application. The interface for Courses probably doesn't make much sense, because a Course class can have all of those properties and methods you described. But again, don't necessarily worry about creating it up front - you're still learning, so you probably won't know what it needs to do, and thus which it should be, until you've fleshed out your application a bit more. There's nothing wrong with violating the DRY principle as you start out and then refactoring as you discover more about where you're headed - until you get to a point in your career where you're consistently drawing on past experience to tackle a problem, you can't expact to foresee how your application will shake out. While patterns books and forums can no doubt give you a lot of insight, I do believe there's a lot of value in doing it youself so that you can see the interfaces and base classes naturally begin to form, and thus build your own personal intuition about what you'll need in the future.
Was This Post Helpful? 2
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5895
  • View blog
  • Posts: 20,126
  • Joined: 05-May 12

Re: Would multiple interfaces be necessary?

Posted 16 August 2016 - 08:18 AM

View Posth4nnib4l, on 16 August 2016 - 11:05 AM, said:

There's nothing wrong with violating the DRY principle as you start out and then refactoring as you discover more about where you're headed - until you get to a point in your career where you're consistently drawing on past experience to tackle a problem, you can't expact to foresee how your application will shake out. While patterns books and forums can no doubt give you a lot of insight, I do believe there's a lot of value in doing it youself so that you can see the interfaces and base classes naturally begin to form, and thus build your own personal intuition about what you'll need in the future.


This.

As much as I hate the way some teachers like to load up students with tons of homework to "drill and kill" there is some value in process of learning and discovery opportunities that these assignments offer up. I still hate homework though. :)/>
Was This Post Helpful? 0
  • +
  • -

#8 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1233
  • View blog
  • Posts: 1,759
  • Joined: 24-August 11

Re: Would multiple interfaces be necessary?

Posted 16 August 2016 - 11:41 AM

Agreed, it's definitely a love/hate relationship.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1