3

I have to provide search functionality to end user and for that purpose i am providing a dropdown and a textbox, the dropdown has options like EmployeeID, DisplayName, City and after selecting the dropdown value he needs to enter a value in the textbox.

If the user selects EmployeeID then the value will be of type Int and if he selects Name or city the value will be of type string.

Public Employee getEmpInfo(object querystring, object queryvalue)
{
// code to get the empplyee info
}

I would like to know if there is any better approach than using object type ?

user3198688
  • 285
  • 1
  • 4
  • 15
  • Depends on how you are querying your objects, in simpler scenrio (where querying for different types is almost same) generics or overloading as given below would be useful. Else you can look up using dynamic linq: https://stackoverflow.com/questions/9505189/dynamically-generate-linq-queries – peeyush singh Apr 09 '19 at 09:33

3 Answers3

3

You can overload your method like this:

If your parameter is int:

public Employee getEmpInfo(int employeeId){
   // query by ID
}

Or a string

public Employee getEmpInfo(string employeeName){
    // query by name
}

Then if you give string, the second will be used by the program, else the first will be. You can both write these functions in your file. It's called method overloading.

References:

https://www.geeksforgeeks.org/c-sharp-method-overloading/

https://www.tutorialspoint.com/What-is-method-overloading-in-Chash

Function Overloading


On the other hand, if you want to use one function to search all types:

public class SearchParams {
    public int ID;
    public string Name;
    public string City;

    public SearchParams(int ID = null, string Name = null, string City = null)
    { 
         this.ID = ID;
         this.Name = Name;
         this.City = City;    
    }
}


public function getEmpInfo(SearchParams params){
    // build query by that parameters
    if(ID != null){
       // add query by ID
    }
}
Taha Paksu
  • 15,371
  • 2
  • 44
  • 78
1

It is possible to solve this in the UI by adding and binding controls for all properties and depending on the selected property name in the combobox, showing only the control for selected property.

This approach makes it possible to have very specific controls for some properties instead of forcing everything to string input.

How this should be done depends on the UI technology (Web, WinForms, WPF, ...)

Emond
  • 50,210
  • 11
  • 84
  • 115
1

In my opinion your approach is wrong.

If you search an Employee by name or by ID or by date of birth, you execute completely different actions and SQL operations.

You should have a version of your method for every possibility, to perform in every method a specific set of operations.

If you make a single method based on parameters types in override, you risk to have troubles when you have different columns with the same type.


This is wrong (and can't even compile):

public Employee GetEmployee(int id)
{
    //Instructions..
}

public Employee GetEmployee(string name)
{
    //Instructions..
}

public Employee GetEmployee(string city)
{
    //Instructions..
}

This will support every possibility and mantains readability:

public Employee GetEmployeeByID(int id)
{
    //Instructions..
}

public Employee GetEmployeeByName(string name)
{
    //Instructions..
}

public Employee GetEmployeeByCity(string city)
{
    //Instructions..
}
Marco Salerno
  • 5,131
  • 2
  • 12
  • 32