简体   繁体   English

客户端 - 服务器代码应该写在一个“项目”还是两个?

[英]Should client-server code be written in one “project” or two?

I've been beginning a client-server application. 我一直在开始客户端 - 服务器应用程序。 At first I naturally created two projects in Eclipse, two source control repositories, etc. But I'm quickly seeing that there is a bit of shared code between the two that would probably benefit from sharing (in the same project or in a shared library) instead of copying. 起初我自然地在Eclipse中创建了两个项目,两个源代码控制存储库等等。但我很快就发现两者之间有一些共享代码可能会受益于共享(在同一个项目或共享库中)而不是复制。

In addition, I've been learning and trying test-driven development, and it seems to me that it would be easier to test based on real client components rather than having to set up a huge amount of code just to mock something, when the code is probably mostly in the client. 此外,我一直在学习和尝试测试驱动开发,在我看来,基于真实的客户端组件进行测试会更容易,而不是只需要设置大量代码来模拟某些东西,代码可能主要在客户端。 In this case it seems having the client and server together, in one project, thinly separated by root packages (org.myapp.client.* and org.myapp.server. , maybe org.myapp.shared. too). 在这种情况下,似乎将客户端和服务器放在一个项目中,由根包(org.myapp.client。*和org.myapp.server。 ,也许是org.myapp.shared。 )进行了细分

My biggest concern in merging the client and server, however, is of security; 然而,我在合并客户端和服务器时最关心的是安全性; how do I ensure that the server pieces of the code do not reach an user's computer? 如何确保代码的服务器部分不会到达用户的计算机? When Eclipse bundles a JAR, I'd have to pick out the server-specific bits and hope I don't miss any, right? 当Eclipse捆绑一个JAR时,我必须选择特定于服务器的位,并希望我不会错过任何一个,对吧?

So especially if you are writing client-server applications yourself (and especially in Java, though this can turn into a language-agnostic question if you'd like to share your experience with this in other languages), what sort of separation do you keep between your client and server code? 因此,特别是如果您自己编写客户端 - 服务器应用程序(尤其是在Java中,尽管如果您想在其他语言中分享您的体验,这可能会变成与语言无关的问题),您保持什么样的分离?您的客户端和服务器代码之间? Are they just in different packages/namespaces or completely different binaries using shared libraries, or something else entirely? 它们只是在不同的包/命名空间中,还是使用共享库完全不同的二进制文件,还是完全不同的东西? How do you test the code together and yet ship separately? 您如何一起测试代码然后单独发货?

A lot of this is going to depend on your specific implementation but I typically find that you have at least three assemblies (binaries) that are created with a project like this. 这很大程度上取决于您的具体实现,但我通常会发现您至少有三个使用这样的项目创建的程序集(二进制文件)。

  1. A Common DLL that contains shared functionality that is used by both the client and the server 包含客户端和服务器使用的共享功能的公共DLL
  2. The DLL/Exe for the client 客户端的DLL / Exe
  3. The Dll/exe for the server 服务器的Dll / exe

Using this approach you have your shared items, but you make sure that items that are server specific are never in a distribution that is sent to the client workstations. 使用此方法可以获得共享项,但是请确保特定于服务器的项永远不会位于发送到客户端工作站的分发中。

Neither. 都不是。 It should be 3. (common, client and server) However, it doesn't necessarily need to be three "projects". 它应该是3.(通用,客户端和服务器)但是,它不一定需要是三个“项目”。 Using Maven I create three sub-modules under a master project. 使用Maven我在主项目下创建了三个子模块。 You can do something similar using Ant. 你可以使用Ant做类似的事情。

I have found that at least one project per finished entity (server deployment, client binary, etc) works well with eg Hudson. 我发现每个完成的实体(服务器部署,客户端二进制等)至少有一个项目适用于例如Hudson。 Then you can have shared code in a basic project available to all. 然后,您可以在所有人都可以使用的基本项目中共享代码。

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

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