NuGet Package vs Web API – When to Create Each

apiArchitecturenuget

As our devs are writing internal applications, often times we need to share code. There are various ways to do this, but it usually comes down to creating a nuget package on a shared server or hosting a web api internally. We don't have a definitive way to decide between the 2 and currently have a mixture of both. Any suggestions/guidance of when to do one over the other?

To get a little more specific… Say I have a few general use methods to FormatXXX(), CalculateXXX(), SendXXX(), etc… that many apps could benefit from. Should this be an internal web api, or a nuget package?

We are a .Net shop, and use VS 2017, TFS (with git). We mostly build web apps, web api's, wcf services, and command line apps. We interface with 3rd party api's all the time.

Best Answer

If its a pure function then you want the code to execute on the same CPU as the app. a nuget package is a good way of getting that code into your project.

If the function has side effects that you want to be global. ie every time this function runs we MUST generate an audit log. or, This function must only have a single instance running at a time and stop when we reach 1000 calls per day! Or more commonly, this function accesses a common database and we need to stop people deleting it. Then you need to control the way the function is used and must host it yourself. Only exposing an API for consumers to call.

The down side of a hosted API is that you have to host the server, probably a in a fail-over cluster so there is a cost involved. Plus sending the call over the network is slow.

A note on shared code solutions:

It's been suggested that you use shared code instead of NuGet packages for internal projects.

While this is a solution the downside over nuget is that each application that uses the code will build its own version of the package. So you lose the benefits of versioning, signing, multiple platform builds etc that you can get from nuget or other package managers.

I would suggest its best to treat you internal 'customers' the same as external ones and let them pull all the packages, internal and external from nuget.

Related Topic