交叉引用组件
我的 .net 解决方案中有三个项目。
主要项目和两个类库项目。
我发现我需要交叉引用类库项目。
我可以这样做吗?安全吗还是有一些注意事项?
I have three projects in my .net solution.
The main project and two class library projects.
I have found out that I need to cross reference the class library projects.
Can I do that? Is it safe or there are some considerations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当项目位于一个解决方案中时,IDE 不会允许您这样做。有一些微妙的方法可以混淆它。但是,由于程序集引用尚不可用,因此无法从头开始构建解决方案(即构建+重建)。重构它,您可能需要一个两者都可以引用的第三个程序集。
The IDE won't let you when the projects are in one solution. There are subtle ways to confuzzle it. But then the solution cannot be built from scratch (i.e. Build + Rebuild) since the assembly reference isn't available yet. Refactor this, you probably want a 3rd assembly that both can reference.
循环引用是可能的(通过命令行和一些技巧;而不是通过IDE),但这是一个很大的痛苦;别这样做!
为常见类型添加额外程序集,或者合并两个库。
在许多方面,越少越简单...无数的 dll 并不意味着您已经达到了纯粹性 - 它只是一团糟。
Circular references are possible (via the command-line and some tricks; not via the IDE) but are a major pain; don't do it!
Either add an extra assembly for the common types, or merge the two libraries.
In many ways, fewer is simpler... A myriad of dlls doesn't mean that you have achieved purity - it is just a mess.
当我遇到这个问题时,我创建了只有属性的类。可能是
Employee
、Customer
、Product,无论如何。这些类不需要引用任何其他项目,因此多个项目可以引用它们。
然后,属于这些对象(
Employee
、Customer
、Product
)的方法进入其他项目中自己的类中。我经常遇到这种情况的一种情况是在三层应用程序中——表示层、业务层和数据访问层。我希望 DAL 检索数据并填充 Employee 对象,该对象将返回到 BLL。如果 Employee 类位于 BLL 中并且同时具有属性和方法,则没有简单的方法可以在 DAL 中填充 Employee 对象并将其返回到 BLL —— 因为 BLL 必须具有对 DAL 的引用,因此DAL 无法依次引用 BLL。创建包含仅属性类(
Employee
、Customer
、Product
)的单独项目是解决此问题的一种方法。When I have run into this problem, I have created classes that only have properties. Could be
Employee
,Customer
,Product
, whatever. Those classes don't need to reference any other project, so multiple projects can reference them.The methods that belong to those objects (
Employee
,Customer
,Product
) then go into their own classes in the other projects.One situation where I have encountered this quite often is in a three-layered application - presentation layer, business layer, and data access layer. I want the DAL to retrieve data and populate an Employee object, which is returned to the BLL. If the Employee class is in the BLL and has both properties and methods, then there is no easy way to populate an Employee object in the DAL and return it to the BLL -- because the BLL must have a reference to the DAL, so the DAL cannot in turn reference the BLL. Creating the separate project with properties-only classes (
Employee
,Customer
,Product
) is one way to solve this problem.如果通过“交叉引用”,您的意思是您想要执行以下操作:
1) 项目 MAIN 包含对 LIBRARY-ALPHA 和 LIBRARY-BETA 的引用strong>
2) 项目 LIBRARY-ALPHA 包含对 LIBRARY-BETA 的引用
3) 项目 LIBRARY-BETA 包含对 LIBRARY-ALPHA 的引用
那么不。 Visual Studio 在构建 ALPHA 和 BETA 之前将无法构建 MAIN。在构建 BETA 之前,它将无法构建 ALPHA。并且在构建 ALPHA 之前,它将无法构建 BETA。因此,它将无法建造任何东西。
If by "cross reference", you mean that you want to do the following:
1) Project MAIN contains references to LIBRARY-ALPHA and LIBRARY-BETA
2) Project LIBRARY-ALPHA contains references to LIBRARY-BETA
3) Project LIBRARY-BETA contains references to LIBRARY-ALPHA
then no. Visual Studio will be unable to build MAIN until it builds ALPHA and BETA. It will be unable to build ALPHA until it builds BETA. And it will be unable to build BETA until it builds ALPHA. Therefore, it will be unable to build anything.