简体   繁体   English

C#如何实现执行多次检查的Save方法?

[英]C# How to implement a Save method which performs multiple checks?

Often I have to deal with a save method which has to check a few things before saving to the database. 通常,我必须处理一个save方法,该方法必须先检查一些内容,然后再保存到数据库中。

Things I check for, for example, are empty properties, and values which depends on other properties. 例如,我检查的是空属性,其值取决于其他属性。

I am always struggling on how to implement such a things. 我一直在努力实现这种事情。 The questions I ask myself are: 我问自己的问题是:

  • Should I use a boolean as a return value for the Save method? 我应该使用布尔值作为Save方法的返回值吗? And in the client code, check of it is false, show the end-user a messagebox with: "Saving failed". 并在客户端代码中检查是否为假,并向最终用户显示一个消息框,其中显示:“保存失败”。 But the problem is, I can't show the user why saving is failed. 但是问题是,我无法向用户显示为什么保存失败。 So I don't like this one very much. 所以我不太喜欢这个。
  • In my Save methods, should I throw an Exception? 在我的Save方法中,我应该抛出异常吗? So when a check fails, a Exception is thrown? 因此,当检查失败时,会引发异常吗?
  • Do nothing. 没做什么。 When a check fails, just don't do anything. 如果检查失败,则什么都不做。 But I think this is not really a option. 但是我认为这不是一个选择。

I was wondering, how do you implement this? 我想知道,您如何实现这一目标? Is there kind of a pattern? 有没有一种模式?

imho it's not up to the save method to validate the object. 恕我直言,这不是由save方法来验证对象的。 It should be valid when Save is called. 调用“保存”时它应该有效。 It's therefore OK to throw an exception if the object is not specified correctly. 因此,如果未正确指定对象,则可以引发异常。

As for validation, there are built in framework in .NET which is called DataAnnotations. 至于验证,.NET中内置了一个称为DataAnnotations的框架。 Use it for easier validations in all layers. 使用它可以更轻松地在所有层中进行验证。

You should not return strings but should throw exceptions. 您不应返回字符串,而应引发异常。 You can create specific exceptions like EmptyPropertyException, DateOutOfBoundException etc. and throw them. 您可以创建特定的异常,例如EmptyPropertyException,DateOutOfBoundException等,然后将其抛出。 Now it is up to the client to catch these exceptions to show the right error messages. 现在由客户端来捕获这些异常以显示正确的错误消息。 Using exceptions is better as you may want to show slightly different error messages in different places or may need to use localized strings. 使用异常更好,因为您可能希望在不同的位置显示略有不同的错误消息,或者可能需要使用本地化的字符串。

Very nice implementation is offered by Enterprise Library - Validation block. Enterprise Library-Validation块提供了非常好的实现。 Please see level of details and ease of implementation on following location: 请在以下位置查看详细程度和易于实施:

http://msdn.microsoft.com/en-us/library/ff953192%28v=PandP.50%29.aspx http://msdn.microsoft.com/en-us/library/ff953192%28v=PandP.50%29.aspx

It dependes on what you need and on what user needs... 这取决于您需要什么以及用户需要什么...
Basicly you could return a string: if is null or empty everything is ok, on the contrary this string is the error. 基本上,您可以返回一个字符串:如果为null或为空,则一切正常,相反,此字符串为错误。 So you can show a MessageBox or take whatever action you please. 因此,您可以显示一个MessageBox或执行您喜欢的任何操作。
Usually I prefer doing nothing server-side (if there is an error) and returning the error itself, so client-side I can take actions or let user decide what to do. 通常,我更喜欢在服务器端不做任何事情(如果有错误),并且自己返回错误,因此在客户端,我可以采取措施或让用户决定要做什么。
You can even return an exception from server if there is an error or null if everything went ok. 如果出现错误,您甚至可以从服务器返回异常;如果一切正常,则可以返回null。

如果您想知道保存失败的原因,请使用它们所针对的异常。

我将使用类似结果状态的结果-具有布尔属性的对象,显示操作是否成功,如果发生某些错误并发生异常,则向用户显示一条消息。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM