Step by Step implementing Onion architecture in ASP.NET MVC Application

Source Code on the GitHub

Recently I gave a talk on Onion Architecture in MVC application to the audience of 300 at the C-Sharpcorner conference 2015. Talk was well received and I had a many request to write a step by step blog post on the same. So here I am writing that. In this blog post is written in simplest words and will help you in implementing onion architecture in ASP.NET MVC application. Even though theme of this post is ASP.NET MVC, you can use the core, infrastructure and the test project with other kind of applications as well like WCF or WPF.

You may want to read: Refactoring the ASP.NET MVC Application to the Onion Architecture

Source Code on the GitHub

What is onion architecture

Onion Architecture is the preferred way of architecting application for better testability, maintainability and dependability on the infrastructures like databases and services. This term was first coined by Jeffery Palermo in his blog back in 2008.

image

Advantages of Onion architecture
  • In the Onion Architecture layers talk to each other using the Interfaces. Any concrete implantation would be provided to application at the run time.
  • Any external dependency like database access and the web service call are part of the external layers
  • UI is part of the external layers
  • Objects representing domain are part of the internal layers or they are the centers
  • External layers can depend on the layers internal to it or central to it
  • Internal layer should not depend on the external layers.
  • Domain object which is at the core or centre can have access to the both the UI and the database layers.
  • All the coupling are towards the centre
  • Codes which may change often should be part of the external layers

Project structure

Let us start with creating the solution structure. We are going to work with the four projects.

  1. Core project (Class library )
  2. Infrastructure project (Class library)
  3. Test project (Unit Test project)
  4. Web project (MVC project)

Core project will contain the domain entities and the repositories interfaces. Infrastructure project will contain the classes (in this case EF data context) which will work with the database. The Test project will have the tests and the MVC project will contain the MVC controllers and the view.

Solution structure should look like as listed in the below image.

image

Core project

The core project is the inner most layer of the architecture. All the external layers like infrastructure, web etc. will use and depend on the core layer. However core project does not depend on any other layers.

Usually the core project should contain,

  • Domain entities
  • Repositories interfaces

It should not have any external dependencies. For example we must not have the following references in the core project,

  • Reference of the ORM like LINQ to SQL or EF
  • Reference of ADO.NET libraries
  • Reference of Entity Framework etc.
Create Entity

Let us go ahead and create BloodDonor entity class.


public class BloodDonor
    {
       public string BloodDonorID { get; set; }
       public string Name { get; set; }

       public DateTime Dob { get; set; }

       public string BloodGroup { get; set; }

       public string City { get; set; }

       public string Country { get; set; }

       public int PinCode { get; set; }

       public string PhoneNumber { get; set; }

       public string Email { get; set; }

       public bool IsActive { get; set; }

       public bool IsPrivate { get; set; }

       public bool IsVerified { get; set; }

    }

We may have a requirement to have some restrictions on the entity properties. For example maximum length, required etc. We do have two choices to achieve this,

  1. Using System.ComponentModel.DataAnnotations
  2. Use Entity Framework fluent API.

Both above approaches have their own advantages. However to keep core project without having any external dependencies like EntityFramework, I prefer to use DataAnnotations. To use DataAnnotations add System.ComponentModel.DataAnnotations reference in the core project. We can modify the entity class as shown in listing below,

using System;
using System.ComponentModel.DataAnnotations;

namespace LifeLine.Core
{
   public class BloodDonor
    {
       [Required]
       public string BloodDonorID { get; set; }
       [Required]
       [MaxLength(50)]
       public string Name { get; set; }
       [Required]
       public string BloodGroup { get; set; }
       [Required]
       public string City { get; set; }

       public string Country { get; set; }
       [Required]
       public int PinCode { get; set; }
       [Required]
       public string PhoneNumber { get; set; }

       public string Email { get; set; }

       public bool IsActive { get; set; }

       public bool IsPrivate { get; set; }

       public bool IsVerified { get; set; }

    }
}

As of now we have created the BloodDonor entity class with the data annotations. Next we need to add the repository interface.

Create Repository interface

We are going to follow the repository pattern. Roughly repository pattern allows us to replace the database access codes (class) without affecting the other layers. In the repository interface we will put definition of all the database operations need to perform.


