I want to reorganize a large application with lot of code duplication into multiple components. Plus, some code is also duplicated over other applications.

The common set of functionality that can be taken out of main application is clearly defined.

Now, do I write a library or do I write a service for this functionality; so that all such applications continue to work and there is only one code-base (of common functionality) to maintain ?


There's a good reason why every heavily-used, broadly-developed Linux application is structured as a executable (e.g., /usr/bin/blarf) that handles the command line input and output, and a library (e.g., /usr/lib/libblarf.so). The idea is that the real work happens inside library functions or methods, that can be called by any program that wants to perform the operation, and that the command-line interface is just one of possibly many ways to ask for that to happen. For example, the Subversion version control system has libsvn, which is used by both the svn command and the various GUIs.

When you say "do I write a library or do I write a service", I assume by "library", you mean something like libsvn, and that by "service", you mean something like a REST interface. I'd say, start by refactoring the library out of your main program, and then build a service that uses the library if you feel the need.

| improve this answer | |
  • I see all the answers favoring library. So choosing this answer as most upvoted. – essbeev Apr 3 '12 at 6:48

How do you use the parts of the code you want to put in libraries/services?

  • If you want to use them from several machines without having to deploy the libraries on all of them, than use services (either with REST calls, or as a default WCF service, depending on your precise requirements).

  • If you want the simplest form, with no client-server communication involved, use libraries.

Note that you can use both: a library which has an associated service which acts as an interface. This way, you can distribute the library when appropriate, and access it through the service in other cases.

| improve this answer | |
  • It would be single sequence of actions which will be requested by multiple external applications, not simultaneously. – essbeev Mar 31 '12 at 12:29

The fact that you have "code duplication" means that your code is running in-process so I would go with a library. That is going to wrap up your code for you anyway so as Ross Patterson one should always go with that option and build on on top of the library.

Out-of-process is always going to be slower but there may be advantages. You could go with Rest, WS, XML-RPC, or even a service bus (especially when doing asynchronous calls). However, it depends totally on how you need to reach your code.

If there is no direct need or requirement for an out-of-process solution I would avoid it.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.