Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Way to Reuse Code When Using Visual Studio?

I've tried two different methods of reusing code. I have a solution full of just class library projects with generic code that I reuse in almost every project I work on. When I get to work on a new project, I will reuse code from this code library in one of two ways:

  1. I have tried bringing the projects I need from this code library into my project.
  2. I have also tried compiling down to a .dll and referencing the .dll from a folder in the root of my current solution.

While the second method seems easier and lighter to implement, I always find myself making small tweaks to the original code to fit it into the context of my current project.

I know this is a bit of a vague question, but has anyone had success with other methods of reusing class libraries on new solutions?

like image 381
Mark Struzinski Avatar asked Jan 24 '23 03:01

Mark Struzinski


2 Answers

In short, what you are doing is right, you want to move the common code into a class library (DLL) and then reference that in any projects that require its logic.

Where you are going wrong is that you are not maintaining it. If you need to make little "tweaks", subclass your existing code and extend it, dont change it.. If there are major changes needed, then re-think the design.

like image 144
Rob Cooper Avatar answered Feb 04 '23 12:02

Rob Cooper


I have done it both ways that you mentioned. I like the second, but like you said, it is a little tedious for when you make updates to that library.

Another way that I have used, is choosing a central location to keep your libraries. Then add a registry key with a string value to point to that directory. When adding a reference, all your libraries will show up under the .net tab, just like if the libraries were in the GAC. Then you can make a post build command to build the library to that central location.

Here's the registry key, change CompanyName and the directory:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\AssemblyFolders\ComapnyName]
@="C:\\CentralLocation"
like image 26
Dale Ragan Avatar answered Feb 04 '23 13:02

Dale Ragan