.net 可配置业务错误消息
我需要实现某种解决方案,以便在我们的业务逻辑层中满足某些条件时返回错误消息。
该错误消息应该可以在文件或表中进行配置,如果需要,可以在运行时进行编辑。
我之前见过几种方法,它总是以“此错误消息是 {0}”结束,然后当开发人员使用该消息时,他们不一定知道该消息需要多少(如果有)参数。
只是希望利用可能已经完成的事情,我不认为 .net 框架中已经有提供者或任何东西。
I need to implement some kind of solution such that in our business logic layer when certain conditions are met an error message is returned.
That error message should be configurable either in a file or table that can be edited at run time if needed.
I've seen it done before a few ways and it always ends up something like "This error message is {0}" and then when the dev goes the use the message they dont neccesarily know how many (if any) parameters the message needs.
Just hoping to leverage off something that may have already been done, I dont think there is a provider or anything already in the .net framework.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个解决方案;
使用命名占位符存储错误消息,如下所示;
然后您需要一个类,该类在其构造函数中接受原始错误消息,例如“这是一些{size}问题”。
然后,该类将允许开发人员为每个占位符指定值。
最后,开发人员将调用一个方法,用指定的值替换占位符并返回结果。
IE
A solution;
Store your error messages with named placeholders like this;
Then you need a class that takes a raw error message like "this is some {size} problem" in its constructor.
The class would then allow the developer to specify values for each of the placeholders.
Finally the developer would call a method that replaces the placeholders with the specified values and returns the result.
i.e.
为什么不将错误消息作为错误类的属性(假设您有一个),允许您的开发人员设置自己的消息,也许事件包括您自己的一些静态消息,然后接受一个参数数组作为第二个string.format 函数的一部分。
似乎这可以解决不知道有多少参数等问题。
why not make the error message a property of your error class (assuming you have one), allowing your developers to set their own messages, perhaps event including a few static messages of your own, and then accepting a paramarray to put in as the second part of your string.format function.
seems like that would get around the lack of knowing how many parameters, etc.