访问修饰符 - 业务对象的属性 - 获取和设置
我使用 LINQ to SQL 作为数据访问层。 我有与数据访问层类似的业务对象。
我已经让数据提供者收到消息#23。在实例化消息时,在消息构造函数中,它获取 MessageType 并创建 MessageType 类的新实例,并填充来自数据库的 MessageType 信息。
所以;我希望它能够获取消息的 MessageType 的名称。
user.Messages[23].MessageType.Name
我还希望管理员设置 MessageType,
user.Messages[23].MessageType = MessageTypes.LoadType(3);
但我不希望用户公开设置 MessageType.Name。 但是当我创建一个新的 MessageType 实例时,Name 属性的访问修饰符是公共的,因为我想从外部类(我的数据访问层)设置它。
我可以将其更改为内部属性,以便我的类可以像公共变量一样访问它,并且不允许我的其他应用程序访问修改它。
这仍然感觉不对,因为它看起来像是公共财产。 在这种情况下公共访问修饰符是否不好? 任何提示或建议将不胜感激。
谢谢。
I am using LINQ to SQL for the DataAccess layer.
I have similar business objects to what is in the data access layer.
I have got the dataprovider getting the message #23. On instantiation of the message, in the message constructor, it gets the MessageType and makes a new instance of MessageType class and fills in the MessageType information from the database.
Therefore; I want this to get the Name of the MessageType of the Message.
user.Messages[23].MessageType.Name
I also want an administrator to set the MessageType
user.Messages[23].MessageType = MessageTypes.LoadType(3);
but I don't want the user to publicly set the MessageType.Name.
But when I make a new MessageType instance, the access modifier for the Name property is public because I want to set that from an external class (my data access layer).
I could change this to property to internal, so that my class can access it like a public variable, and not allow my other application access to modify it.
This still doesn't feel right as it seems like a public property.
Are public access modifiers in this situation bad?
Any tips or suggestions would be appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您始终可以这样定义您的财产:
如果这就是您正在寻找的,但有点不清楚您想要什么。什么是允许设置该属性的“管理员”?如果这意味着该属性只能由同一模块或“朋友”模块在代码中设置,那么这就是解决方案。
另一种方法是仅公开公开一个仅带有 getter 的接口
IMessage
,即MessageType MessageType { get; } }
用于“公共”模块。在您的核心模块中,您将拥有完整的Message
类,其中还有一个 setter。如果“管理员”的意思是,如果可以设置属性(例如在 GUI 中),则取决于当前用户的角色,那么就没有可以通过简单的属性设置器来解决的简单解决方案。您需要将其公开。然后,您将必须编写一些奇特的权限管理,您将使用它来评估 GUI 的该部分是否将被启用,并且您将在将值保存到数据存储之前再次使用它进行验证。
You can always define your property like that:
If that is what you're looking for, but it's a bit unclear what you want. What is an "administrator" who is allowed to set that property? If that means the property should be set only in code by the same module or a "friend" module, then that's the solution.
Another way would be to publicly expose only an interface
IMessage
with only a getter á laMessageType MessageType { get; }
for "public" modules. In your core modules you would have the fullMessage
class where you also have a setter.If you mean by "administrator" that it depends on the current user's role if the property can be set (in a GUI for instance), then there is no simple solution that can be solved by a simple property setter. You will need to make it public. Then you will have to code up some fancy rights management which you will use to evaluate if that part of the GUI will be enabled and which you will use again to validate before you save the value to your data store.