Monday, March 28, 2011

Implementing dependency injection pattern in .NET

Dependency injection pattern is a Software and Architecture designing technique that enables to us to separation of concerns,Modules,Classes and Extensibility of Software Development Project. It enables developer to reduce the complexity of project and you can also add new features to software without modifying whole structures. Let’s first understand how dependency injection is important and why we need it.

Why we need dependency inejction?


Let’s a take two class example of shopping cart. I am having two classes ProductDAL and ProductBLL.Here ProductDAL class represent whole data access Methods while ProductBLL implements whole Business logic in software. In normal scenario what we are doing do we will create a new object ProductDAL classes and then we will use that class for our database operations like below.
Public Class ProducDAL
{
 //Methods for database operations
}

Public Class ProductBLL
{
 ProductDAL objectProductDAL=new ProductDAL();

 //Methods for business logic where we are going to use DAL class
}
As you can see in above scenario we will have tight coupling because here we have created the new object of ProductDAL class and we can only change that if we change the container class ProductBLL. This will not help if we need to extend software after sometime and we need to modify the BLL without modifying existing class.So here comes dependency injection in picture. You can use dependency injection in this kind of scenario.

Ways of implementing Dependency Injection:


There are three ways of implementing dependency injection pattern.
  1. Constructor Injection.
  2. Setter Injection.
  3. Interface base Injection

Constuctor Injection:


In this kind of injection we can use constructor parameters to inject dependencies. Like following.
Interface IDAL
{

}

public class ProductDAL:IDAL
{
 //implement the methods of IDAL
}

public class ProductBLL
{
 private IDAL myDalObject;

 public ProducttBLL(IDAL iDal)
 {
     myDalObject=iDAL;
 }

 //use myDalObject to implement business logic
}
Here you can see in above example I have created a Interface IDAL and that interface contains the all method of Data Access Layer. Now we have ProductDAL class which implements that interface. So now you can create object of ProductBLL class like following.
ProductDAL objProductDAL=new ProductDAL();
PrductBLL objProductBLL=new ProductBLL(objProductDAL);
Here you can see you can pass any class as parameter in ProductBLL class which implements IDAL interface. Its not a concrete object so you can change implementation of ProductDAL class without changing ProductBLL class.

Setter Injection:


In this way we can create public property of Interface and then we can use that property to define the object of ProductDAL class like following.
Public Class ProductBLL
{
 IDAL _myDalObject;

 Pulibc IDAL myDalObject
 {
     get
     {
         return _myDalObject;
     }
     set
     {
         _myDalObject=value;
     }
 }
}
So here you can use property to initialize the ProductDAL class like following.

Infterface Injection:


In this section we can have a Method which will have interface as parameter and that will set object of ProductDAL class.
Public Class PrductBLL
{
 IDAL _myDALObject;

 public IntializeDAL(IDAL dalOjbect)
 {
     _myDALOjbect=dalObject;
 }
}
This is same as constructor injection except that this will intialize object after we call this IntializeDAL method like following.
ProductDAL objProductDAL=new ProductDAL();
PrductBLL objProductBLL=new ProductBLL();
objProductBLL.IntializeDAL(objProductDAL);
Hope you liked it. Stay tuned for more.. Happy programming…

Share:

Three new Action Result Type in ASP.NET MVC 3

In ASP.NET MVC incoming browser request mapped to a controller’s action method and that action method returns type of ActionResult in response to the browsers request. I was playing bit with ASP.NET MVC3 to check out new features of ASP.NET MVC 3 and I have found three great new ActionResult Type. Below is details explanation of each one.

  1. HttpNotFound: This return type returns a error 404 on client. This action result type can be very useful when we have resources that are not found and so we can notify the client with 404 error.
  2. RedirectResult:This returns a temporary redirect code 302 or permanent redirect code 302 depending upon a Boolean flag. This kind of redirection is very important for Search Engine optimization. I have already discussed this feature with asp.net 4.0 here.
  3. HttpStatusCodeResult: Returns a user specified code so developer have choice to return specific error code.

Here are code example of Action ResultType how we can use that in code.

public ActionResult SpecificResult()
{
return new HttpStatusCodeResult(404);
}

public ActionResult NotFound()
{
return HttpNotFound();
}

public ActionResult PermanentRedidrect()
{
return new RedirectResult("http://jalpesh.blogspot.com");
}
Hope you liked it.. Stat tuned for more..Happy Programming
Technorati Tags: ,
Shout it
kick it on DotNetKicks.com
Share:

My blog post is appearing on Microsoft Official ASP.NET site.

Today I got a surprise from the Microsoft. One of my friend Hajan Informed me that my blog post is appearing on the Microsoft Official asp.net site www.asp.net. This is great surprise and honor for me that my blog post are appearing on Microsoft official site.

MicrosoftASP.NET

I would like to thanks all the peoples who are supporting me and all the people who are their to correct me If I am wrong. Thank you once again!!. I will continue to writing my blogs to serve community and this kind of encouragement give more boost for writing blogs.

Stay tuned for more.. I will keep writing.. Thank you readers..

Technorati Tags: ,
Shout it
Share:
Wednesday, March 23, 2011

Working with more then one web.config files in asp.net application.

Recently one of reader of my blog how we can work with more then one web.config files in asp.net application. So I decided to blog about that. Here is the my reply for that.

You can work with more then one web.config file in asp.net. But you can not put more then one web.config in each folder. Let’s first understand the hierarchy of web.config and other configuration file settings. On the top of the every configuration files you will have machine.config file which will have all system wide configuration settings.You can find this file in your OS drive like C: /windows/Microsoft.NET/vFrameworkNumber/Config folder. Here framework number with what ever framework you are using 1.1/2.0 or 4.0. You can override those settings in web.config file at the your application root folder. Same way you can add more web.config file in subfolder and can override the setting of parent folder web.config file. So we will hierarchy like below.

Hirerchay

Now let’s Create Project for it. In that I have create two web.config and 2 pages. First I have putted the web.config in root folder and then I have putted web.config in subfolder. Same way I have created a sub folder and then I have putted the web.config in sub folder. I have also putted one asp.net page in root as well as subfolder to use respective web.config settings. Here are my folder structure like below.

FolderStructure

Below is code for root folder web.config

<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<appSettings>
<add key="root" value="This is from root web.config"></add>
<add key="MySetting" value="This my settings is from root web.config"></add>
</appSettings>

</configuration>

and following is code for sub folder web.config.

<?xml version="1.0"?>
<configuration>
<system.web>
</system.web>
<appSettings>
<add key="sub" value="This is from sub web.config settings"></add>
<add key="MySetting" value="This my settings is from sub folder web.config"></add>
</appSettings>
</configuration>


After that I have written a code in root asp.net page to print settings from web.config folder like this following.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MoreWebConfig
{
public partial class Root : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(System.Web.Configuration.WebConfigurationManager.AppSettings.Get("Root"));
Response.Write("<BR>");
Response.Write(System.Web.Configuration.WebConfigurationManager.AppSettings.Get("MySetting"));

}
}
}

Same way I have wrriten code in subfolder like following.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MoreWebConfig.SubFolder
{
public partial class SubFolderPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(System.Web.Configuration.WebConfigurationManager.AppSettings.Get("Sub"));
Response.Write("<BR>");
Response.Write(System.Web.Configuration.WebConfigurationManager.AppSettings.Get("MySetting"));

}
}
}

Now let’s run the both pages in browser one by one and you can see root folder application page is fetching settings from root folder web.config while sub folder application page is fetching setting from subfolder web.config even if key ‘mysetting’ is same on both as expected. You can see out put in browser below.

Root.aspx

Root

SubFolderPage.aspx

SubFolder

So it’s very easy to work with multiple web.config. The only limitation of this you can not access sub folder web.config settings from root folder page. Except all you can use anything. Hope you liked it. Stay tuned for more..Happy programming..


Technorati Tags: ,
Shout it
kick it on DotNetKicks.com
Share:
Monday, March 14, 2011

TSQL Quiz 2011 on beyondrelational.com

One of the my friend Jacob Sebastian running a SQL Server TSQL quiz on his site beyondrelational.com. This is a great opportunity to learn TSQL and win great price Like Apple IPad and other lots of cool stuff. So if you are expert and if you learning TSQL then its a great way to test your knowledge.

For whole month of march selected quiz master will ask a question and you have to answer all this question day by day and at the end of month you will have great chance to win Apple Ipad.

For more details you can visit following link:

http://beyondrelational.com/quiz/SQLServer/TSQL/2011/default.aspx

Hope you liked it.Stay tuned for more..

Shout it
Share:

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews