使用存储库模式进行类封装
我正在使用存储库模式从数据库中提取信息。我有两个类,report 和 reportRepository。
我遇到的麻烦是,由于 reportReposity 必须填写报告对象的所有详细信息,因此 报告 中的所有成员都必须可公开访问。
有没有一种方法可以确保只有存储库类可以访问报表类的某些方法,并且只有它可以设置其他类不能设置的一些属性,有点像 friend在c++中是这样的。或者有完全不同的方法来处理这种情况吗?
我在 ASP.NET 2.0 中使用 C#
I am sortof using a repository pattern to extract information from a database. I have two classes, report and reportRepository.
The trouble I have is that since reportReposity has to fill in all the details for the report object, all the members in the report have to be publicly accessible.
Is there a way so that I can ensure that only the repository class can access some of the methods of the report class and only it can set some of the properties that other classes cannot, sort of like what friend does in c++. Or is there a completely different way of handling this situation?
I am using C# in ASP.NET 2.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这看起来有点时髦,但它做了你之后的事情,任何带有“受保护”修饰符的属性都只能在该类和任何派生类中访问
所以除非你在存储库中,否则你看不到Person 派生类 PersonBuilder,它具有一个填充 Person 受保护属性的构造函数。从外部看,它看起来就像您神奇地填充字段,而无需使用设置器或构造函数。
This looks a bit funky but it does what your after, any property with a 'protected' modifier will only be accessable within that class AND any derived classes
So unless your within the repository you can't see the Person derived class PersonBuilder which has a constructor that populates the protected properties of Person. Externally it looks like your magically populating fields without using setters or constructors.
您可以将访问修饰符应用于属性,例如:
Internal 为同一程序集中的任何类型提供对指定属性的写访问权限。
You can apply access modifiers to properties, e.g:
Internal gives write access to the specified property for any types in the same assembly.