确保封装的建议
在我的解决方案中,我有一个 DLL 项目(例如 Project1
),它拥有一个类(例如 class A
)。该类有一些非常重要的属性,只能在该 DLL 中设置。引用此 DLL 的任何其他项目只能使用 getter。到目前为止,我已经通过内部访问设置器来处理这种情况,例如:
public class A
{
int Num1
{
get;
internal set;
}
int Num2
{
get;
internal set;
}
}
现在我必须将另一个项目 Project2
添加到解决方案中,该解决方案不仅必须使用 class A
(getter 和 setter)而且,Project1
还引用了这个新项目。因此,我决定将 A 类
分离到层次结构顶部的不同定义 dll 中。这就是我的问题所在。现在,所有 setter 对于 Project1
和 Project2
都是不可见的。如果我删除 internal
访问器,那么引用定义 dll 的任何程序集都可以使用 setter。我不想这样,因为class A
的信息非常重要,一定不能设置错误。
如何限制从 Project1
和 Project2
外部对 class A
的 setter 的访问?
我想到的唯一解决方案是使用InternalsVisibleTo。我不知道,听起来并不完美。另外,将 Project1
和 Project2
结合起来也是另一种解决方案,但这两个解决方案负责完全不同的任务,这不是设计方面的最佳选择。
In my solution I have a DLL project, say Project1
, that owns a class, say class A
. This class has some properties, so vital that must only be set in this DLL. Any other projects that references this DLL can only use the getters. So far I have handled the situation by accessing the setters internally, like:
public class A
{
int Num1
{
get;
internal set;
}
int Num2
{
get;
internal set;
}
}
Right now I have to add another project, Project2
, to the solution that not only must use class A
(both getters and setter) but also, Project1
has a reference to this new project. So I decided to separate class A
in a different definition dll, on the top of the hierarcy. That is the point, my problem araises. Now all the setters are invisible to Project1
and Project2
. If I remove the internal
accessor then setters will be available to any assembly that references the definition dll. I do not want this because the information on the class A
is so important, it must not be set mistakenly.
How can I limit the access to the setters of class A
from outside of Project1
and Project2
?
Only solution comes to my mind is using InternalsVisibleTo
. I do not know, it doesn't sound perfect. Also combining Project1
and Project2
would be another another solution, but these two are responsible for completely different tasks, it is not the best option designwise.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将内部保护器保留在其上,但给予另一个程序集访问内部成员的信任。
您可以在类级别执行此操作,如果需要,也可以在整个程序集本身上执行此操作。
InternalsVisibleToAttribute 类
You can keep the internal protector on it, but give another assembly the trust to access the internal members.
You can do this at the class level, or on the entire assembly itself if you want.
InternalsVisibleToAttribute Class
如果我正确地假设 Project1 执行一些特定的操作,而 Project2 是其之上的抽象,那么这是控制反转的候选者 - 理想情况下,实现应该取决于在抽象上,而不是相反。
如果 Project2 中的内容需要控制 Project1 中定义的内容,请在 Project2 中创建一个接口并让 Project2 与该接口通信,然后在 Project1 中实现该接口并在内部完成所有脏工作。
If I'm right in assuming that Project1 does some specific stuff, and Project2 is an abstraction on top of that, then this is a candidate for inversion of control - ideally, an implementation should depend on an abstraction, not the other way around.
If things within Project2 need to control something defined in Project1, create an interface in Project2 and have Project2 talk to the interface, then implement the interface in Project1 and do all the dirty work internally.
你总是可以做这样的事情(设计的噩梦,但做了你要求的事情):
然后你可以正常实例化类......当你需要设置使用反射的值时:
you could always do something like this (a nightmare of a design but does what you asked for):
and then you could instanciate the class normally... when you need to set the values you use reflection: