Best way to prevent unused properties from object

I am compiling a WCF service that handles a large search (currently 50-60 parameters, more likely to be added in the future). To handle this, I have a search object with all the criteria that will be passed to the service in the message object. Although all search parameters should be available, it often happens that 2-3 of the parameters receive user input, and the rest are zero. In my opinion, it makes no sense to pass the entire object through each method if only a few fields are used. I am looking for a method that will retrieve fields that are used with their values, which can then be checked and passed to the data layer to perform a search. A few ways to do this that I can think of include:

  • Use reflection to cycle through the properties and add non-zero properties to the Dictionary<string, object> . The problem that comes to mind here is that I am losing the type of search parameter, which means that the data level search function will be one giant case argument with hard-coded cast values ​​for each potential field. This seems to be too much, and creating too tight a connection.
  • Create a SearchValue class with the Name, Value, and System.Type properties and use reflection to build the List<SearchValue> . This still leads to checking a large case in the search, but instead it will be by type. This has something to do with making the process more generic (that is, no matter what combination of search values ​​is used), but it also feels like I'm reinventing the wheel.

What are the pros and cons of using these methods? Is there a better way to achieve my goals?

+4
source share
5 answers

I'm not sure if this works for this link, but is it possible the EmitDefaultValue property in the DataMemberAttribute?

0
source

The only problem with passing the entire object around between the client and the service is possible when there are too many empty xml nodes in the serialized request, which makes it more necessary. Are you trying to handle this? I would say that in any case, it does not really matter and it is not worth inventing complex user mechanisms.

0
source

In the first paragraph, you mention using Reflection to convert your type to Dictionary . And on the other hand, why not write the reverse logic to convert the Dictionary to your type again using Reflection ?

Example:

Client Side > YourType β†’ ( Reflection ) β†’ Dictionary > Channel> Dictionary β†’ ( Reflection ) β†’ YourType > Server Side

0
source

It comes to my mind several options:

  • Use codegen to create packaging / unpacking code for and from the dictionary. The generator code will use reflection, but there will be no packing / unpacking code.

  • Divide your search class into several smaller classes, and then specify your search class. Do not instantiate child classes unless these search parameters are used. Perhaps something like:

    • Search
      • A new class containing some common fields
      • A new class containing some other fields
0
source

You have a dataContract class with an additional message.

 [DataMember(EmitDefaultValue = false)] 

public int salary = 0;

DataContract serializer ignores such an element when the default value.

MSDN Recommendation: It is not recommended to set the EmitDefaultValue property to false. You should do this only if there is a definite need to do this, for example, to ensure compatibility or reduce data size.

You also have the IsRequired property in DataMember, setting it to false and EmitDefaultValue by reducing transport and serialization costs.

0
source

Source: https://habr.com/ru/post/1332928/


All Articles