In this post I’ll mention the refactoring updates I applied today on GeneriX solution. This post is directly related to previous post (GeneriX for SharePoint) and can be considered as an extension of it.
If you remember, we used copy-paste approach to duplicate some class definitions (action and handler classes) in step-1 and step-3 of previous post. Actually, by doing so we kinda implemented new action and handler classes for our new custom action: check-out items.
Simply copy-paste introduces duplicate code, which actually needs to be written only once, if proper refactoring is applied.
In our case, custom actions and custom handlers were repeating some common rendering and init functions. So I introduced two new base classes for those: CustomActionBase and CustomHandlerBase.
As seen, both classes are declared abstract. Also keyword virtual used for methods which can be overridden in derived classes.
Hopefully this comparison (Virtual vs. Abstract Classes) helps to understand why ProcessItem and SetUIDetails methods are declared as abstract. Because those methods MUST be implemented by deriving classes.
Actually implementation of ProcessItem will be our custom logic that we’d like to apply on each selected item. e.g. delete, check out …etc.
Similarly, implementation of SetUIDetails will determine UI behaviour of our custom Actions Menu item, such as title, icon, description.
Now, let’s see how simplified our action and handler classes are after refactoring applied.
For instance, below you can see handler and action classes for custom action: Delete. In our handler class, we have no unnecessary code, simply we have just the code we need to process a selected item, which is deleting in this case. Since, ProcessItem method was declared as abstract in base class, we implement it here in deriving class. If you don’t, you get a compilation error during build.
In derived action class, all we need to provide is UI details for our actions menu item. For delete operation, providing details below, assigns necessary values for our delete menu item control.
In summary, it’s a good practice to apply Refactoring on your code where possible. Not only it reduces amount of code you write, but also it improves code execution performance. Also, your code becomes more modular and easy to read and understand by others.
GeneriX Wsp: Zieglers.SharePoint.Generic.20100325.wsp
GeneriX Source Code: GeneriX.zip
(Don’t forget to remove .doc extension Rename downloaded files exactly as seen above)
Let me know if you have questions regarding refactoring, virtual-abstract classes, source code, deployment issues … etc.