简体   繁体   English

防止从对象传递未使用的属性的最佳方法

[英]Best way to prevent unused properties from being passed from object

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). 我正在组建一个处理大型搜索的WCF服务(目前有50-60个参数,将来可能会添加更多参数)。 To handle this, I have a Search object with all the criteria which will be passed to the service in a message object. 为了解决这个问题,我有一个Search对象,其中包含将在消息对象中传递给服务的所有条件。 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. 虽然所有搜索参数都必须可用,但通常情况是2-3个参数接收用户输入而其他参数为空。 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: 我能想到的几种实现方法包括:

  • Use reflection to loop through the properties, and add non-null properties to a Dictionary<string, object> . 使用反射循环遍历属性,并将非空属性添加到Dictionary<string, object> 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. 这似乎是过度的,并且创造了过于紧密的耦合。
  • Create a SearchValue class with Name, Value, and System.Type properties, and use reflection to build a List<SearchValue> . 使用Name,Value和System.Type属性创建SearchValue类,并使用反射构建List<SearchValue> 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" (ie, independent of what combination of search values are used), but it also feels like I'm reinventing the wheel. 这使得该过程更具“通用性”(即,与使用的搜索值的组合无关)具有一定的吸引力,但它也感觉我正在重新发明轮子。

What pros and cons am I missing with these techniques? 我对这些技术缺少什么利弊? Is there a better way to accomplish my goals? 有没有更好的方法来实现我的目标?

The only problem with passing the whole object around is possible between client and service, when there are too many empty xml nodes in serialized request making it bigger then necessary. 客户端和服务之间传递整个对象的唯一问题是,当序列化请求中有太多空的xml节点时,必须使其更大。 Are you trying to handle this? 你想要处理这件事吗? I would say that anyway it is not a big deal and is not worth inventing complicated custom mechanisms. 我会说,无论如何这不是什么大问题,也不值得发明复杂的自定义机制。

我不确定这是否适用于此通信线路,但可能是DataMemberAttribute上的EmitDefaultValue属性?

In your first point you mention using Reflection to convert your type to a Dictionary . 在第一点中,您提到使用Reflection将您的类型转换为Dictionary And on the other side, why not write a reverse logic to convert the Dictionary back to your type using Reflection again? 而在另一边,为什么不写一个反向逻辑的转换Dictionary恢复使用型Reflection一次?

Example: 例:

Client Side > YourType >>( Reflection )>> Dictionary > Channel > Dictionary >>( Reflection )>> YourType > Server Side Client Side > YourType >>( Reflection )>> Dictionary >频道> Dictionary >>( Reflection )>> YourType > Server Side

A few options come to my mind: 我想到了一些选择:

  • Use codegen to create the packing/unpacking code into and out of a Dictionary. 使用codegen创建打包和打包字典的打包/解包代码。 The generator code would use reflection, but the packing/unpacking code would not. 生成器代码将使用反射,但打包/解包代码不会。

  • Split your Search class into multiple smaller classes, then have your Search class reference those. 将您的Search类拆分为多个较小的类,然后让您的Search类引用它们。 Don't instantiate the child classes if those search parameters aren't being used. 如果未使用这些搜索参数,请不要实例化子类。 Perhaps something like: 也许是这样的:

    • Search 搜索
      • New class containing some common fields 包含一些常用字段的新类
      • New class containing some other fields 包含其他一些字段的新类

Have a dataContract class with optional datamember. 有一个带有可选datamember的dataContract类。

[DataMember(EmitDefaultValue = false)]

public int salary = 0;

DataContract serializer ignore such member when value is default. 当值为默认值时,DataContract序列化程序忽略此类成员。

MSDN recommendation: Setting the EmitDefaultValue property to false is not recommended. MSDN建议:不建议将EmitDefaultValue属性设置为false。 You should do this only if there is a specific need to do so, such as for interoperability or data size reduction. 只有在特定需要时才应执行此操作,例如互操作性或减小数据大小。

You do also have property IsRequired in DataMember, setting it to false and EmitDefaultValue with help reducing transport and serialization overhead. 您还在DataMember中具有属性IsRequired,将其设置为false,并在EmitDefaultValue中帮助减少传输和序列化开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 防止类被实例化的最佳方法? - Best way to prevent a class from being Instantiated? 如何防止在默认编辑操作中更改Entity Framework对象的属性? - How can I prevent an Entity Framework object's properties from being altered in a default edit action? 在控制台应用程序中,防止转义命令行参数中特殊序列的最佳方法是什么? - in a console app what's the best way to prevent special sequences in command line arguments from being escaped? asp.net Core/OnPostAsync:防止记录被创建两次的最佳方法是什么? - asp.net Core/OnPostAsync: what's the best way to prevent a record from being created twice? "我的模型的列表属性没有从控制器传递给视图" - List properties of my model are not being passed to the View from the Controller 从方法更改对象属性(最佳实践) - Changing object properties from method (Best Practise) 如何防止自动实现的属性被序列化? - How to prevent auto implemented properties from being serialized? 在从不同项目传递的dll中动态检测对象属性 - Dynamically detect object properties in a dll passed from different project 防止引用引用未使用的权限 - Prevent reference from referencing unused depedency 将属性从对象复制到另一个的优雅方法 - Elegant way to copy properties from object to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM