4

I have a few classes that are abstracted in a way that I can use them in multiple projects. I'm always working on these classes, optimizing, adding, etc. So when I optimize something in one of these classes, I then need to copy that new version into every project I remember using it. This isn't a very good way of doing it, but is there a better way?

Thanks

Jess
  • 8,628
  • 6
  • 49
  • 67
  • 1
    All answers mention *Class Library* projects, which are usually the way to go, though there can be reasons to reference files instead of compiled libraries; please refer to my answer. – Marius Schulz Nov 23 '11 at 09:12

5 Answers5

5

Put these base classes in a single project and share this project between your different solutions as an referenced class library. This way you will not have to copy / paste anything between projects or solutions and everything should always be up to date.

You could even set-up a local NuGet feed so you can use NuGet to retrieve this shared project as a reference in a well structured and managed way.

Johannes Kommer
  • 6,401
  • 1
  • 39
  • 45
  • Agree shearing files between projects is not always the best way of doing things. Try to extract common staff into one or several libraries. Unless you have a real reason of not doing this. Let us know if there is one. – George Mamaladze Nov 23 '11 at 09:04
2

Instead of manually copying the updated classes to every project that uses them, create a Class Library project and reference the compiled file in every project that uses the classes. Organizing your classes like that will help you to follow the DRY ("Don't repeat yourself") principle.


If you need to reference files instead of compiled libraries, however, you can reference a file as a link so that multiple projects refer to the same file without copying it to each solution folder. To do that, right-click on your project, choose Add existing item..., browse to the .cs file, and choose Add as Link from the combobox in the right lower corner.

Marius Schulz
  • 15,976
  • 12
  • 63
  • 97
1

How about if you extract the classes into a separate project, and add a reference to this project in every project you are using?

misha
  • 2,760
  • 3
  • 28
  • 30
1

It is a bad idea to copy paste file throughout the application. To avoid these repetitions you can either:

  1. make a link, if the amount of file is really small . In the Solution browser of Visual Studio, right click, Add Existing file, chose your file and in the split button, choose Add as a link
  2. create a separate project and reference this project wherever is is necessary if the amount of files not tiny.
JiBéDoublevé
  • 4,124
  • 4
  • 36
  • 57
0

Create a base-lib and build it to a "shared" location. Add a reference to it in you project. It will keep the other projects smaller and will be faster to build.

Asken
  • 7,679
  • 10
  • 45
  • 77