Object-oriented – How to implement business logic with Web Services

business-logicobject-orientedweb services

I'am little confused about how business logic should be implemented using web services. For example, think about an education management application. There are simply students, teachers and courses. Now, the server side of the application may provide getStudents operation via a WSDL interface. This operation returns list of Student elements.

According to object oriented paradigm a class should have a certain responsibility. It should hide its internal state and one can reach its data only using its operations. But at the client side a Student class is only a data bag. There is no logic so no responsibility here.

Another problem is that there is no reference semantics at the client side. Normally, a student is associated with some courses. But in the implementation a Student object has list of Course objects or it may hold some identifier for courses.

Finally, using web services (by WSDLs) seem to convenient for access remote data but not for execute business logic remotely. Am I right, or do I miss something important about web services?

Edit:

My intent is implement business logic at server side. For example suppose that a have classes in server side like that:

class Student
{
  //some properties like name, courses, etc.
  double calculateGPA(); //calculates average grade using course credits.
  //other operations like getName()
}

class SchoolRepository
{
  List<Student> getStudents();
  List<Course> getCourses();
  //other operations
}

Now, I can create WSDL which provide SchoolRepository interface. So, client get list of students. But they cannot reach business logic implemented in calculateGPA() directly. I may provide another WSDL interface for that. But it breaks data and behavior encapsulation.

Best Answer

Your example getStudents() is what is confusing you. I realize you've selected this as a simple example, but that's the problem. It is so simple in your mind you don't see any logic being applied.

Taken literally, you are going to return every student ever entered into the system since the beginning of time. Is there really a need for that? Does this include students who enrolled, but left the school before ever finishing one class? Do all the students have to be accepted? Have they graduated?

Once you start considering the real benefit of your application (it's not just a list of students and their coursework), you'll see where the logic is needed. Here are a few examples:

  • Enroll a student in a class, but check for prerequisites, conflicting classes, availability, faculty approval, etc.
  • Expell a student - create a workflow and notification process to get all the necessary approvals.
  • GetFullTimeStudentsByGradingPeriod(gradingperiod)- calculate the number of credit hours enrolled during the given period to determine full-time status.
  • AssignStudentCourseGrade(student, course) - Check if enrolled and is the user allowed to enter grades for this course.

If your application is nothing but a "skin" on a database, then you don't really hve much logic to worry about, but I think you're making a lot of assumptions that you'll soon learn will require logic to be performed by the service and not that application using the service.