Note that there are some explanatory texts on larger screens.

plurals
  1. POBest way to prevent unused properties from being passed from object
    text
    copied!<p>I'm putting together a WCF service that handles a large search (currently 50-60 parameters, with more likely to be added in the future). To handle this, I have a Search object with all the criteria which will be passed to the service in a message object. While all the search parameters must be available, it is often the case that 2-3 of the parameters receive user input and the others are null. To my mind, it doesn't make sense to pass the entire object around through every method if only a few fields are used. I'm looking for a technique that will extract the fields that are used with their values, which can then be validated and passed to the data layer to execute the search. A few ways to accomplish this that I can think of include:</p> <ul> <li>Use reflection to loop through the properties, and add non-null properties to a <code>Dictionary&lt;string, object&gt;</code>. The problem that comes to mind here is that I lose the type of the search parameter, which means the data layer search function will be one giant case statement with hard-coded cast values for each potential field. This seems to be overkill and creating way too tight of coupling.</li> <li>Create a SearchValue class with Name, Value, and System.Type properties, and use reflection to build a <code>List&lt;SearchValue&gt;</code>. This still results in a big case check in the search, but rather than by property it would be by type. This has some appeal in making the process more "generic" (i.e., independent of what combination of search values are used), but it also feels like I'm reinventing the wheel.</li> </ul> <p>What pros and cons am I missing with these techniques? Is there a better way to accomplish my goals?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload