Custom Controller Factory & Structure Map - MVC Razor

Oww.... wait, why i have to go for Custom Controller Factory ? 
Error shown in earlier post "Default Controller Factory Vs Custom Controller Factory"  is lack of default constroctor. So, i will add one parameter-less constructor. That is it. Problem solved. :)

Sorry, now you have created a bigger problem. Let me show you how.
If you understood the article "Dependency Injection", you will observe the flexibility (Loosely coupled) we achieved because of that.


Default Controller Factory Vs Custom Controller Factory - MVC Razor

In order to have a better understanding of this, lets start this discussion with MVC Page life cycle.
Let's see step by step how it works.


Presence Control in Web app

We have seen many ways of implementing presence control in our web application.
The simplest of all of them is a combination of Images with Java script.
Lets see how to implement it.
Before going to actual implementation, we have to answer a few questions.
Why i need to use this one, i have my predefined reusable user controls for presence control ?
Ans: Yes, you can use them unless you answer below questions.
    i. Is it light weight?
    ii. Is it talking to your Messenger service or you want to take the overhead of communicating with exchange server?
    iii. Do you want to have complete control over the functionality and display?
    iv. Do you want to avoid the issues due to lack of complete information regarding references and bugs?
    v. Are you working on MVC Razor which cannot accommodate the heavy weight user controls designed in regular asp.net?


Digital Certificates & Logic Behind

Digital certificates are electronic credentials that are used to assert the online identities of individuals, computers, and other entities on a network. Digital certificates function similarly to identification cards such as passports and drivers licenses. They are issued by certification authorities (CAs) that must validate the identity of the certificate-holder both before the certificate is issued and when the certificate is used. Common uses include business scenarios requiring authentication, encryption, and digital signing.

Certificate Architecture:
A digital certificate binds a user, computer, or service’s identity to a public key by providing information about the subject of the certificate, the validity of the certificate, and applications and services that can use the certificate. Certificates issued in Windows Server 2003 and earlier PKIs are structured to meet these objectives based on standards established by the Public-Key Infrastructure (X.509) Working Group (PKIX) of the Internet Engineering Tasks Force (IETF). The following figure shows the contents of X.509 version 3

  • Subject. Provides the name of the computer, user, network device, or service that the CA issues the certificate to. The subject name is commonly represented by using an X.500 or Lightweight Directory Access Protocol (LDAP) format.
  • Serial Number. Provides a unique identifier for each certificate that a CA issues.
  • Issuer. Provides a distinguished name for the CA that issued the certificate. The issuer name is commonly represented by using an X.500 or LDAP format.
  • Valid From. Provides the date and time when the certificate becomes valid.
  • Valid To. Provides the date and time when the certificate is no longer considered valid.
    • The date when an application or service evaluates the certificate must fall between the Valid From and Valid To fields of the certificate for the certificate to be considered valid.
  • Public Key. Contains the public key of the key pair that is associated with the certificate.

WCF - Implementing Message Level Security

Lets start this topic with a comparison between Transport and Message level security.

Transport Level Security
Message Level Security
Scenarios when we should be using one of them
When there are no intermediate systems in between this is the best methodology.  
If it’s an intranet type of solution this is most recommended methodology.
When there are intermediate systems like one more WCF service through which message is routed then message security is the way to go.
Does not need any extra coding as protocol inherent security is used.
Performance is better as we can use hardware accelerators to enhance performance.
There is lot of interoperability support and communicating clients do not need to understand WS security as it’s built in the protocol itself.
Does not need any extra coding as protocol inherent security is used.
Performance is better as we can use hardware accelerators to enhance performance.
There is lot of interoperability support and communicating clients do not need to understand WS security as it’s built in the protocol itself.
As it’s a protocol implemented security so it works only point to point.
As security is dependent on protocol it has limited security support and is bounded to the protocol security limitations.

Needs application refactoring to implement security.
As every message is encrypted and signed there are performance issues.
Does not support interoperability with old ASMX webservices/

So, both Transport and Message level security have their pros and cons.
But, in real time internet scenarios, message level security will be the best pick.

Message Level Security:
Message security uses the WS-Security specification to secure messages. The specification describes enhancements to SOAP messaging to ensure confidentiality, integrity, and authentication at the SOAP message level (instead of the transport level).


Builder Pattern

Developers have observed that, composing a system using inheritance makes it too rigid.
Creational Pattern is a way to break that tight coupling by abstract out the object creation.
The Creational patterns aim to separate a system from how its objects are created, composed, and represented. They increase the system's flexibility in terms of the what, who, how, and when of object creation. Creational patterns encapsulate the knowledge about which classes a system uses, but they hide the details of how the instances of these classes are created and put together.

Lets start this pattern with a hot snac. "Pizzaaaa . . . "
 Even though i am a non vegetarian, i love "Veggi Delight" than any other flavor. I know its very hard to discuss a design pattern keeping this picture in front of you. But lets stick to topic.
