Best way to reuse code when using Visual Studio?

I tried 2 different code reuse methods. I have a solution full of just class library projects with common code that I use in almost every project I work on. When I start working on a new project, I will reuse the code from this code library in one of two ways. I tried to bring the projects that I need from this code library to my project. I also tried to compile the .dll file and link to the .dll from the folder at the root of my current solution. Although the second method seems simpler and easier to implement, I always find that I am making small changes to the source code to fit it in the context of my current project. I know this is a bit of a vague question, but did anyone succeed with other methods of reusing library classes for new solutions?

+4
source share
8 answers

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

If you are mistaken, you do not support it. If you need to make small β€œtweaks” [subclass] ( http://en.wikipedia.org/wiki/Subclass (computer_science) _ your existing code and extend it, do not modify it. If you need serious changes, change your mind about the design.

+8
source

I did this in both directions that you spoke of. I like the second one, but as you said, it is a little tedious when you do updates in this library.

Another way I have used is to choose a central location for storing your libraries. Then add a registry key with a string value to point to this directory. When you add a link, all your libraries will be displayed on the .net tab, as if the libraries were in the GAC. You can then create a post build command to create the library in this central location.

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

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\AssemblyFolders\ComapnyName] @="C:\\CentralLocation" 
+1
source

I like to write all my general classes as simple as: generics. I keep them as independent as possible from the application and try to not even know them. If I make a fancy Tree class, I will use generics to create it as a Tree <T> so that I can use any type that I want with the class. If I need to use Tree to store GameCharacter objects, I can create an instance of Tree <GameCharacter> but if I am writing a business application, I can use it as Tree <Spreadsheet>.

If you find yourself changing Reuse Libary to suit your projects, try making them less specific and instead get the base library classes in your real projects. Put all the common logic in the library classes and for individual parts of the application, create a derived class and implement your logic in this derived class.

Regarding the organization of solutions, I save the reuse library as a separate project in a shared folder and include the project in any solution I create, which allows me to easily reference it, but also make any changes with any of the solutions of my applications.

0
source

I do not use Visual Studio or .NET, but I think that this problem is quite common among all programmers, so I think I will hack it.

We have problems like this when we try to extract common code into a separate library. At first, this may be normal, but ultimately 1 client needs a new function and will require a change in the library. Invariably, this leads to problems with some of the other customers, creating a huge mess. If you do not have a good option for the version of your library file, this is a complex problem.

In the end, you might be better off just copying and pasting the source files into your new project. Yes, this violates the principle of DRY (Do not Repeat Yourself), but you avoid a lot of problems related to dependencies created by the shared library.

0
source

What you really need to do is use some version control software that:

  • Any change that you make in one project will reflect all projects without loss of referential integrity.
  • You may be able to store more than one version of the library that you have and reference a specific version instead of pulling hair out, figuring out which version is used, with which project

Just make sure you have unit tests in hand to make sure that your previous projects are not affected or are slightly affected by the changes made to your library.

Good luck!

0
source

@Outlaw
If you need to copy the code and make changes depending on the application, then this code is not general. it should be reorganized so that the overall functionality remains in the shared library, and the functionality of the application should be added to a subclass in this application code base.

Using version control with a branch for each specific version of the application can help with integration problems - make changes to the branch and then test them with other applications before merging them back into the trunk. This site has a few questions about good free online version control servers if you don't want to set up your own.

0
source

Some really good reusable code is in Ayende Rahien Rhino Tools . Take a look at not only how it implements various common codes, but also how they are organized.

0
source

The idea is good - put your code in a shared dll and refer to it. We use this method extensively and it works. Obviously, some projects will get out of sync eventually, the way we do it:

  • Be the least specific if you can use generic types then do it. For example, if you want to use a function that processes NULLs or nothing, and

returns something, implementing NullConvert (o as an object, subst as an object), since an object is better than NullConvert (o as an object, String) as

Line. You can then add other types, leaving the signature intact. BUT, if the type is not processed, be specific within the method and make exceptions for the raw types - do not leave it accidentally that it works. In the above example, consider the return type and see if your implementation supports this type.

  • Group functions by type in a namespace; MyGeneric.DateFns, MyGeneric.StringFns, MyGeneric.Comms, etc.

  • Do not change functionality after using a class or method, and it may be unsafe. Mark them as deprecated and include comments to indicate where the new / best class was posted.

  • You can consider two or more libraries, for example, General methods and classes in the base library, as well as domain classes and

which use common methods (from the base library) in another library (at a higher level). Thus, the base library does not have to be constantly changing.

  • Consider using "Implementations" instead of classes. If you have a function that processes an ArrayList, for example, set

parameter of your method instead of iList. Then you can use other types of lists if they implement iList inside them.

  • Avoid introducing specific features, such as an explicit driver (Oracle 8.x). Wrap it with something else, so if it changes, change

the inside of the wrapper object, not the object itself.

  • Learn how to use reflection. Let's say you need a function to get distinguishing values ​​from an array of objects. You can use reflection and

then pass the name of the / s property you need to distinguish; GetDistinct (MyList as iList, "Name") as List (String). Your code may look

a parameter called "Name" through reflection (there is a slight performance limitation).

  • Learn how to write extensions (component model). For example, if you write a function to always return formatted

include the function in the extension function. Call wisely, if your company name is ABC, then use ABCDateFormat, for example, to distinguish between your and MS functions, for example.

Too much needs to be done to be listed here. Your step in the right direction.

0
source

Source: https://habr.com/ru/post/1276487/


All Articles