简体   繁体   中英

Best practice for sharing packages between 2 projects?

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.

管理解决方案的NuGet程序包...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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