Wednesday, October 7, 2009

Behavioral Pattern - Mediator Pattern

Definition:


Mediator defines an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.


UML Diagram:

















Sample code:


Mediator:


public enum BillerType
    {
        Vodafone = 1,
        BESCOM = 2,
        TataIndicom = 3,
        Airtel = 4
    }
    public interface IBillPayAgent : IDisposable
    {
       
        bool PayBill(double amount);
    }
    public class BillPayAgent : IBillPayAgent
    {
        private BillerType _billerType;
        public BillPayAgent(BillerType billerType)
        {
            this._billerType = billerType;
        }
        private IBiller Biller
        {
            get { return BillerFactory.GetFactory(_billerType); }
        }
        public bool PayBill(double amount)
        {
            using (Biller)
            {
                Biller.ReceiveAmount(amount);
            }
            return true;
        }

        public void Dispose()
        {
            Dispose(true);
        }

        ~BillPayAgent()
        {
            Dispose(false);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
                GC.SuppressFinalize(this);
        }
    }



Supplier:


    public class IBiller : IDisposable
    {
        public virtual void ReceiveAmount(double amount)
        {
            Console.WriteLine("Received Amount: " + amount.ToString());
        }

        public void Dispose()
        {
            Dispose(true);
        }

        ~IBiller()
        {
            Dispose(false);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
                GC.SuppressFinalize(this);
        }
    }

    public sealed class Vodafone : IBiller
    {
        public override void ReceiveAmount(double amount)
        {
            Console.WriteLine("Greetings from Vodafone");
            base.ReceiveAmount(amount);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
                GC.SuppressFinalize(this);
            base.Dispose(disposing);
        }
    }

    public sealed class TataIndicom : IBiller
    {
        public override void ReceiveAmount(double amount)
        {
            Console.WriteLine("Greetings from TataIndicom");
            base.ReceiveAmount(amount);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
                GC.SuppressFinalize(this);
            base.Dispose(disposing);
        }
    }

    public sealed class BESCOM : IBiller
    {
        public override void ReceiveAmount(double amount)
        {
            Console.WriteLine("Greetings from BESCOM");
            base.ReceiveAmount(amount);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
                GC.SuppressFinalize(this);
            base.Dispose(disposing);
        }
    }

    public sealed class Airtel : IBiller
    {
        public override void ReceiveAmount(double amount)
        {
            Console.WriteLine("Greetings from Airtel");
            base.ReceiveAmount(amount);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
                GC.SuppressFinalize(this);
            base.Dispose(disposing);
        }
    }



Consumer:


public class Consumer : IDisposable
    {
        private IBillPayAgent billPayAgent = default(IBillPayAgent);
       
        public void DoShopping()
        {
            Console.WriteLine("I am doing shopping");
        }

        public void PayEBBill(double amount)
        {
            using (billPayAgent = new BillPayAgent(BillerType.BESCOM))
            {
                billPayAgent.PayBill(amount);
            }
        }

        public void PayMobileBill(double amount,BillerType billerType)
        {
            using (billPayAgent = new BillPayAgent(billerType))
            {
                billPayAgent.PayBill(amount);
            }
        }

        public void Dispose()
        {
            Dispose(true);
        }

        ~Consumer()
        {
            Dispose(false);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
                GC.SuppressFinalize(this);
        }
    }

Disposable pattern using IDisposable in C#

public interface ISomeInterface : IDisposable
    {
        void SomeMethod();
    }

    public class MyClass : ISomeInterface
    {
        public void SomeMethod()
        {
        }

        public void Dispose()
        {
            this.Dispose(true);
        }

        ~MyClass()
        {
            this.Dispose(false);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
                GC.SuppressFinalize(this);
        }
    }



Code to execute:
MyClass myClass = new MyClass();

            try
            {
                myClass.SomeMethod();
            }
            finally
            {
                myClass.Dispose();
                myClass = null;
            }



Please note that, if the object is of IDisposable; it is better to use using {....} block in C#. It internally takes care of the garbage collection.

Tuesday, October 6, 2009

Elements.xml file for creating folders inside document library

Below is the sample elements.xml file content for creating folders inside document library through features


<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/"><Module Path ="Client Supplies" Url ="Requirement/Client Supplies" />
<Module Path ="Functional Requirements" Url ="Requirement/Functional Requirements" />

</Elements>


Please note that, like this any level of nested folder structure can be created. (Url = "Docs/FirstLevel/SecondLevel/ThirdLevel"

Solution for "Cannot complete action. Try again later" error while activating ListInstance in sharepoint

Each ListInstance in ListInstance.xml file must contain "FeatureId="00bfea71-e717-4e80-aa17-d0c71b360101"" to avoid this error. 
Basically, your <ListInstance> node should look like as shown below;


    <ListInstance
           Title="Requirement Management"
           Description="Requirement Management"
           Id="A26D9F72-5F3B-477d-8E43-CB20698BC669"
           TemplateType="101"
FeatureId="00bfea71-e717-4e80-aa17-d0c71b360101"
OnQuickLaunch="true"
           Url="Requirement">
    </ListInstance>



The feature ID listed above is the FeatureID of the a normal (OOTB) sharepoint feature. This is required to find the TemplateType 101; which is a default doclib.