MVC 3 Dependency injection with Unity 2.0 Video



I just create a video that explain the new feature of MVC3 IDependencyResolver which make the DI(Dependency Injection) easier with mvc .net, I used the Unity 2.0 DI framework to inject Service A and Service B, in this video I show how to work with Unity in design-time and run-time for different services

I will use Unity Microsoft, with net MVC, as we know Microsft Unity one of the appliction block for dependency injection .net

Thanks for Jan Jonas for his post.

For more information you can visit the following link on MSDN

You can download the project from here (Download)

MVP design pattern for Web application vs. MVC, with Example



I start reading about MVP design pattern in web application and I found it very useful, but I get out of one main important concept, the MVP pattern is very good in web application if we working with ASP.NET Web Forms because it will make the model testable but if we working with MVC it’s already a testable and more and more it can be TDD (Test driven Development) model, of course this not the only reason for MVP or MVC they also enhance the maintainability of the web application and there are many other reasons for using them.

I read many articles about MVP but most of my writing come from dot net miscellany, I create a Web form application that has MVP pattern that can download from the following link download

Download

So let’s start what is the MVP pattern?

MVP

MVP

Model View Presenter (MVP) is a software design pattern which essentially isolates the user interface from the business logic through the presenter. MVP is derived from the Model View Controller (MVC) pattern, by Martin Fowler.
The principal behind the MVP pattern is that an implementing application should be split into three core components; Model, View and Presenter:
The main concept of the MVP is that the UI is separated from the business logic and this could have many advantages as we will see

  • The Model component encapsulates all the Data in the application. This may be a database transaction or a call to a web service, or any persistence or even volatile data
  • The View component is the Presentation layer (User Interface); this may be a standard Win Forms client, an ASP.NET Web Form or Web part or Mobile client. In the MVP pattern, it will handle the rendering and accepting user input only.
  • The Presenter is controlling of the application’s actions. For example a sample operation would involve; taking user input from the View, invoking operations on the Model and if needed, setting data in the View to indicate the operations result and so on.

The advantages of MVP pattern are:

  • Isolation of User Interface from Business logic
  • The ability to change the UI from Web to Window or Mobile is very easy
  • Ability to test all code in the solution for all projects (excluding visual presentation and interaction)

We can see here that we can separate the physical class of the MVP in separate assemblies like the following architecture and layer diagram

Layers of MVP

Layers of MVP

You can download the project as I mention before from the above download link

For more information you can read the following post MSDN

Thanks

Adapter Pattern



Summary.


Adapter pattern Convert the interface of a class into another interface clients expect.
Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
Also known as Wrapper

Example and reasons.
Dot Net library, we cannot change the library interface, since we may not have its source code
Even if we did have the source code, we probably should not change the library for each domain-specific application

Solution
We will create our adapter for our domain that adapts the interface to match our needed interface.
Participants
•         Target
•         Adapter
•         Adaptee
•         Client
•         Factory
Class adapters
•     Simple and invisible to the client.
•     A class adapter has instance of an interface and inherits a class.
•     Overriding behavior can be done more easily with a Class adapter
Use the Adapter pattern when you want to:
•     Create a reusable class to cooperate with yet-to-be-built classes.
•     Change the names of methods as called and as implemented.
•     Use an existing class, and its interface does not match the one you need.

Classes

public class Client1
{
public void AnyMethod()
{
//Target t = Factory.CreateTarget();
Target t = new Target();
//Client1 use the the target interface “Request”
t.Request();
}
}

public class Target
{
public virtual void Request()
{
Console.WriteLine(“Called Target Request()”);
}
}

public class Adapter : Target
{
private Adaptee _adaptee = new Adaptee();
public override void Request()
{
// Possibly do some other work
//  and then call SpecificRequest
_adaptee.SpecificRequest();
}
}

public class Adaptee
{
public void SpecificRequest()
{
Console.WriteLine(“Called SpecificRequest()”);
}
}
public class Factory
{
public static Target CreateTarget()
{
return new Adapter();
//or
//return new Target();
}
}

Thanks
M.Radwan

Why using Interface


Using interface add flexibility and power to your design it solves the hard equation how to make the classes cohesion and lose coupling? , because by making interface, class doesn’t need each other anymore, by using interface you define the services that will provided by the class.
This will apply the Dependency inversion principle
You can see the video for more information.
For more information about Dependency inversion principle visit the following links

http://www.objectmentor.com/resources/articles/dip.pdf
Thanks
M.Radwan