You are currently viewing Using Abstraction for Unit Testing in a Restaurant App
using-abstraction-for-unit-testing-in-a-restaurant-app

Using Abstraction for Unit Testing in a Restaurant App

  • Post published:October 7, 2016

Unit testing is core implementation when it comes to the well organize architecture of any application. vteam #247’s client required an application for his restaurant business that would use a combination of different design patterns. In this way, different layers could be tested as independent components and he could make sure that no other part of the project was disturbed due to the modifications in the system.

After analyzing the requirements, and keeping in mind that project layers should be testable independently, vteams came up with the following design pattern for a restaurant web based application:

  • Repository Pattern
  • Unit Of Work
  • Dependency Injection
    .

The interface plays an important role to build a loosely coupled architecture, because it shows an abstraction of a system. So, vteams prepared a Data Access Layer, using repositories, with interface implementation in order to expose operations which were required either by the Service Layer or directly by the web application. By implementing the Repository pattern, vteams achieved the following benefits:

  1. Query logic centralized
  2. Reusable functionality
    .

unit-testing-1

To protect the modifications made to the payment gateway, in case of future updates or insertions in multiple tables, Unit Of work was implemented. This would either successfully commit or rollback the transaction, in case of failure by using different repositories in the project.

Now, the data access layer was separated in a library project, which can be used in any other project/class library due to its abstraction. The Restaurant application was developed using ASP.NET MVC, with C# programming language so as to behave for the developed class library as a client. vteams wanted to avoid any instance of a Repository directly within the controller class. This is where Dependency Injection comes in; it injects the concrete implementation into the abstraction and allows lose coupling between the Repository layer and web application.

unit-testing-2

AutoFac was used as Dependency container, which can inject the concrete implementation within the controller constructor. By using the instance of repository, the can client can achieve any level of a particular functionality through operations. Ultimately vteams achieved the primary goal by preparing different layer using abstraction for the client.