using System.Collections.Generic;

namespace LifeLine.Core.Interfaces
{
    public interface IBloodDonorRepository
    {
        void Add(BloodDonor b);
        void Edit(BloodDonor b);
        void Remove(string BloodDonorID);
        IEnumerable<BloodDonor> GetBloodDonors();
        BloodDonor FindById(string BloodDonorID);

    }
}

As of now we have created the domain entity and the repository interface. Next let us go ahead and create the infrastructure project.

Infrastructure project

In the infrastructure project we perform operations related to outside the application. For example,

  • Database operation
  • Accessing outside service
  • Accessing File systems

Above operations should be part of the infrastructure project. We are going to use the Entity Framework to perform the database operations. We are working with the Entity Framework code first approach, so we need to perform the following steps,

  1. Create the data context class
  2. Implement the repository class
  3. Create the data base initializer class

Before we go ahead and create the data context class, let us go ahead and add reference of the Entity Framework. To add the reference right click on the infrastructure project and select manage Nuget package. From Nuget package manager select the Entity framework package to install in the project.

DataContext class

Let us create the data context class. In the EF code first approach, we create the data context class which will represent the database. Since we have only one business entity, so we will create one table BloodDonors.


using System.Data.Entity;
using LifeLine.Core;

namespace LifeLine.Infrastructure
{
   public class BloodDonorContext : DbContext
    {
       public BloodDonorContext()
           : base("name=BloodDonorContextConnectionString")
       {
           var a = Database.Connection.ConnectionString;
       }

       public DbSet<BloodDonor> BloodDonors { get; set; }
    }
}

Connection string

Optionally either you can pass the connection string or rely on the Entity Framework to create the database. We will set up the connection string in app.config of the infrastructure project. Let us go ahead and set the connection string as shown in the below listing,


  <connectionStrings>
    <add name="BloodDonorContextConnectionString" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=BloodDonors;Integrated Security=True;MultipleActiveResultSets=true" providerName="System.Data.SqlClient"/>
  </connectionStrings>

Database initialize class

We want database to initialize with some initial values. It can be done as shown in the below listing,

using System;
using System.Data.Entity;
using LifeLine.Core;

namespace LifeLine.Infrastructure
{
   public class BloodDonorInitalizeDb : DropCreateDatabaseIfModelChanges<BloodDonorContext>
    {

       protected override void Seed(BloodDonorContext context)
       {
           context.BloodDonors.Add(
           new BloodDonor
           {
               Name = "Rahul Kapoor",
               City = "Gurgaon",
               BloodGroup = "A+",
               BloodDonorID = "BD1",
               Country = "India",
               IsActive = true,
               IsPrivate = false,
               IsVerified = true,
               PhoneNumber = "91+7378388383",
               PinCode = 122002,
               Email = "Rahul@abc.com"

           });
           context.BloodDonors.Add(
               new BloodDonor
               {
               Name = "Salman Khan",
               City = "Mumbai",
               BloodGroup = "A-",
               BloodDonorID = "BD2",
               Country = "India",
               IsActive = true,
               IsPrivate = false,
               IsVerified = true,
               PhoneNumber = "91+84848484",
               PinCode = 25678,
               Email = "Salman@abc.com"
               }
       );
            base.Seed(context);
       }
    }
}

We are setting the value that if the model changes recreate the database. We can explore the other options of the entity framework also.

Repository class implementation

Next we need to implement the repository class. Repository class will access the database using the LINQ to Entity. To create the BloodDonorRepository class let us go ahead and create a class which will implement the IBloodDonorRepository interface.


using System.Collections.Generic;
using System.Linq;
using LifeLine.Core;
using LifeLine.Core.Interfaces;

namespace LifeLine.Infrastructure
{
   public class BloodDonorRepository : IBloodDonorRepository
    {
       BloodDonorContext context = new BloodDonorContext();
        public void Add(BloodDonor b)
        {
            context.BloodDonors.Add(b);
            context.SaveChanges();
        }

        public void Edit(BloodDonor b)
        {
            context.Entry(b).State = System.Data.Entity.EntityState.Modified;
        }

        public void Remove(string BloodDonorID)
        {
            BloodDonor b = context.BloodDonors.Find(BloodDonorID);
            context.BloodDonors.Remove(b);
            context.SaveChanges();
        }

        public IEnumerable<BloodDonor> GetBloodDonors()
        {
            return context.BloodDonors;
        }

        public BloodDonor FindById(string BloodDonorID)
        {
            var bloodDonor = (from r in context.BloodDonors where r.BloodDonorID == BloodDonorID select r).FirstOrDefault();
            return bloodDonor;
        }
    }
}

To implement the repository class, we are using the LINQ to Entity for the database operations. For example to add a blood donor,

  1. Create object of context class
  2. Use context.entiy.Add(entity)
  3. Use context.SaveChnages()

As of now we have implemented the infrastructure project. Make sure to build the project to confirm everything is fine.

Directly MVC project or should we write test?

After creation of core project and the infrastructure project, we need to take a decision that whether we want to directly create the MVC project or write the unit test for the repository class. Better approach would be to write the Unit tests for the repository class such that we would be sure that database related code has been implemented correctly.

Test Project

We have created a test project by selection Unit Test project template from the Test project tab. To start writing test, very first in the unit test project we need to add following references

  1. Reference of the core project
  2. Reference of the infrastructure project
  3. Entity framework package
  4. Reference of the System.LINQ

In the test project, I have created a class called BloodDonorRepositoryTest. Inside the unit test class initialize the test as shown in listing below,

 


BloodDonorRepository repo;
        [TestInitialize]
        public void TestSetUp()
        {

            BloodDonorInitalizeDb db = new BloodDonorInitalizeDb();
            System.Data.Entity.Database.SetInitializer(db);
            repo = new BloodDonorRepository();
        }

In the test setup, we are creating instance of BloodDonorInitalizeDb class and then setting up the database with the initial values. Also in the test setup, instance of repository class is created. Next let us create test to validate whether data base is initialized with the right number of data or not.

[TestMethod]
        public void IsRepositoryInitalizeWithValidNumberOfData()
        {
            var result = repo.GetBloodDonors();
            Assert.IsNotNull(result);
            var numberOfRecords = result.ToList().Count;
            Assert.AreEqual(2, numberOfRecords);
        }

In the test above we are calling the GetBloodDonors method of the repository class and then verifying the number of records. In ideal condition above test should be passed. You can verify the test result in Test-Window-Test Explorer

clip_image002

We can right tests for add, edit, and delete also. As of now we have written test to verify that database is getting created along with the initialized data. We can be sure about the implementation of the repository class after passing the test. Now let us go ahead and implement the web project.

Web Project

We are going to create MVC project which will use the core project and the infrastructure project. Add following reference in the MVC project

  1. Reference of infrastructure project
  2. Reference of core project
  3. Entity framework package

After adding all the references build the web project. If everything is find we should get successful build. Next let us write click on the Controller folder and add a new controller. Select create controller with Entity Framework with view option. Give name of the controller as BloodDonorsController.

Also we need to select the following options,

  1. BloodDonor class from the core project as the Model class
  2. BloodDonorContext class as the data context class from the Infrastructure project

image

We have created the controller along with the view using the scaffolding. At this point if we notice the web project, we will find BloodDonors controller and a BloodControllers subfolder in the view folder.

image

 

As of now we have created the controller using the model and data context class. Next we need to create instance of BloodDonorInitalizeDb class and set the database. We need to write this code inside the global.asax .


BloodDonorInitalizeDb db = new BloodDonorInitalizeDb();
            System.Data.Entity.Database.SetInitializer(db);

Last we need to copy the connection string from the app.config of infrastructure project to web.config of the web project. So let us go ahead copy the connection string in the web.config


<connectionStrings>
    <add name="BloodDonorContextConnectionString" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=BloodDonors;Integrated Security=True;MultipleActiveResultSets=true" providerName="System.Data.SqlClient"/>
  </connectionStrings>

At this point if we run the application we should able to perform the CRUD operations.

image

 

Dependency Injection using Unity

We have a running application but there is a problem. On noticing the controller class closely, we will find that object of the data context class is directly created inside the controller. We used scaffolding to create the controller using the entity framework and it causes creation of data context object inside the controller. Right now the controller and the data context class are tightly coupled to each other and if we change database access program then controller will be affected also.

We should not have the BloodDonorContext object inside the controller.

clip_image002[6]

We have already created the repository class inside the infrastructure project. We should use the repository class inside the controller class. We have two options to use the repository class

  1. Directly create the object of repository class in the controller class
  2. Use dependency injection to resolve repository interface to the concrete repository class object at the run time using the unity container.

We are going to use the second approach. To use the unity container add Unity.Mvc5 package in the web project using the manage Nuget package.

image

After installing the unity package, we need to register the type. To register the type open the UnityConfig class inside the App_Start folder. As highlighted in in the below image add the register.

clip_image002[8]

 

And inside the global.asax register all the components of the UnityConfig as shown in below image.

clip_image002[10]

So far we have added the unity container reference and register the type. Now let us go ahead and refactor the controller class to use the repository class. To start with

  1. Create a global variable of the type IBloodDonorRepository
  2. Create controller class constructor with a parameter as shown in below image

image

Once this is done we need to refactor the controller class to use functions of repository class instead of the data context class. For example Index method can be refactored as shown below,

image

After refactoring the BloodDonorController class will look like as shown in below listing,

 


using System.Linq;
using System.Net;
using System.Web.Mvc;
using LifeLine.Core;
using LifeLine.Core.Interfaces;
namespace LifeLine.Web.Controllers
{
    public class BloodDonorsController : Controller
    {
        IBloodDonorRepository db; 

        public BloodDonorsController(IBloodDonorRepository db)
        {
            this.db = db;
        }

        // GET: BloodDonors
        public ActionResult Index()
        {
            return View(db.GetBloodDonors().ToList());
        }

        // GET: BloodDonors/Details/5
        public ActionResult Details(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            BloodDonor bloodDonor = db.FindById(id);
            if (bloodDonor == null)
            {
                return HttpNotFound();
            }
            return View(bloodDonor);
        }

        // GET: BloodDonors/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: BloodDonors/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "BloodDonorID,Name,BloodGroup,City,Country,PinCode,PhoneNumber,Email,IsActive,IsPrivate,IsVerified")] BloodDonor bloodDonor)
        {
            if (ModelState.IsValid)
            {
                db.Add(bloodDonor);
                return RedirectToAction("Index");
            }

            return View(bloodDonor);
        }

        // GET: BloodDonors/Edit/5
        public ActionResult Edit(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            BloodDonor bloodDonor = db.FindById(id);
            if (bloodDonor == null)
            {
                return HttpNotFound();
            }
            return View(bloodDonor);
        }

        // POST: BloodDonors/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "BloodDonorID,Name,BloodGroup,City,Country,PinCode,PhoneNumber,Email,IsActive,IsPrivate,IsVerified")] BloodDonor bloodDonor)
        {
            if (ModelState.IsValid)
            {
                db.Edit(bloodDonor);
                return RedirectToAction("Index");
            }
            return View(bloodDonor);
        }

        // GET: BloodDonors/Delete/5
        public ActionResult Delete(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            BloodDonor bloodDonor = db.FindById(id);
            if (bloodDonor == null)
            {
                return HttpNotFound();
            }
            return View(bloodDonor);
        }

        // POST: BloodDonors/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(string id)
        {
            BloodDonor bloodDonor = db.FindById(id);
            db.Remove(bloodDonor.BloodDonorID);
            return RedirectToAction("Index");
        }

    }
}

 

Conclusion

This is it. These are the steps we need to follow to create a MVC application following to onion architecture.

Source Code on the GitHub

Have something to add? Please add in the comment section.

Presented 3 talks in C# Corner annual conference 2015

image

Read official recap here

Once again it was great show at C# Corner Annual conference 2015 with more than 500 attendees and around 10 speakers. I had an opportunity to present on three different topics in this esteem conference. I presented on following topics,

  1. 9 JavaScript tips for .NET developers
  2. Goodness of IgniteUI in AngularJS
  3. Onion Architecture in ASP.NET MVC application

image

I had the first session of the conference on 9 JavaScript tips. In this 45 min session I talked about various tips related to JavaScript functions, scope, objects, DOM etc.

clip_image002

I had an opportunity to co present the session with great Jason Beres. While in the session he gave awesome insight of IOT, Cloud and Data Visualization, I had an opportunity to show a step by step demo on AngularJS and Ignite UI. Jason had to say as shown in below image about the demo

clip_image003

My third session was on onion architecture in ASP.NET MVC application. This session went crazily houseful. During the session official social media channel of conference posted the update shown in below image on their official Facebook.

clip_image004

There were around 50 people seating on floor and same number of audience were standing for this session. I was very happy seeing this response.

clip_image006

Once again it was great show by the C# corner team. I congratulate the team for the great work and thank Mahesh Chand  sir for the opportunity.

clip_image008

Last but not least congratulation to all the C# corner MVP. They inspire me to get going.

clip_image010

I am already excited for C# Corner conference 2016.

11 things about JavaScript functions that .NET developers should know: Part 2

Read full article on the Infragistics blog

This is the second part of the series in 11 Things about JavaScript Functions that .NET Developers Should Know . In part 1 we discussed the following topics,

  1. JavaScript functions as an expression
  2. JavaScript functions as a statement
  3. Return statements in JavaScript functions
  4. Parameters in JavaScript functions
  5. The Arguments object in JavaScript functions
  6. Varargs JavaScript functions

In this part we will discuss the following topics,

  1. Scopes in functions
  2. Nested functions and Lexical scoping
  3. Functions’s this keyword
  4. Function as constructor
  5. The call() and the apply() methods

Scopes in function

Scoping stands for current context of code execution. Either a variable o expression can be executed in the context of the function they are created or in the global context of the JavaScript. JavaScript does not have the block scoping. It only supports scoping at the function level or global level.

Any variable defined anywhere in the function can be used everywhere in the function but not outside the function.

Read full article on the Infragistics blog

Presenting on Onion Architecture in MVC and JavaScript tips in C-SharpCorner conference 2015

I am very excited for 5th edition of C-SharpCorner conference, which is the largest developer conference in the north India. I have been speaking in the C-SharpConference since its 1st edition, and have seen it evolving from mere 10 attendees to almost 1000 attendees in its last (2014) edition. I take pride speaking in this conference.

This year conference is happening on 4th April and around 1000 developers are expected to attend. Are you joining? Register here [Oops seems ticket sold out, feel free to reach me if you need one]

image

I am presenting two talk in the conference,

  1. Nine tips in High performance in JavaScript
  2. Onion Architecture in ASP.NET 5.0 MVC application

JavaScript talk is the 1st talk of the conference (starting 9 AM) just before the keynote session. Whereas Onion Architecture talk is after the lunch (starting 2.20 PM).

In the JavaScript talk, I will cover

  • How to manipulate DOM
  • Document.getElementbyId or queryselector
  • Using XmlHttpObject
  • Various JS functions tips
  • Object, Overriding , Inheritance in JavaScript

In the Onion Architecture talk, I will cover

  • · What is onion architecture
  • · Refactor existing application to onion architecture
  • · Create application from scratch adhering onion architecture

I am very excited for both the talk. Hope to see you there.

Simplifying AngularJS in 60 Minutes : Join the webinar on 27th March 3 PM India time

 

Register for the webinar here

Learn more about Infragistics

image

 

I am excited to announce that for Infragistics, I am hosting a webinar: Simplifying AngularJS in 60 Minutes on 27th March 2015 at 3 PM IST.

Usually webinars are hosted in the EST, and most of the developers from the Indian subcontinent are not able to join that due to the time constraints. To help the developers from the subcontinent Infragistics is bringing this webinar. To join Register here

AngularJS is Google’s answer to client-side JavaScript MVC application development. There is no question that it is one of the most popular client side frameworks out there.

In 60 minutes webinar we will cover following topics,

  • Setting up Visual Studio for AngularJS
  • Setting up Sublime, Node, and Bower for AngularJS development
  • Understanding $scope, $rootScope, and scope hierarchy
  • Controller and nested controllers
  • Services
  • Custom Directives with isolated scopes

Register for the webinar here

Date: 27th March 2015, Friday

Time: 3 PM to 4 PM IST

Register for the webinar here .  Excited to see you on Friday 27th March at 3 PM. Happy coding.