Good patterns of development really make maintenance even easier. And since we are all doing maintenance after we write our first line of code, we really should keep maintenance concerns in mind at all times. When you do interfaced based development, it allows you to swap out behaviors without changing code that uses the interface. Now can you imagine taking that to the next level?
Awhile back I was giving a presentation on Inversion of Control and I was asked if it were possible to set a new DLL next to an existing application in production and get that application to use the new DLL using IOC. The thought was that you wouldn’t have to worry about breaking existing code. I sat down later and learned that it’s not only possible, but actually very easy! It gives you a pretty powerful sword to wield should you ever need it.
Implementing A New Behavior Without Changing a Line of Deployed Code
Let’s say you are using Windsor Container for your IOC. So you have code in production and need to change some behavior. But you don’t have the ability to get to the source code, just the DLLs in production. You need to just make the current application just log a message instead of doing whatever the system did before. This system is a simple HR system called employee.system.
1. We create a project called employee.system.additional and reference the code that has the interface we need to implement.
2. Then we create our implementation of the interface. This is a new implementation of ICommand. It's very simple, it just logs a message.
3. After we are done, we build the new assembly and take that that DLL and place it in the same folder as the existing code.
4. Then we crack open the configuration file of the deployed system and make some changes to allow us to use the new DLL's code (the existing code doesn't even know of this new assembly).
5. Then we run the application.
The functionality of the application has now changed completely based on a new DLL and a configuration change. How many lines did we change in the current application? That’s right, zip. Zero. None. Big goose egg.
Conclusion
Maintenance takes on a whole new meaning when you can do something like this! I’m not saying this is something you should do in a normal environment. But just knowing about it gives you options. It’s something that should be used in special situations like when you have access to a DLL but don’t have access to the source code. This is something you can tuck away and pull out one day when you have a need for something like this. Or similarly you could use something like Reflexil to just manipulate IL code and save modified assemblies to disk.
Posted
12-03-2009 10:56 PM
by
Rob Reynolds