Introduction
Recently I have stumbled on an interesting problem : I was working on a project and developed some user controls to use in this project.After a while i have started another project and wanted to use the same user controls for the new project.The problem was that i couldn't find a way to share the controls between the projects i a way that will separate the controls from the projects that use them.
Motivation
The reason that I needed this solution is because in a point of time I had 10 different projects using the same user controls.Each time I wanted to change something in one of the user controls I had to change it in all 10 projects and then recompile and redeploy them.This way I have one project containing the user controls , and every change in it applies to all the sites.
the reason that I prefer using user controls rather then converting them to server control is that sometime there are controls that are heavy and complex in presentation and event handling (several buttons, textboxes etc.) and transforming it to a server side control could be a real headache .
How to Do It
The solution to the problem was pretty simple :
first of all , i have separated the user controls to a different project.The new project contained only the User controls and files related to them.
The second step was connecting the user controls project to the projects that will use them.to do this, first of all we need to define a reference in each project ot the user controls project.the next step will be (and the most important step) to go to the iis and create a virtual directory under the project's web directory that will point to the directory containing the user controls (it shouldn't be defined as an application in IIS so make shure you remove the application after creating the virtual directory) . the final step is using the user controls in your code. this should be done by refering to them as if they are part of the project and placed in a subdirectory (according to the name we gave to the virtual directory in IIS).
that is all .
regarding the encapsulation of resources : the use of a virtual directory for mapping the user controls to the project folder solves this issue because when you try to use a resource , the request is handled according to the path from the request to the resource in the containing site. Which means : as long as you use relative paths to resources you'll be safe regarding resources inside the user controls project and event resources that are in the containing project (which is not recommended because it creates a dependency between the projects)
once we have done all of this , we are sharing the user controls among the projects.
One Minor Problem
There is one minor problem with this method (which is important and I forgot to mention in the article): every time you make a change in the code behind of the user controls , you should redeploy it's DLL among all the projects that use these controls. BUT : if you don't use code behind : you're safe(I do use - a lot), if you do use : it's a lot simpler to copy some DLLs then recompile and redeploy 10 projects !!!
enjoy.