Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing code between 2 projects without a dll

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 ?

like image 774
Stefan Steiger Avatar asked Dec 02 '10 15:12

Stefan Steiger


People also ask

How do I share code between projects in Visual Studio?

Each project will reference the same location on disk. Doing this is very simple. In the target location, right click the folder and select “Add > Existing Item…” Usually you'd just press “Add,” but in this case, you actually want to click the dropdown and select “Add As Link.”

Can two projects reference each other C#?

This can be achieved by adding reference of one project in the other project. If both the projects are in the same solution, then expand the project name, right click on 'References', click on Add references. Go to 'Projects' Tab, select the project name which you want to use in current project, click ok.


4 Answers

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.

like image 74
Unmesh Kondolikar Avatar answered Sep 17 '22 21:09

Unmesh Kondolikar


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.

like image 39
Richard Avatar answered Sep 21 '22 21:09

Richard


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.

like image 25
Pedro Avatar answered Sep 21 '22 21:09

Pedro


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! =)

like image 39
Rob Avatar answered Sep 18 '22 21:09

Rob