Zieglers

Just little about C#, .NET, SQL Server, SharePoint and SAP

GeneriX for SharePoint (Refactoring Applied) – Part: 2

Posted by zieglers on March 25, 2010

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.

CustomHandlerBase Class:

CustomActionBase Class:

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.

Zieglers

Advertisements

3 Responses to “GeneriX for SharePoint (Refactoring Applied) – Part: 2”

  1. lukydrums said

    Hi,

    I have a problem with this solution – I have deployed it on farm, installed and activated features, but can’t see those custom controls 😦
    Is there any other way to install this?

    Thanks for help!

    R
    Luky

  2. zieglers said

    Hi,

    I just downloaded above posted wsp and deployed it on an ootb, clean farm, and it works all fine.
    Make sure you are trying this with a document library – template 101.

    Also, try to activate features in the following order:
    1. jQuery
    2. Multiple list item selection
    3. Custom Actions
    Let me know if you still have problems.

    zieglers

    • lukydrums said

      Thank you for your reply.

      Yes I did exactly you described above but still no luck.
      Just for info – I don’t have feature named “Custom Actions” – I downloaded GenericX.zip and there are DeleteItems, Jquery and SelectMultipleItems features.

      When I install features to farm I see duplicate SelectMultipleItems feature. When I deactivate one of them the result is the same – no custom actions.

      Thanks.

      Luka

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: