在CF9中使用隐式setter和getter时,我们处理什么验证?(What do we handle validation when using implicit setters and gette

编程入门 行业动态 更新时间:2024-10-28 22:23:33
在CF9中使用隐式setter和getter时,我们处理什么验证?(What do we handle validation when using implicit setters and getters in CF9?)

使用隐式setter和getter时我们如何处理验证? 我想当使用显式的getter和setter时,我们会做类似的事情:

public void function setFirstName() { if (! len(arguments.firstName)) { //throw some error/add an error to an error container } else { variables.firstName = arguments.firstName; } }

如果我们使用隐式getter和setter,我们可以在CFC本身中定义验证规则,例如:

/** * @validate string * @validateparams {minLength=2, maxLength=40} */ property String firstName;

我的问题是:

验证逻辑是在CFC本身,还是来自调用者? 我们如何捕获错误并将其返回给用户以获得反馈? 我可以考虑使用try / catch,但这应该在控制器还是模型中?

我是OOP的新手,我依靠CFWheels为我做这一切。 我想了解在没有框架的情况下如何处理此功能,或者如果我使用的是Hibernate。

How do we handle validation when using implicit setters and getters? I imagine when using explicit getters and setters, we would do something like:

public void function setFirstName() { if (! len(arguments.firstName)) { //throw some error/add an error to an error container } else { variables.firstName = arguments.firstName; } }

If we use implicit getters and setters, we can define validation rules within the CFC itself, such as:

/** * @validate string * @validateparams {minLength=2, maxLength=40} */ property String firstName;

My questions are:

Would validation logic be in the CFC itself, or from the caller? How do we trap errors and return them to the user for feedback? I can think of using try/catch, but should this be in the controller or the Model?

I'm a newbie to OOP and I've relied on CFWheels to do all of this for me. I'd like to learn how this functionality is handled in the absence of a framework, or if I were using Hibernate, for example.

最满意答案

当我在大学做Java时,验证通常在setter中完成。 但是,在web dev中工作了一段时间之后,我注意到它通常不是一个好主意,因为您通常希望构造一批错误消息并返回给用户。 因此,对象中的validateX()方法或返回错误集合的Service层上的validateX()方法将更合适。

话虽如此,除了ValidateThis之外,还有http://hyrule.riaforge.org/如果您正在运行CF9。

When I was in college doing Java, validation was usually done in setter. However, after working in web dev for a while, I noticed that it's generally not a good idea because you generally want to construct a batch of error messages and return to the user. Therefore, a validate() method in the object, or a validateX() method at the Service layer that returns a collection of errors would be more suitable.

Having said that, beside ValidateThis, there's also http://hyrule.riaforge.org/ if you are running CF9.

更多推荐

本文发布于:2023-08-05 13:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1432727.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:隐式   getter   setter   handle   setters

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!