-1

I have 3 methods these are same methods only some parameters will be change I want to write one method how can i write

        public string method1(int id)
    {
        var getAllStudents = rep.Students.Where(e => e.StudentId == id).ToList();
        foreach (var item in getAllStudents)
        {
            if (item.isActive != true)
                return "Error";

        }

        return "OK";

    }
    public string method2(int id)
    {
        var getAllTeachers = rep.Teachers.Where(e => e.TeacherId == id).ToList();
        foreach (var item in getAllTeachers)
        {
            if (item.isActive != true)
                return "Error";

        }

        return "OK";

    }
    public string method3(int id)
    {
        var getAllClasses = rep.Classes.Where(e => e.ClassId == id).ToList();
        foreach (var item in getAllClasses)
        {
            if (item.isActive != true)
                return "Error";

        }

        return "OK";

    }

I think there is very easy way to write 1 method. the think is where parameter has different id.. Thanks.

John
  • 23
  • 4
  • That doesn't work because at least, when creating the database command, you have to distinguish between the various tables you want to get data from. You could write one method where you pass a type and put a `switch` statement in it, but you wouldn't win a lot with that – schlonzo May 15 '18 at 07:21

4 Answers4

1

Avoid conditional logic based on arguments. This leads to fragile code because every parameter combination has to be tested to be considered reliable. This leads to complex code that is easily prone to bugs. Having simpler single-purpose methods are typically much more reliable and easier to understand and maintain.

For instance given your example and assuming that "rep" was your instance's DbContext...

public bool IsActiveStudent(int id)
{
   bool result = rep.Students.Any(x => x.StudentId == id && x.IsActive);
   return result;
}
public bool IsActiveTeacher(int id)
{
   bool result = rep.Teachers.Any(x => x.TeacherId == id && x.IsActive);
   return result;
}
public bool IsActiveClass(int id)
{
   bool result = rep.Classes.Any(x => x.ClassId == id && x.IsActive);
   return result;
}

These can be essentially one-liners by simply returning the .Any() result. I tend to favour selecting the result into a variable first and returning it on a separate line since it makes it easier to breakpoint and inspect.

If you need to return a string for "Ok" vs. "Error" then:

return result ? "OK" : "Error";

Methods should strive to do one thing, and do it well. Easy to understand and troubleshoot if need be. Adding parameters and conditional code inside the method merely makes the code more volatile and leaves openings for bugs. In the end it doesn't make the code much shorter when the initial method could be simplified.

Steve Py
  • 26,149
  • 3
  • 25
  • 43
0

You can not overload methods if they signatures are the same.

You have two methods with the same signature:

public string checkexist(int id)

What you can do is to rename your methods, like this:

public interface WriteSomethingHere { 

   public boolean isStudentExist(int id);
   public boolean isTeacherExist(int id);
   public boolean isClassExist(int id);
}
zappee
  • 20,148
  • 14
  • 73
  • 129
0

I just found answer using generic repo

public T GetEntity<T>(int Id)
where T : class
    {
        using (MyEntities rpContext = new MyEntities())
        {
            return rpContext.Set<T>().Find(e => e.Id == Id);
        }
    }

after calling

var entityStudent = GetEntity<Student>(1);
var entityTeacher = GetEntity<Teacher>(1);
var entityClasses = GetEntity<Classes>(1);
John
  • 23
  • 4
-1

You have Create Enumeration

  Public Enum ParameterStaus:short
  {
    Student=1,
    Teacher=2,
    Classess=3
  }
  public string method2(int id.ParameterStatus status)
  {
   if(status==ParameterStatus.Teacher)
   {
    var getAllTeachers = rep.Teachers.Where(e => e.TeacherId == id).ToList();
    foreach (var item in getAllTeachers )
    {
        if (item.isActive != true)
            return "Error"; 

      }

      return "OK"; 
    }
  }
  Else if(status==ParameterStatus.Student)
   {
      var getAllStudents = rep.Students.Where(e => e.StudentId == id).ToList();
     foreach (var item in getAllStudents)
     {
         if (item.isActive != true)
            return "Error";

      }

     return "OK";
   }
   Else
   {
     var getAllClasses = rep.Classes.Where(e => e.ClassId == id).ToList();
     foreach (var item in getAllClasses)
     {
        if (item.isActive != true)
            return "Error";

      }

     return "OK";
  }
}
kari kalan
  • 497
  • 3
  • 20