There are lot of variations in Pizza, different sizes, there are different types of bread doughs(base), different types of sauces , different types of toppings that can be used to prepare a pizza.
Now let's cook a pizza in c# mode.


WCF - Implementing Transport Security using SSL

Security :  Security ensures that the information does not go in wrong hands when it travels from the sender to the receiver. This feature ensures that the receiver of the message gets the same information that the sender sends without any data tampering.

There are two types of security provisions in WCF.
1. Transport Level
2. Message Level

Transport level security happens at the channel level. Transport level security is the easiest to implement as it happens at the communication level. WCF uses transport protocols like TCP, HTTP, MSMQ etc and every of these protocols have their own security mechanisms. One of the common implementation of transport level security is HTTPS. HTTPS is implemented over HTTP protocols with SSL providing the security mechanism. No coding change is required it’s more of using the existing security mechanism provided by the protocol.

Message level security is implemented with message data itself. Due to this it is independent of the protocol. Some of the common ways of implementing message level security is by encrypting data using some standard encryption algorithm.
 In this post we will see how to implement transport security on http protocol using SSL certificates.


MVC Razor - Custom Html Helper Calsses.

We have seen several examples on MVC Razor in earlier posts. In every code module we demonstrated until now, @Html.SomeFunction() made our lifes easy with MVC. These are nothing but Html Helper classes.

HTML Helper classes:
An HTML Helper is just a method that returns a string. The string can represent any type of content that you want. For example, you can use HTML Helpers to render standard HTML tags like HTML <input> and <img> tags. You also can use HTML Helpers to render more complex content such as a tab strip or an HTML table of database data.
The ASP.NET MVC framework includes the following set of standard HTML Helpers (this is not a complete list):
  • Html.ActionLink()
  • Html.BeginForm()
  • Html.CheckBox()
  • Html.DropDownList()
  • Html.EndForm()
  • Html.Hidden()
  • Html.ListBox()
  • Html.Password()
  • Html.RadioButton()
  • Html.TextArea()
  • Html.TextBox()
We can even write our own helper classes to perform common & complex tasks with ease.
Immediately a question pops up into your mind.
Why Helper classes, why not a DLL where i can use through out the project?
Yes, you can do that, but with helper class you can do better. Let me explain you. If you use the function as DLL, you might use it in model or Controller. Thus you are making that functionality specific to that controller or Model or may be view. But if you use it as a helper class, it can be used just like any html helper class through out the project. In future if you changed the helper method name, it requires no code change in Controller or Model. Search the old method name and replace it with new method name just like any html tag.
Enough with theory, lets jump in to practice.

Dependency Injection (DI / IOC)

"Loosely Coupled" is the one of the basic qualities of a good design.
Let me justify that first.
If you have 2 choices:
1. A task which has lot of dependencies on others.
2. A task which have Minimum number of dependencies.
Which one will be easier. Obviously its #2.
So, less dependencies means more loosely coupled and better the design it will be.

I believe 80% of world population know below Guy.
He is Harry potter with a Magic Wand (stick in his hand), which he will be using to cast the spell.
Now lets watch the potter's magic in a console application using c#.


MVC Razor - Using Sections & Partial Views

Being a web developer, dealing with page layouts and striving for optimized code will be our D2D activity.
In typical Asp.Net scenario we use Master Pages and User controls for these two tasks respectively.
So, today we will see how we handle these things in MVC Razor 3.0.
There are no Master Pages and User controls in MVC.
You know why? Comparatively they are complex to develop. :)

In order to implement both Sections and Partial Views, i will take a task of displaying a small Menu which will redirect me to 2 different pages in my application.

There are 3 ways of doing it.
1. Hard coding the menu on Master Page.
2. Rendering the menu based on Page.
3. Using Partial Views.


Abstract Factory Pattern

Let me begin with a proverb, "Change is Inevitable & Good." If you are not ready to adapt to the change, you will not survive. Same case applies to our software as well.

Like many of us, I am a big time Fan of PC Games. So, i thought of interpreting the usage and implementation of a pattern on the same lines.

I love "Company of Heroes" game. It has awesome UI and whole lot of Gadgets and i had great time playing it.
Look at UI once.
If you closely observe, there are Cars, Tankers, and Flights. In fact there are different types of each kind.
Now, lets say i have Scenario, where i designed this kind of a game (Lets Assume ;))
I have to handle all these kinds of vehicles say, Tankers. Tomorrow, i got a complain that, the tankers are not efficiently attacking the Aircrafts. Now, i have to categorize Tankers into 2 different types.
1. Ground Tankers.
2. Air Tankers
Tomorrow, there may come another type. And my code should be open to implement any new kind of vehicle that may creep into scope in future.

So, in order to handle families of related and dependent objects, we use "Abstract Factory Pattern".

Coming to Game, when the user starts the game, i have to give him the following vehicles to start off.
1. Soldiers Car
2. Officers Car
3. Passenger Aircraft
4. Soldiers Aircraft
5. Land Tanker
6. Air Tanker.