简体   繁体   English

共享程序集

[英]Sharing Assemblies

A have a design question. 有一个设计问题。 I have wrote a couple of c# applications. 我写了几个c#应用程序。 Most of these apps have their own sql or access database, but then they also share a common database, but they are entirely different apps which have their own responsibilities in their own business domains. 这些应用程序中的大多数都有自己的sql或access数据库,但是它们也共享一个公共数据库,但它们是完全不同的应用程序,它们在自己的业务域中有自己的职责。

Some people I spoke with believe the following : Applications should be silos. 我采访过的一些人相信以下内容:应用程序应该是孤岛。 They should share nothing. 他们应该分享一切。 No sharing of assemblies, components or databases. 不共享程序集,组件或数据库。

I need advice on this. 我需要这方面的建议。 When is it acceptable to share, or is it never a good idea. 何时可以分享,或者从来不是一个好主意。

Thanks 谢谢

There is a line, somewhere... sharing library code is positively encouraged - but you need to be a little bit careful when apps consume each-other, simply to prevent spaghetti code and circular references. 在某个地方有一条线......积极鼓励共享库代码 - 但是当应用程序彼此消耗时,您需要小心一点 ,只是为了防止意大利面条代码和循环引用。

If the code you mention is really library code (ie neither project is the "master" for this data), then it should be fine (versioning issues aside). 如果你提到的代码实际上是库代码(即这两个项目都不是这个数据的“主”),那么它应该没问题(除了版本问题)。 If it is the case that Project A wants Project B's data, then it usually isn't long before Project B wants Project A's data, and you have a spider web. 如果项目A想要项目B的数据,那么通常不久之后项目B需要项目A的数据,并且你有一个蜘蛛网。 In such cases, it may be preferable to use an SOA approach; 在这种情况下,最好使用SOA方法; have the projects expose their data via something like a WCF service, so that other projects only call into the public mex/wsdl API (ideally without assembly sharing). 让项目通过类似WCF服务的方式公开他们的数据,以便其他项目只调用公共mex / wsdl API(理想情况下没有汇编共享)。

But it is a deceptively complex question, and I don't think any single answer can cater for every scenario. 但这是一个看似复杂的问题,我认为任何单一的答案都不能满足每一种情况。

Isn't that a bit the whole point of a "dll" of assambly? 这不是一个“dll”的整体点吗? To share things... 分享事情......

Why recreate and double-maintain the code when you could share things in an assembly? 为什么在可以在程序集中共享内容时重新创建和重新维护代码?

I'd say share only what was designed and intended to be shared. 我会说只分享设计和打算分享的内容。

Most times I've found code sharing just out of laziness, that is, sharing code that wasn't designed at all to be shared nor with it in mind. 大多数时候,我发现代码共享只是出于懒惰,也就是说,共享完全没有设计的代码,也不是为了共享代码。 This almost always results on problems later on when the original code needs to be changed and there's no easy way to do it because the code was intended to be used from one location and not several so the designer didn't cope with the possibility of extension for multiple "users". 这几乎总是在以后需要更改原始代码时出现问题并且没有简单的方法来执行此操作,因为代码旨在从一个位置而不是几个位置使用,因此设计人员无法应对扩展的可能性对于多个“用户”。

On the other hand you may have in mind that you are designing something to be shared, for example a communication library or an encryption library or the entire .NET framework. 另一方面,您可能想到您正在设计要共享的内容,例如通信库或加密库或整个.NET框架。 In this cases sharing is ideal and will improve both maintainability and code reuse because the design covered it from the beginning. 在这种情况下,共享是理想的,并且将提高可维护性和代码重用,因为设计从一开始就涵盖了它。

That doesn't only applies to assemblies but to classes, resources or any other piece of your project. 这不仅适用于程序集,也适用于类,资源或项目的任何其他部分。 That's the reason why design is so important , because having thought about it beforehand you have foreseen most of the possible complications and therefore the impact of sharing has been quantified and limited so that the cost of sharing is below the benefits . 这就是为什么设计如此重要的原因,因为事先考虑过它, 你已经预见到了大部分可能的复杂情况 ,因此共享的影响已经量化和限制,因此共享的成本低于收益 If you rush into sharing things without taking the consequences into account then your code will not be prepared for it and therefore the cost of adapting the code for sharing or the related problems of sharing will be quite higher than any possible benefit. 如果您急于分享事物而不考虑后果,那么您的代码将不会为此做好准备,因此调整共享代码或相关共享问题的成本将远高于任何可能的好处。

You should share everything (within the limits of common sense of course.) You are a team of developers, not a set of individual developers. 您应该共享所有内容(当然,在常识范围内)。您是一个开发人员团队,而不是一组个人开发人员。 Sharing code has numerous benefits: reusability means quicker development, simpler maintenance (you only need to fix a bug once), a new feature or improvement is potentially able to benefit all dependent projects immediately and it means sharing ideas as well. 共享代码有许多好处:可重用性意味着更快的开发,更简单的维护(您只需要修复一次错误),新功能或改进可能立即使所有依赖项目受益,这也意味着分享想法。 Personally, I have learnt a couple of tricks and good practices from the source code written by my coworkers. 就个人而言,我从我的同事编写的源代码中学到了一些技巧和良好实践。

The benefits to using dll's to share code, is that you should only have to write the code once. 使用dll共享代码的好处是,您只需编写一次代码即可。 If each application is a silo, then you have to keep multiple copies of the same code and so have to potentially fix the same bug in many places. 如果每个应用程序都是一个孤岛,那么您必须保留相同代码的多个副本,因此必须在许多地方修复相同的错误。

However sharing code between apps brings a whole series of problems with it. 但是,应用程序之间共享代码会带来一系列问题。 The instant you share code, you run into problems of different apps wanting different versions of that shared code. 在您共享代码的瞬间,您会遇到需要该共享代码的不同版本的不同应用程序的问题。 This is commonly known as dll hell . 这通常被称为dll hell Even .NET - with its system of allowing multiple versions of the same dll to coexist on a system - doesn't solve all the problems. 甚至.NET--其系统允许同一个dll的多个版本在系统上共存 - 并不能解决所有问题。 Unless you can keep all customers up to date with all products, you will still likely get into the situation of having to make the same change to different versions of the one dll. 除非您能够让所有客户了解所有产品,否则您仍然可能会遇到必须对一个dll的不同版本进行相同更改的情况。

There is no right answer on this one. 这个没有正确的答案。 Both silos and sharing bring problems with them. 孤岛和共享都会给他们带来问题。

EDIT: 编辑:

If you are referring to teams working in silos and not sharing code, then DrJokepu is right: sharing development effort between individuals and teams is a definite "must do". 如果您指的是在孤岛中工作但不共享代码的团队,那么DrJokepu是对的:在个人和团队之间共享开发工作是一个明确的“必须做”。

Sharing assemblies across applications makes sense only from theoretical point of view. 仅从理论的角度来看,跨应用程序共享程序集才有意义。 Practically it brings whole bunch of problems. 实际上它带来了一大堆问题。 Avoid assembly sharing, deploy all assemblies locally. 避免汇编共享,在本地部署所有程序集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM