Best practice for sharing packages between 2 projects? - c#

I know that many people use the patern of having two projects, AppName and AppName.API. What happens when both of them projects require a class from the same package.
In my mind I have two options, create another project (AppName.Common) and reference that project on both AppName and AppName.API, or just add the package to both projects via Nuget.
Which is the best practice for this? Have I even got this right, is there an option I've missed? I'm a beginner to programming and just looking for some guidance towards advanced topics.
Many thanks.

NuGet was created for package management and Visual Studio handles package management for you. "Best Practice" for this question is subjective, in my opinion. What I recommend is that you add both projects to a solution. Then, right-click on your solution in the solution explorer and choose the Manage NuGet Packages for Solution menu option.
Then, from the package manager, install your package into both projects of your solution. When you move this solution from machine to machine, or someone else works on it, they can simply restore packages to install the dependencies. Your packages will be stored in a common folder at the solution level.

Just add the package to both projects via Nuget. The transitive reference approach (depend on Common just for dependencies) has pains the second you have 3 projects where two need a dependency but the third doesn't, at which point you've introduced a pointless dependency.
Don't overcomplicate package management or project layouts; always keep things simple or they (as a rule of thumb) will get in your way as you scale.
Edit: You mentioned you were new to programming. You don't always need X.Impl and X.Api (nor is that better than simply having X), and until you know WHY you might need them, it's going to be best to keep things simple. Your goal as a new programmer is likely to learn programming. If you overcomplicate your project structure & application architecture, you're going to waste time wrangling that vs learning, well, programming. Focus on writing as little code as possible, as few abstractions as possible, and creating as few csprojs as possible (ideally 1).

I agree Nuget is best for package management. But IMO it’s not best to use it if it’s not a generic package and not used by many teams and projects.
For your purpose I feel using a common class library and referencing it in both projects works very well and sorts out the debugging problems with remote packages.

Related

Determine which projects use my Nuget package on Github?

I've got a few Nuget packages that are SDKs for third-party APIs. Now, their download count is in the low thousands, but I know a large proportion of that is going to be from my CI/CD pipelines and me doing nuget restores whilst developing and whatnot.
So, I'd like to know if anyone is actually using my projects. It's not the end of the world if not, because I made them because I needed them, but it'd be good to know if I've helped someone.
Is there any way to reliably establish if any projects are consuming my nuget package, assuming that these are published publicly on Github? The best I can think of is just googling the name of my package but that's not proving very fruitful.
Using Github itself might work. Maybe there is a good method around, but this is the best I could find get in my three minutes test.
https://github.com/search?l=XML&q=RestSharp.105&type=Code
https://github.com/search?l=XML&q=Hypermedia.Core&type=Code
https://github.com/search?l=XML&q=DiscosWebSdk&type=code
Is way less than perfect, but better than Google for the matter at hand.
Is there any way to reliably establish if any projects are consuming my nuget package?
The closest would be, in nuget.org/packages, the Used By section which lists:
NuGet packages that depend on xxx,
GitHub repositories that depend on xxx.

Is there a way to have multiple applications reference a global project that has other project references

After searching quite a bit, I seem to be unable to find an answer to my problem. Usually, I find that means it is a non existent or incorrect approach, but I think it worth it to have an answer floating around on the internet nonetheless.
Essentially, we have 4 applications referencing 5 different "source" projects. So the scenario is, when we add a 5th application (for example), we will need to create project references to the other 5 different projects, as the application requires their output.
Its not a difficult task because the amount of projects is small, but it got us thinking. What if you could create a single project, maybe called Libs or something, reference all 5 projects in that project, and then the applications must only reference Libs. The idea seems cool, but I don't know if it will work because when you create a project reference, it points to Libs single output libs.dll.
So to actually ask a question, is this possible, and if so, how can it be done? Currently, having Libs reference the other "source" projects, and then having the applications reference the Lib project does not work, as it says there are missing assemblies.
And just to go over how this was created. The 5 source projects reside in a couple different solutions, so the only tedious part of this approach is "add existing project" at the initial start of the application's solution.
The way we manage this kind of thing in my organisation is to make a NuGet package for each of these shared "source" projects (e.g. in our case we have an error logging library, an XML utils library, a bespoke HTTP client, and others). These are published to our private NuGet feed URL (hosted on Azure DevOps, but you can just use a standard Windows fileshare if necessary) for our developers to use in their applications.
This has some clear advantages over your approach:
1) Dependencies - this seems most relevant to your question. If the project you built the NuGet package from itself depends on any other NuGet packages (either publicly available ones, or others from our private feed) then when someone installs that package in their project it will automatically install all the other packages it depends on.
So in your case you could create a shell "libs" package which doesn't deliver any content itself, but has dependencies on all your other packages, causing them to be installed automatically. In our case we have several cases of dependency (e.g. a "base" error logging package which is relied on by error handling modules which are tailored to different app types, e.g. MVC, Web API, Windows Services), and it works very well.
2) Updates and maintenance. In your scenario if you make a breaking change to one of your "source" projects, then, because you have a direct project reference declared in Visual Studio, any project which references the source one will have to make related changes to cope with the updates to the source project, before you can re-compile it and do whatever feature changes you're trying to achieve. This could be a pain, and be an untimely problem, especially in the case of major updates. However if instead you install a NuGet package containing that functionality, the developer of the application can choose if and when to install an updated version of the package.
There are other minor benefits as well which I won't go into, but there are some very good reasons why almost all major programming languages now provide similar "package" and "feed" functionality as a way of managing dependency on external projects and libraries. Your approach is, I feel, outdated and naive, resulting in the issue you've described and the potential for other irritations as well.

Best Practices for Transitive Package References in NuGet

I am relatively new to the new .NET project format, and was looking to solicit some opinions on how best to manage a library's dependencies.
Let's say I am writing a library (intended to be shared as a NuGet package) which contains some ASP.NET core functionality.
In my library's csproj file, I can add a reference to Microsoft.AspNet.Core and happily use everything I want from it or any of its transitive dependencies.
An alternative approach would be to explicitly add references for each part of AspNet.Core that I use, as I start using types from those packages. For example, to add a package reference to Microsoft.AspNetCore.Http.Abstractions the first time I use HttpContext.
To me, the latter approach seems like it would be preferable. It provides a more clear description of the actual dependencies of my package, and is not susceptible to breaking in the same way as referencing Microsoft.AspNet.Core may be when updating to a version which may remove Microsoft.AspNetCore.Http.Abstractions from its transitive dependency graph.
If the general consensus is indeed the latter approach described above, is there any way to enforce this behaviour? For example, by failing a build if a project references types in a transitive dependency?
Really interested to hear everyone's thoughts on this!
I'm an experienced C# developer and maintainer of several NuGet packages. Though those packages don't actually have any dependencies.
I agree that the latter approach of only referencing the exact packages used is preferable. This has several advantages:
Helps prevents conflicts between assembly versions by reducing the number of packages your package pulls in
Minimizes the total added size to projects using your package
May enable advanced scenarios for users of your package
I recently had to file a bug due to the third situation. The xUnit library provides their assertions in two different packages xunit.assert and xunit.assert.source. The second enables you to extend the assertion class with custom assertions. I was using the xunit.categories package and it references xunit which references several packages including xunit.assert. So when I tried to switch to using xunit.assert.source I got a conflict because they contain the same classes. However, xunit.categories doesn't even use the assertion portion of xUnit. It should only reference xunit.core.
I think I prefer explicitly referencing packages. However, I can understand the answer may be subjective.
In any case, there is a tool that helps you find transitive package references:
https://github.com/spectresystems/snitch

How do you go about using 'lib' folders in .Net projects for building in dependencies?

I need to get my external dll dependencies (automap, others ...) into a situation where I can build them on my build server. I would also like to get then them into subversion, so the build server can pick them up.
So I'm new to the whole 'lib' folder thing. I've searched Google, but it seems it's kind of assumed, there are no basics of what to do here. The books I own don't go into it. It's been a long time since I had a mentor at work, or even someone I could ask questions of ... and I'd really love to understand the fundamentals of what I should be doing here.
I write in .Net, use Jenkins as my CI server (new to that) and msbuild (new to that too). I'm hearing svn:externals (don't compute), NuGet ....
Please help!
Suppose my solution is called MySolution and is stored in C:\MySolution, then I have have three directories for binaries, all managed by source control.
vendor the source code of third party frameworks. If needed they are built and signed (with my key) and treated as if the code was my own. This is sometimes necessary to "fix" defects in the framework or debug their source to understand why it fails.
src\packages modules managed by nuget (I wished to combine this with my "lib" folder, but that isn't yet supported)
lib compiled libraries for which I don't have the source and that are not managed by nuget.
(I have omitted folders like "src", "sample", "setup", "documentation" and "scripts" to keep the answer specific to the OP).
The recent months I started to create my own nuget packages for "packages" in the lib folder so I can migrate all of them to "packages". Its published to a private nuget server. It also simplify managing the binaries across solutions.
I use to use externs, but they pose a branching nightmare after a while because you have to branch and pin the external dependencies to. With nuget this is no longer needed.
I would definitely avoid putting binaries in source control. You'd be far better off creating your own nuget repository containing your preferred versions of packages and either using nuget restore or some other way of "rehydrating" your dependencies for building. I use a simple batch file called nuget-update.bat which just looks at all packages.config files and gets any dependencies it finds.
It seems that you posted a sequence of questions on the same topic. I recommend NuGet, as it becomes critical and promoted hard by Microsoft. However, many old libraries are not available there, and you may still need to keep a lib folder. My open source project #SNMP is a good example,
I tried to use as many NuGet packages as possible, and even stepped up to maintain some of the dependencies, such as DockPanel Suite.

Looking for an msbuild and xbuild task to fetch referenced libraries (without nuget.exe)

I have an issue with creating an easy solution for my build system, based on mono.
Current situation is that I keep my referenced libraries inside the git repository, which is not good, for many obvious reasons.
What I want to achieve is something like what NuGet provides - automatically download dlls from the Web, put them in some directory and forget about them.
I want to do this at build time, so it would not require any additional actions with downloading libraries etc. The best option would be an msbuild (xbuild on mono) task, but I want it to be system independent, so the popular one, executing NuGet.exe, is out of question (consider parallel mono installations, etc.).
I've tried Pepita project, but it's... wrong. No, really, it is, it has too many design mistakes to be easy to use or repair. To make a proper configuration would require a serious rewrite of the whole project.
What I would love is a library, that would employ NuGet.Core library and be available as a task. If such a lib is not there, I could use any solution, that would download a nuget package and unpack it to a directory specified in .csproj.
Even better, it would be nice if such a library could resolve dependencies without specifying them explicitly in packages.config (or similar) file, e.g. if I want to include Castle.Windsor I don't want to include Castle.Core in my config file.
I know about the OpenWrap project (with NuGet Gallery), it looks promising, but I can't find the solution where I would just put a constant set of libraries in my repo once, modify csproj files, some configs and have it done.
I can tell you that OpenWrap at the core has everything built-in to do what you want. Everything you can do with the openwrap-shell is also available to be called from msbuild. So, it seems to me that you would just need to add a before build hook to call out to openwrap to perform an "update-wrap". Several months back I actually looked into doing something similar. AFAIR I actually wrote an msbuild script to call openwrap tasks, but didn't really hook them into the normal build process.
I don't know exactly what you mean with "put a constant set of libraries in your repo once"? For OpenWrap, all you need to do is maintain the "openwrap descriptor" for your project. That file contains all direct dependencies of your project (with or without restrictions on version numbers). (Indirect dependencies are pulled in automatically) Are you wondering about how you get started when you have a bunch of binary dlls to start with? I can tell you what I did. Basically, I do not use any NuGet packages, I created OpenWrap packages for everything. I also created OpenWrap packages for all our binary dependencies (some of which are open-source). This is really super simple: you fill in correct dependencies in the OpenWrap descriptor and specify that the package must only contain the given dlls. We had a bunch of binary dependencies, but once you start packaging them, it's definitely not that much work.
If you want to see an example, you can check this one:
http://code.google.com/p/ppwcode/source/browse/dotnet/External/Apache.Log4Net/trunk/Apache.Log4Net.wrapdesc
That is all you have to do to package your binary dependencies. This is a package I created and we currently use it in the company where I work. I know Log4Net is probably available as a NuGet package, and I could probably use that. The advantage of creating those binary packages myself, is that I have full control over the packages, over the version numbering of the packages, over how a big project is split over several smaller packages and so on.
As an OpenWrap repo, you can use a folder on your local filesystem, or a folder on a network share. What we use, is actually a webdav repository that we mount locally on a drive (using Windows 7). This works fine for us and also allows us to specify who has read and write access to the repository.
You mention mono.... well, that might be a problem: the currently released version of OpenWrap (2.0.2) does not run on mono AFAIK. But the good news is that Sebastien Lambla has been working hard to get OpenWrap to run on mono+xbuild for the new version that is going to be released very soon: 2.0.3. No alpha/beta builds available yet, but you can build from git. (In that case you would need to build both openwrap-shell and openwrap). Sebastien Lambla, who created OpenWrap, normally keeps an eye on questions on StackOverflow and will probably be able to give you a more complete answer on the mono status.
Btw, where I work, we are using OpenWrap already for over a year. Back then we compared both NuGet and OpenWrap, and at that moment OpenWrap was way way way ahead of NuGet. Basically, to me, NuGet was not a tool for dependency management, but a tool to assist you in Visual Studio to pull in binary dependencies from a remote server (meaning: copy dll from remote server to local folder and add reference to local dll in project file). In the mean time, NuGet has been playing catch-up with OpenWrap and has added functionality that already existed in OpenWrap. There are in my opinion only 2 things that NuGet has over OpenWrap and that is integration in Visual Studio (aka overview of remotely available packages and click-click-click adding of packages) and the fact that it is maintained by Microsoft people (AFAIK). Both things are just political: it's easier to convince people with a pretty interface and microsoft support. Personally, however, I think that OpenWrap is technically superior and I think it's really a pity that it doesn't get the attention that it deserves.

Categories

Resources