开发者

Patterns and Libraries for working with raw UI values

开发者 https://www.devze.com 2022-12-29 12:12 出处:网络
By raw values, I mean the application level values provided by UI controls, such as the Text property on a TextBox.Too often I find myself writing code to check and parse such values before they get u

By raw values, I mean the application level values provided by UI controls, such as the Text property on a TextBox. Too often I find myself writing code to check and parse such values before they get used as a business level value, e.g. PaymentTermsNumDays.

I've mitigated a lot of the spade work with rough and ready extension methods like String.ToNullableInt, but we all know that just isn't right. We can't put the whole world on String's shoulders.

Do I look at tasking my UI to provide business values, using a ruleset pushed out from the server app, 开发者_开发百科or open my business objects up a bit to do the required sanitising etc. as they required? Neither of these approaches sits quite right with me; the first seems closer to ideal, but quite a bit of work, while the latter doesn't show much respect to the business objects' single responsibility. The responsibilities of the UI are a closer match.

Between these extremes, I could also just implement another DTO layer, an IoC container with sanitising and parsing services, derive enhanced UI controls, or stick to copy and paste inline drudgery.


You want an API like Spring's DataValidation. It provides a nice way to validate and bind values that's written in such a way that it's even useful outside of UI.

In my mind, useful outside the UI IS orthogonal. The Spring binding API is routinely used in its web MVC implementation, but it knows nothing about controls. Even if you don't use it, I'd recommend reading about the Spring design. There's much worth emulating there.

I've only used the Java version, but there's a Spring.NET that's supposed to be quite capable.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号