23

How can I have code-sharing between two projects without making a dll?

The issue is: I have a tool that syncs users & groups from LDAP to a database.

Now the tool is a windows service, but testing it as such is very difficult and time consuming.

Which is why I made a console application where I can test the LDAP syncing, and then just copy the respective sourcecode-files over to the service project.

But... keeping the common files in sync is a bit of a problem. I don't want to make a dll, because this probably creates me a problem with the 3rd project, a windows installer (for the service) where I have to use ExecutingAssembly path...

Is there a way to share the code without making a separate dll? Automagic statical linking, so to say ?

Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442
  • On vista and newer symlinks might be useful. – CodesInChaos Dec 02 '10 at 15:27
  • @CodeInChaos, there's no way to store symlinks in sourcecontrol/backups easily - you are doing both of those, right? =) – Rob Dec 02 '10 at 15:30
  • http://stackoverflow.com/questions/954560/what-does-git-do-to-files-that-are-a-symbolic-link sounds like git stores sym-links as sym-links. No idea if it works on windows though since I don't use vista yet. And depending on the use-case it might be enough to add the sym-link to the ignore-file and manually create it for each coder. A bit hackish, but should work. – CodesInChaos Dec 02 '10 at 15:51
  • But the VS "Add as Link" feature looks cleaner to me. Didn't know about that one. – CodesInChaos Dec 02 '10 at 15:54

7 Answers7

38

How about adding a file as a link.

In Visual Studio right click on your console test app project -> select add existing file -> in the file add dialog navigate to files in your actual windows service project -> select the files you want to share -> and on add button select add as link option.

Unmesh Kondolikar
  • 9,256
  • 4
  • 38
  • 51
  • 4
    That sneeky dropdown triangle on the Add button is quite hard to spot! Very useful if you want a shared library say for Android and Windows with the same code, but different project wrappers. – AndyM Nov 07 '12 at 03:49
  • How about if I wanna add a bunch of files from a directory tree? Can I add an entire directory recursively? – markonius Apr 25 '19 at 18:53
  • Link in answer is dead - *"Oops! That page can’t be found."* – Pang Oct 28 '21 at 01:02
15

You can add a file to a project as a link. On the Add Existing Item dialogue the Add button has a drop down on its right. Use this to select "Add as Link":

alt text

Put the file as a solution item and add as a link to each project.

Richard
  • 106,783
  • 21
  • 203
  • 265
2

How about hand-modify the project files to point to the same source file?

Another option - put both projects in the same folder. Add a class to one, then in the other project add existing class and point to the class just created.

Pedro
  • 12,032
  • 4
  • 32
  • 45
2

You could:

  • maintain the shared code in a separate project that produces a DLL and then use a tool such as ILMerge to turn the DLL & EXE into one assembly.
  • share the source-files between multiple projects, either by tweakiing your project files or doing something funky with your source-tree layout.

All that said, the best approach would be to bite the bullet and store the shared code in a shared assembly (DLL). What happens when you decide to, for example, expose this code via a WCF service? It starts getting more complicated then as you have 3 places that reference the same code files. Don't just think about what makes your life easiest now, think about what'll make your life (and that of anyone else who has to maintain the code) easier in the future as well! =)

Rob
  • 45,296
  • 24
  • 122
  • 150
  • ILMerge is too unautomatic (unless there is an easy way to automatize it). I decided for 'something funky' (=add the file as a link). As to the last paragraph - all valid and good arguments, but I don't want a dll for the mentioned reasons. Plus I would have to compile the dll project each time I make a change, while this way, I can just press the run button. Don't know about sourcesafe, but in the worst case, the links are re-added fast. – Stefan Steiger Dec 02 '10 at 16:17
  • of course you can automate ILmerge in the post-build event, executing it from command line – Pauli Østerø Jan 08 '11 at 00:46
  • @StefanSteiger You do realize that pressing the run button = build & run, right? If there any outstanding changes, it will recompile the project and any dependent projects in the same solution before it runs. – Dan Bechard Aug 20 '15 at 13:22
  • @StefanSteiger You could use Costura.Fody as an automized alternative: https://github.com/Fody/Costura – pinki Aug 29 '16 at 10:53
1

Necromancing - As per Visual Studio 2017:

You can create a shared project, and then reference the shared project in another project.

It will use the framework-version and libraries from the project you reference the shared-project from. You can also use the same shared project in multiple projects, provided you get no conflict.

This is basically statical linking on a source-code level.
This also works with HTML&JavaScript-files (specifically, it works with publishing), but with HTML & JS files, you will run into problems while debugging...

It's under "Classical Windows Desktop", but you can also use it for .NET Core etc.

enter image description here

0

I'm going to describe the setup we use to manage and test our Windows Service projects. While this doesn't answer the question of "sharing code without a DLL" (Unmesh's answer takes care of that), I think the OP probably doesn't realize how easy this is with a DLL. In any case, I'm hoping it will help someone.


Create a solution, LDAPSync. Create three projects in this solution:

  • LDAPSyncLib
  • LDAPSyncSvc
  • LDAPSyncTest

LDAPSyncLib is a DLL project that contains all of your business logic and main functionality.

LDAPSyncSvc is a Windows Service project that contains two classes, a service controller class that inherits from ServiceBase, and an Installer class for your service. This project has a "project reference" to LDAPSyncLib.

LDAPSyncTest is either a GUI application (WinForms, WCF, etc.) or a console application, depending on your needs. This project also has a "project reference" to LDAPSyncLib. Its sole purpose is to provide some interface which allows you to easily make the required calls into your business logic for testing purposes. In Visual Studio, set this as your "StartUp Project".

Now, when you run in debug via Visual Studio you will get a nice little GUI or command window that you can use to manually make test calls. When you install it as a Windows Service, the LDAPSyncSvc project's controller class will take over and handle all of the necessary service requests (start, stop, pause, etc.)

We have around 30 in-house Windows Service projects that we've been continuously managing, developing and testing for over a decade and this workflow has proved invaluable in quickly finding and fixing bugs when they arise. Best of luck with your project and I hope this helps some future Googlers.

Dan Bechard
  • 5,104
  • 3
  • 34
  • 51
0

If you want to share functionality, you should use a DLL or similar.

Since what you want to share is the source, what you are essentially sharing is file sharing. So you can do that by making your projects reference external sources or you can have your source control do this for you.

If you are using Visual SourceSafe, you can make a link between two folders. VSS will make sure that they are treated as the same file.

Bruno Brant
  • 8,226
  • 7
  • 45
  • 90