Cross referencing assemblies

天大地大妈咪最大 提交于 2019-12-01 20:26:05

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/4586334/cross-referencing-assemblies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!