Jump to content

Dotnet experts ravali


KrishnaDevaRaya

Recommended Posts

Dependency injection with Unity container... ma project lo use chesthunnaru. 

online entha chadivina naku concepts ardham kavadam ledhu bro.

any links that explains the concept in simple steps  ? or meeku eviraikaina telusunte koncham explain cheyyandi bros plz ??

Link to comment
Share on other sites

MVC 5 with Unity for Dependency Injection

2:29 PM SARANGA RATHNAYAKE 3 COMMENTS  



Dependency Injection allows us to inject objects into a class, instead of constructing them internally. Unity is a dependency injection container that we can use for constructor, property, and method call injections.

You can get good idea about dependency injection by reading answers to this stack-overflow question.

This tutorial is an step-by-step guide with screen-shots where you can learn how to use Unity with Asp.Net MVC5.

I'm going to create sample web application for academic course listing website. There I have Institution and Course as main entities.

I will not connect this sample application with a database and will illustrate with hard-coded data. Let's start...

1. Create Sample Web Application

In this example I'm using Visual Studio 2013 and let's start by creating sample MVC web application.

Check whether you can run the created web application and if there are no issues it should give following output.

2. Install Unity.Mvc5 Library

To install Unity.Mvc5 Library using nuget, we have to go to Package Manager Console and execute the following command.

?

1

Install-Package Unity.Mvc5

Wait until it completes the installation.

After completion if you check the Solution Explore, you can see newly added UnityConfig class inside App_Start folder. That's where we register all our components with the container.

3. Update Sample Application

Let's update our sample application now, first I'll add two another Class Library projects called Entities and Services. There Entities project holds all the entity (model) classes we are going to use and Services project will be used to put our service classes.

Then I'll add following classes to created Entity project.

Institution Class

?

1

2

3

4

5

6

7

8

9

10

namespace Entities

{

    public class Institution

    {

        public long InstitutionID { get; set; }

        public string Name { get; set; }

        public string Telephone { get; set; }

        public string Address { get; set; }

    }

}

Course Class

?

1

2

3

4

5

6

7

8

9

10

namespace Entities

{

    public class Course

    {

        public long CourseID { get; set; }

        public long InstitutionID { get; set; }

        public string Title { get; set; }

        public string Description { get; set; }

    }

}

Next I will create the Services Class Library project and add reference to Entities project from Services project.

Now I will create two service classes for institutions and courses. And also I will add relevant interfaces for those classes.

IInstitutionService Interface

?

1

2

3

4

5

6

7

8

9

using Entities;

 

namespace Services

{

    public interface IInstitutionService

    {

        Institution GetInstitutionByID(long institutionID);

    }

}

InstitutionService Class

?

1

2

3

4

5

6

7

8

9

10

11

12

using Entities;

 

namespace Services

{

    public class InstitutionService : IInstitutionService

    {

        public Institution GetInstitutionByID(long institutionID)

        {

            return new Institution { InstitutionID = institutionID, Name = "Sample Institution", Address = "Sample Address", Telephone = "0123456789" };

        }

    }

}

ICourseService Interface

?

1

2

3

4

5

6

7

8

9

using Entities;

 

namespace Services

{

    public interface ICourseService

    {

        Course GetCourseByID(long courseID);

    }

}

CourseService Class

?

1

2

3

4

5

6

7

8

9

10

11

12

using Entities;

 

namespace Services

{

    public class CourseService : ICourseService

    {

        public Course GetCourseByID(long courseID)

        {

            return new Course { CourseID = courseID, Description = "Sample course description", InstitutionID = 1, Title = "Sample Course Title" };

        }

    }

}

Your solution explore should looks like below now with all those projects and classes.

We need to add references to Entities and Service projects within Unity Web Application before we continue.

4. Configure Unity

Let's register our components with the unity container. There first we have to update our UnityConfig class as below.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

using Microsoft.Practices.Unity;

using Services;

using System.Web.Mvc;

using Unity.Mvc5;

 

namespace UnityWebApplication

{

    public static class UnityConfig

    {

