Dependency Injection in ASP.Net Web Forms

I have started writing tips on my different blog instead of this one. Make sure to follow that one so that you can get latest updates. Here is the link- StackBlogger: Tips and Tricks about Programming

Also follow my another Blog where I write how to make money online, tips about blogging, content creation etc. Here is the link- FlexiPlanet: A hub for Content Marketing.

Today, we will learn about injecting the Dependency in ASP.Net Web Forms using Unity Container.

Dependency Injection is a Software design pattern which we use in C# to reduce class dependency.

The Unity Container (Unity) is a lightweight, extensible dependency injection container. As we know, there are many dependency injection containers and unity is one of them.

Steps to implement DI (Dependency Injection) in project are as follows:-

Step 1 – Create a new ASP.Net Web Forms project

Open the Visual Studio and create a new ASP.Net Web Forms Empty project as follows-

Step 2 – Create few folders

Create few folders in root directory of project and make your project’s solution directory look like this-

Step 3 – Add a new Model class

Create an User.cs model in Model folder and paste below codes-

public class User
 {
 public string Email { get; set; }
 public string FirstName { get; set; }
 public string LastName { get; set; }
 }

Step 4 – Add new interface

Add a new interface IUserRepository.cs  in IRepository  folder and paste below codes in that-

public interface IUserRepository
 {
 IEnumerable<User> GetUsers();
 }

Step 5 – Add new repository class

Add a new repository UserRepository.cs  in Repository  folder and paste below codes in that-

public class UserRepository : IUserRepository
 {
 IList<User> userList = new List<User>();
 public UserRepository()
 {
 User user = new User
 {
 Email = "jimcute8879@gmail.com",
 FirstName = "Jameer",
 LastName = "Khan"
 };
 userList.Add(user);
 }
 public IEnumerable<Model.User> GetUsers()
 {
 return userList;
 }
 }

Step 6 – Open Package Manager Console and install Unity.WebForms

Run below code in Package Manager Console in order to install the Unity Container-

Install-Package Unity.WebForms



It will add an ASP.Net folder in project and will add the Unity Container default class in that-

Step 7 – Register the Interface and Implementation classes

Register all your Interface and Implementation classes in UnityWebFormsStart.cs file.

private static void RegisterDependencies(IUnityContainer container)
 {
 // TODO: Add any dependencies needed here
 container.RegisterType<IUserRepository, UserRepository>();
 }

Step 8 – Call the interface in your Default.aspx file

[Dependency]
 public IUserRepository _userRepository { get; set; }
 protected void Page_Load(object sender, EventArgs e)
 {
 var users = _userRepository.GetUsers();
 }

Step 9 – View the injected dependency

Now run the project and you would see that it has injected the required dependency while running the project-

Visit my Github Repository here to download the full project.

That’s all!!!

Any comment regarding DI are most welcome..

First day of working in Laitkor Infosolutions Pvt. Ltd.

It is a great experience to work in Laitkor infosolutions Pvt. Ltd. Lucknow for first day.
I’m tired off. There was nothing to do by whole day but although I’m tired.

It’s my first day of first job.

If we get the job according to our choices then we gives better output and works perfectly and I got my first job according to my choice.

Get understand Go lang…………….

Hello friends I am Jameer, student at Dr MCSCET Lucknow, and I am a web developer.

Go lang is the New language from Google, and peoples can use this language to develop secured apps and there are many frameworks by which users can make websites using Go lang.

Google is broad company and its language is interesting and easy to learn.

People can get the Go lang job details at  www.golangprojects.com .

There are many frameworks that you can use in your projects when you are working with web projects like-  The Revel web framework (robfig.github.io/revel), Beego (beego.me), Martini (http://martini.codegangsta.io/), traffic (https://github.com/pilu/traffic) and many more…

 

Best language to develop apps…

google-go-language

How Microsoft has impacted my life.

I am Jameer Khan, student at Dr M C Saxena college of Engg & Tech, Lucknow, and I am writing this blog for first round selection process at Microsoft Student Associates.  

Microsoft has a great influenced at every person’s life and gives a way to live.                                                                                               I started my career and touched the computer when I came for B.Tech. In those days I was not aware with programs that how to write programming code in any language. After 6 months, I started doing program on Turbo C and later my interest about programs increased day by day. I was in 2nd year when I start ASP.Net and start developing web pages using ASP.Net but I was worried that .aspx pages are slower in execution so I wanted to leave ASP.Net but after 6 months I start learning MVC and I saw that MVC websites with Razor web pages are executing fast. Then I decided to continue with Microsoft and now I am so much happy with ASP.Net, MVC and Razor.

My interest towards Microsoft is increasing day by day and I want to learn more things about Microsoft.

I am writing this blog for the first round selection in Microsoft Student Associates  and I suggest to everyone to get registered for Microsoft Student Associates.

 

 

 

“How I plan to bring a change to the society being a MSA:”

 

I believe that it will be more responsibility after becoming a Microsoft Student Associates. I understand my responsibility and I can do it by- making Applications on how to interact users towards Microsoft and also by making websites on it. I have plan to make to Microsoft club in which my colleagues will be involved and we will make a large club group with enthusiast group of my college.