Tuesday, January 21, 2014

Recommendation for when to use Interface or Abstract base class.




Interface:

Interface is a contract with the class that class will provide the implementation of all the methods or property declared in an Interface.

·         Interface will only contain declaration of the methods or property.

·         Class can implement multiple interfaces.

·         If a class wants to inherit from another class then that class should precede interface implementation.

Example:

Class A : BClass, NewInterface

{

}

Abstract Base Class:

Abstract class is a contact with the inheriting class that inheriting class will provide the implementation of all the abstract methods and properties in defined in the abstract class.

·         A class can inherit only one class.

·         Abstract class can contain both abstract method and implemented methods.

·         If a class inheriting from abstract class does not provide the implementation of all the abstract methods in abstract class, then that class has be marked with abstract keyword.

When to use Abstract base class and when to use Interface

·         If you module or component will undergo versioning in future then it is better to use abstract base class. If a new version of a component is introduced then by updating the base class all the inheriting class will automatically get updated. If you have used interface and a new version is introduced then a new interface need to be created.

·         Use abstract base class for commonly related classes. Interface is used to provide common functionality for unrelated classes.

·         Interface is best suited for small set of functionality, whereas for large modules abstract base class is useful.

·         Abstract base class provides partially implementation functionality for your classes, whereas interfaces provide no implementations for its members, it is implementing class responsibility to provide complete implementation of all the members of an interface.

Saturday, January 11, 2014

Best way for MVC Model Binding

While understanding MVC model binding, I came across below mention 6 steps which helped me understanding modeling technique.

Tip #1: Prefer Binding Over Request.Form
Tip #2: Custom model binders
Tip #3: Custom Model Binding via Inheritance
Tip #4: Using Data Annotations for Validation
Tip #5 : Recognize Binding and Validation As Two Phases
Tip #6: Binders Are About The Environment

Please refer below blog for more details:
http://odetocode.com/blogs/scott/archive/2009/04/27/6-tips-for-asp-net-mvc-model-binding.aspx

Thursday, January 9, 2014

Power of OfType and Cast in Linq

Let’s understand the OfType and Cast with the help of example.

ArrayList states = new ArrayList(4);
states.Add("Karnataka");
states.Add("AP");
states.Add("MP");
states.Add(4);

// Apply OfType() to the ArrayList.
IEnumerable<string> query1 = states.OfType<string>();

foreach (string state in query1)
{
        Console.WriteLine(state);
}
 ---------------------------------------------------------------------
// Apply Cast() to the ArrayList.
IEnumerable<string> query1 = states.Cast<string>();

foreach (string state in query1)
{
        Console.WriteLine(state);
}

OfType

When Converting ArrayList to IEnumerialble using OfType(), it will convert all the elements of type string and ignore the other type. The output of above OfType conversion will be as follows:
Karnataka
AP
MP
Oftype will skip the value 4 as it is not string type and we wanted to convert the ArrayList to IEnumeruable of string type.  If we tried to convert the ArrayList to IEnumeriable, then the output would be:
4

Cast

Will try to cast all the elements of type x(string in above example), if some elements are not of type x then it will throw InvalidCastException. Second for loop stated above in the example will not work as when Cast will try to convert element “4” to string it will throw InvalidCastException.

Tuesday, January 7, 2014

Understanding Association, Aggregation and Composition


Let’s understand association, aggregation and composition used in OOPs concepts.

Association

Association describes *is a relationship between two classes. Association *Uses another object to perform some action on its behalf. For Example:

Manager class *Uses ReportManager class to perform some action on its behalf.

       public class Manager
       {
              public Manager()
              {
                     var report = new ReportManager();
                     report.Initalize();
              }
       }

It is a directional association between Manager and ReportManager.

Aggregation and Composition

Explanation of aggregation and composition cannot be understood separately, explanation of these concepts should go hand in hand.
Aggregation is a special type of an association. Aggregation explains *has a relationship between the two classes. In this object of one class is contained in the other class. If second instance is part of the first instance of a class then it is mentioned as Aggregation between the two classed. For example:
public class Employee
       {
              private Insurance employeeInsurance = new Insurance();

       }

Employee class contains Insurance class to get the employee insurance.
 Insurance class aggregate Employee class or in other words Employee contains Insurance.

One thing to note here is that both employee and Insurance class are independent of each other. There can be an employee in an organisation that does not have insurance.

Take another example where Heart does not exist without Body. The life time of heart depends on Body. If Body is destroyed Heart will also get destroyed. Here we say that Body is composed of Heart. This explains the concept of Composition.
Please provide your feedback for this topic.

Implicit Vs Explicit Interface implementation in C#


If you want to understand the difference between implicit and explicit interface and confused which interface to use when, then you are at right place.

Implicit Interface

Implicit interface allows to access methods and properties of an interface as they are part of implementing class.

Explicit Interface

Explicit interface allows to access methods and properties of an interface only when you are treating an Interface.

Let’s understand the above explanation with the help of example:    

Example:

Manager is a class that implements IEmployee and IDepartment interface. Both IEmployee and IDepartment has a method Display(). IEmployee’s method Display() is implemented implicitly and IDepartment’s method is implemented explicitly.

public interface IEmployee
       {
              void Display();
       }

public interface IDepartment
       {
              void Display();
       }

public class Manager : IEmpoyee, IDepartment
       {
              public void Display()
              {
                     Console.WriteLine("IEmployee.Display().");
              }
void IDepartment.Display()
              {
                     Console.WriteLine("IDepartment.Display().");
              }
              public void GetReport()
              {
                     Console.WriteLine("Generate report.");
              }
       }
class Program
       {
              static void Main(string[] args)
              {
                     Manager manager = new Manager();
                     manager.Display();
              }
       }
Difference between implicit and Explicit interface implementation

·         No need to specify interface name with method in implicit interface, where as it is required to specify interface name with method implementation in explicit interface.

o    public void Display() vs void IDepartment.Display()

·         Access modifier is required with method name and it must be public, even protected will not work; whereas in Explicit, no access modifier is required.

·         Implicit implementation allows making method as virtual and abstract; same is not true with explicit interface implementation.

When to use implicit and Explicit interface implementation

·         Do not use explicit members as a security boundary. They can be called by any client who cast an instance to the interface.

·         Do use explicit members to hide implementation details:

o   In explicit interface implementation, IDepartment.Display() method will not appear in class Intellisense list, Below digram shows implecit Display method of IEmployee interface.

o   In above example if you want to invoke Display method of IDepartment interface, use below code:

 
·         Do use explicit members to approximate private interface implementations.

·         Do expose an alternative way to access any explicitly implemented members that subclasses are allowed to override. Use the same method name unless a conflict would arise.