        public static void RegisterComponents()

        {

            var container = new UnityContainer();

             

            // register all your components with the container here

            // it is NOT necessary to register your controllers

             

            // e.g. container.RegisterType<ITestService, TestService>();

 

            container.RegisterType<ICourseService, CourseService>();

            container.RegisterType<IInstitutionService, InstitutionService>();

             

            DependencyResolver.SetResolver(new UnityDependencyResolver(container));

        }

    }

}

This RegisterComponents method can be called within the Application_Start method in Global.asax. We can update our Global.asax as below.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

using System.Web.Mvc;

using System.Web.Optimization;

using System.Web.Routing;

 

namespace UnityWebApplication

{

    public class MvcApplication : System.Web.HttpApplication

    {

        protected void Application_Start()

        {

            UnityConfig.RegisterComponents();

 

            AreaRegistration.RegisterAllAreas();

            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

            RouteConfig.RegisterRoutes(RouteTable.Routes);

            BundleConfig.RegisterBundles(BundleTable.Bundles);

        }

    }

}

5. Continue with Sample Application

Now we are going to display these institution and course information in our web application. I'll add new Empty Controller called Institution to our MVC application by right clicking Controllers folder.

Normally if we want to use our InstitutionService within the InstitutionController, we need to create instance of InstitutionService inside InstitutionController. Dependency injection is basically providing the objects that an object needs (here the InstitutionService) instead of having it construct them itself. I'm going to use constructor injection in this application and let the unity to create new instance of InstitutionService for InstitutionController. There we need to add constructor to our InstitutionController with parameter as below.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

using Services;

using System.Web.Mvc;

 

namespace UnityWebApplication.Controllers

{

    public class InstitutionController : Controller

    {

        private IInstitutionService institutionService;

 

        public InstitutionController(IInstitutionService institutionService)

        {

            this.institutionService = institutionService;

        }

 

        // GET: Institution

        public ActionResult Index()

        {

            return View(this.institutionService.GetInstitutionByID(1));

        }

    }

}

Let's add view to display our information, you can find folder called Institution inside Views folder and we will create new view called Index there.

Now modify your view code as below to display institution information.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

@using Entities;

@model Institution

 

@{

    ViewBag.Title = "Institution";

}

 

<hr />

 

Institution ID : @(Model.InstitutionID)

 

<hr />

 

Institution Name : @(Model.Name)

 

<hr />

 

Institution Address : @(Model.Address)

 

<hr />

 

Institution Telephone : @(Model.Telephone)

It's done, you can run your application and try /Institution/Index. You should get following output.

6. Move Unity Configuration to Separate File

We can move unity configuration to separate file which enable us to do modifications without recompiling existing application. We can do this by adding config section to web.config file as below.

?

1

2

3

4

5

6

7

8

<configuration>

  <configSections>

    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />

    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->

    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

  </configSections>

  <unity configSource="unity.config" />

  <connectionStrings>

unity.config

?

1

2

3

4

5

6

7

<?xml version="1.0" encoding="utf-8" ?>

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">

  <container>

    <register type="Services.IInstitutionService, Services" mapTo="Services.InstitutionService, Services" />

    <register type="Services.ICourseService, Services" mapTo="Services.CourseService, Services" />

  </container>

</unity>

We need to change the UnityConfig to load the configuration from given file, modified code can be found below.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

using Microsoft.Practices.Unity;

using Microsoft.Practices.Unity.Configuration;

using Services;

using System.Web.Mvc;

using Unity.Mvc5;

 

namespace UnityWebApplication

{

    public static class UnityConfig

    {

        public static void RegisterComponents()

        {

            var container = new UnityContainer();

             

            // register all your components with the container here

            // it is NOT necessary to register your controllers

             

            // e.g. container.RegisterType<ITestService, TestService>();

 

            //container.RegisterType<ICourseService, CourseService>();

            //container.RegisterType<IInstitutionService, InstitutionService>();

 

            container.LoadConfiguration();

             

            DependencyResolver.SetResolver(new UnityDependencyResolver(container));

        }

    }

}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...