Elimination
Elimination

Reputation: 2723

Two C# projects shares code

I have a GUI and a Windows Service which need to share the model (POCO classes). How do I do that?

I actually seen the option to add one project to another as a reference and as a result, all public classes can be shared.

Upvotes: 0

Views: 83

Answers (2)

leondepdelaw
leondepdelaw

Reputation: 59

If two projects share the same code, a good thing to do is to make a reference to a third project with the shared code. So basically create a new project "Model" with your poco's.

Why is it not good to add one project to another as a reference? The GUI probably doesn't need the whole project Windows Services to operate. The same goes the other way around. The Windows Services doesn't need the GUI.

Upvotes: 2

Martin Noreke
Martin Noreke

Reputation: 4146

In general you would create a project structure as follows:

  • MyProject.DataModel
  • MyProject.Gui
  • MyProject.WindowsService

The GUI and WindowsService can both have references to the DataModel project, in which case changes to the DataModel are immediately picked up by both at compilation.

When you compile to an EXE, you will have DLLs in the same directory, one which would be MyProject.DataModel.dll. The EXE will load that DLL at runtime in order to provide access to those classes.

Upvotes: 5

Related Questions