Tuesday, June 18, 2013

MVC Custom Model Binder

Recently I was looking at an unusual problem where we had to add custom fields to a view on an ad hoc basis. Not really a problem if it was an MVC app talking directly to a data source. But mine was over several REST based services that were talking to a CRM dynamics backend, spread across multiple domains. Which meant we had to change the data models through out the system. That's when it occurred to  me that we can have one dedicated field  that can store all the ad hoc field data using JASON or xml and we append it to one of the existing fields. Probably not the ideal solution, but that's when I came across custom binding for MVC. I would like to share with you how simple and easy it is to use custom binding to solve similar issues.

See here for more http://www.codeproject.com/Articles/605595/ASP-NET-MVC-Custom-Model-Binder?msg=4591496#xx4591496xx

Friday, February 10, 2012

The Adapter Pattern

Definition : The adapter pattern is used to match one interface with the other.

Problem : In the below example I have a console application based on FarmAnimals. I would like to build a collection of farm animals with several capabilities. Some of these capabilities I wish to implement on my own , other I hope to be able to make use of using third party libraries with existing functionality. I have found one such library called the ZooLib, but the ZooLib animal implementation a very different interface.

Analysis : My console application FarmAnimals consists of a concrete class called goat that implements the IFarmAnimal interface. The IFarmAnimal interface specifies a method getDietaryType.

The ZooLib assembly consists of a concrete function getFamily, that I would like to use in my console application FarmAnimals. But since my goat class implements IFarmAnimal and the getFamily which is part of the MountainGoat concerete class which implements the interface IZooAnimals which are incompatible interfaces.

Solution : I introduce an abstract base class called ZooAnimalAdapter. ZooAnimalAdapter impliments both interfaces, it delegates all my IFarmAnimal operations to the concrete subclasses and builds wrappers around the IFarmAnimal.

Code

The below is my FarmAnimal application

class Program
{
static void Main(string[] args)
{
Goat g = new Goat();
g.getFamily();
g.getDietaryType();

Console.ReadKey();
}
}

public abstract class ZooAnimalAdapter:IFarmAnimal,IZooAnimal
{
public abstract void getDietaryType();

public void getFamily()
{
IZooAnimal za = new MountainGoat();
za.getFamily();
}
}

public interface IFarmAnimal
{
void getDietaryType();
}



Below are the contents of the ZooLib library




public interface IZooAnimal
{
void getFamily();
}

public class MountainGoat : IZooAnimal
{
public void getFamily()
{
Console.WriteLine(" Family : Bovidae");
}
}

Sunday, January 29, 2012

Object Oriented Programming

Object Oriented Concepts

 Inheritance

   Creating a class based on an existing base class or super type. As the title suggests, this concept defines reuse or the recreation of objects using an existing class.

In the programming world we realize this by defining a base class that contains a set of shared or common methods and then create subclasses that inherit from this class. These methods and properties are then automatically available in the subclass.

What can we achieve by Inheritance?
The implementation of a common method/methods can be reused or shared among the subclasses from the base class.

 Abstraction

The basic principle behind abstraction is to hide the complex details and keep things simple.


In the programming world we realize this by defining a base abstract class that contains a method/methods with or without concrete  implementations.The subclasses that extend this abstract class may or may not in turn implement these methods.

What can we achieve by Abstraction?
As you will read later below, one of the principles of object oriented principles is to code to an interface rather than an implementation. Abstraction helps in coding to a interface or a super type giving you the flexibility to change the implementation at a later stage.


 Polymorphism

 Polymorphisms is a generic term that means 'many shapes'. In OOP's polymorphism is the ability to be able define multiple implementations for the same method call.

In the programming world we realize this by defining a common interface across various concrete class with different implementations.

Ad-hoc polymorphism: polymorphism is not the same as method overloading or method overriding 

What can we achieve by Polymorphism?
We can achieve interchangeability. That is we can easily change the behavior of a given method without the need to create a new type.

Encapsulation

Simply means hiding information that does not need to be seen by others. An analogy can be made here with the notion of a capsule, which not only encloses its contents, but also protects it from the exterior environment.

In the programming world we realize this by the use of access modifiers. For example we can make the methods of  a class private thus making its implementation in accessible to other objects.

What can we achieve by Encapsulation?
We can reduce the dependency of objects on each other. For example we can prevent users from setting the internal data of the component into an invalid or inconsistent state.


Object oriented principles

1) Code to an interface rather than to an implementation
2) Favor composition over inheritance
3) Encapsulate what changes or varies



Reference

http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29
http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming
http://www.my-project-management-expert.com/definition-object-oriented-programming.html