在属性中实现逻辑是否是一个好习惯 [英] Is it a good practice to implement logic in properties

查看:50
本文介绍了在属性中实现逻辑是否是一个好习惯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们将ASP.NET与C#结合使用,并基于我经过的开源项目/文章,我发现许多属性都包含逻辑,但是当我这样做时,组长告诉我这不是擅长将逻辑放在属性内,但可以通过方法调用逻辑...

we use ASP.NET with C# and based on open source projects/articles I passed through, I found many properties were including a logic but when I did so the team-leader told me it's not good at all to place logic inside properties but to call the logic through methods...

那真的很糟糕吗?为什么不在属性中使用逻辑?

is that really bad? and why not to use logic in the properties?

谢谢

推荐答案

属性访问应该是瞬时的(无需长时间的等待),一致的(无更改的值)和安全的(无例外).如果可以保证,我认为在属性中放入逻辑是可以的.

Property access is expected to be instantaneous (no long waits), consistent (no changing values), and safe (no exceptions). If you can make those guarantees, I think putting logic in properties is OK.

这篇关于在属性中实现逻辑是否是一个好习惯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