Architecting Web API
Architecting Web API
0 - CodeProject
13,053,844 members (59,381 online) Sign in
Password
Architecting Web API Project With Repository Layer,Dependency Injection And Entity
FrameWork 6.0 Sign in with
Here in this article we will learn step by step creating WebAPI with layered archicture,dependecy Injection and code first migration.
Introduction
ASP.Net Web API is a framework from Microsoft to develop RESTful services that reach a broad range of clients like(Browser,Mobile,Desktop application,iot,etc).It is mainly used to create
HTTP services.
These API transfer data through XML,JSON or any format.So in this article we will learn how to create web Api.Here you will get to know abouy following things.
Table of contents
Introduction of REST and WebAPI
Working with WEBAPI project.
Creating project Layer in WEB API.
Working with Entity Framework Identity framework.
Dependency Injection in WebAPI.
Repository pattern Implemantation.
Testing API using POSTMAN.
Authentication and Authorization in Web API
REST stands for "Representational state transfer".So to find the exact meaning of REST i did a homework and got diffrent ideas of diffrent authors,but what i understand is given below.
Representational - Every data,image, page, video,audio, etc in the server is consider as Resources.
When a user requested for any resources to the server the resources are transmitted to the user and represented in diffrent format like HTML, Image, JSON, XML etc.
So the term Representational described how the data is represented to its client.If it is an website normally the data is represented by HTML where as if we are creating any services it may be
represented as JSON or XML.
State Transfer:-The data or information/resources of a particular entity present in a specific time in the server is called state.
so the state of the entity may be change over time.Lets take an example of registered user records present in the server,So the registered users can be increased or decresed at any
time, Changing the users per time represents its state .
So the term transfer represent the exchange of these state resources to the clients over HTTP protocol.To access these data from the client side we have HTTP verbs
like(GET,POST,PUT,DELETE).
https://www.codeproject.com/Articles/1192403/Architecting-Web-API-Project-With-Repository-Layer 1/16
27/7/2017 Architecting Web API Project With Repository Layer,Dependency Injection And Entity FrameWork 6.0 - CodeProject
So now lets start creating a WebApi from scratch.To crate a webapi project goto create new Project and select an api template there,
After selecting this Web API template .please change the Authntication to Individual User Account.
This account is mainly responsible to add the controllers,model that are related to the User Identity database.If you dont select any authentication by default No Authentication will be
selected.After clicking OK you will successfully create your project as follow.
So here our main intension to create a WEBAPI project with Layered archicture with implementing Repository pattern and Dependency Injection.So add 4 more projects of type class Library
and give them a meaning ful name as follow.
https://www.codeproject.com/Articles/1192403/Architecting-Web-API-Project-With-Repository-Layer 2/16
27/7/2017 Architecting Web API Project With Repository Layer,Dependency Injection And Entity FrameWork 6.0 - CodeProject
1. WebAPI.IBLL Layer:
As we will work on Repository pattern we need a intermediate layer between our controller and Data Access layer,so this layer plays an important role in creating loosly coupled
application by acting as a mediator between the controller and Dataaccess layer.
This layer mainly contains interfaces where the abstract methods are declared.
Lets consider we are going to create a products related API projects, so this layer will contain interface as follow.
namespace WebAPI.IBLL
{
public interface IProductDetails
{
bool SaveProducts(Products pob);
List<Products> showDetails();
bool DeleteDetails(int id);
}
}
2.WebAPI.BLL Layer:
This layer contains classes which is mainly used to implement all the interface methods in that.The classes which implement the interface abstract methods must be inherited from that
interface.
This layer can be considered as the Data Access layer of the project.This contains all the data quering techniques to interact directly with database.
As shown in the image i have added a class file called "productDetailsHelper" and write the following code.
namespace WebAPI.BLL
{
public class ProductDetails : IProductDetails
{
APIContext _webcontext = new APIContext();
https://www.codeproject.com/Articles/1192403/Architecting-Web-API-Project-With-Repository-Layer 3/16
27/7/2017 Architecting Web API Project With Repository Layer,Dependency Injection And Entity FrameWork 6.0 - CodeProject
obj.ProductName = x.ProductName;
obj.Price = Convert.ToInt32(x.Price);
li.Add(obj);
});
return li;
}
else
{
return li;
}
So if we see without any WebAPI.IBLL Layer our project will be tightly coupled and be seen like this.
Tightly coupled means two classes or two modules are fully dependent upon each other. Changing one class object may lead to change in several areas in the other class.When we are
creating an object of a class and calling other class method by its object then we can say that the two classes are tightly coupled or dependent on each other.
So here in this scenario to decouple this Bussiness module and DataAccess Layer we have introduced a new layer called as "WebAPI.BLL Layer".
so a repository Layer acts like a intermediate layer between the Bussiness classes(it can be anythings like your api controller,classes,etc) and the data access logic(which mainly contain logic
to connect with DB and doing CRUD opperation).
A repository isolates all the data access code from bussiness logic.
https://www.codeproject.com/Articles/1192403/Architecting-Web-API-Project-With-Repository-Layer 4/16
27/7/2017 Architecting Web API Project With Repository Layer,Dependency Injection And Entity FrameWork 6.0 - CodeProject
After using this repository layer now our application looks in such way.
3.WebAPI.Data:This layer mainly contains your Edmx files if you are adding your database entities using "Entity Data Model wizard". Here i have not added any edmx file rather then that i
have manually added my own context.
So i have named it as "APIContext".
The class that is inherits from DbContext is called context class in entity framework.DbContext is an important part of Entity Framework. It act a bridge between your domain or entity classes
and the database.
DbContext performs 3 important functions these are-
1.
DbContext contains entity set (DbSet<TEntity>) for all the entities which is mapped to DB tables.
2. DbContext converts LINQ-to-Entities queries to SQL query and send it to the database for CRUD opperation.
3. DbContext converts raw table data into entity objects.
4. DbContext constructor takes the connection string name to check the DB.If the DB is present it will work on it else it will create a new DB.
namespace WebAPI.Data
{
public class APIContext:DbContext
{
public APIContext() : base("DefaultConnection")
{
}
public DbSet<Products> products { get; set; }
}
}
This APIContext class is inheritad from Dbcontext class and i am passing my connection string name in this constructor.
Note:In this Layer we need to add entity frame DLL.
4.WebAPI.Model:
This contain all the models.Each models is considered as a table.so these models names are passed to the Dbset.
https://www.codeproject.com/Articles/1192403/Architecting-Web-API-Project-With-Repository-Layer 5/16
27/7/2017 Architecting Web API Project With Repository Layer,Dependency Injection And Entity FrameWork 6.0 - CodeProject
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WebAPI.Model
{
[Table("products")]
public class Products
{
[Key]
public int slNO { get; set; }
So in this way we can make layering our project.Now lets see the archicture diagram of the project.
SET QUOTED_IDENTIFIER ON
GO
GO
Now its seems to be every thing is set,now we will create an api controller and perform our CRUD opperation.So here let me add my API controller.
We need to pass our data from api controller to WebAPI.BLL layer for any opperation.
so to pass our data from api controller to productDetail class methods we need to create object of productDetail class and using that object we have to call its methods.
we will do something like this-
https://www.codeproject.com/Articles/1192403/Architecting-Web-API-Project-With-Repository-Layer 6/16
27/7/2017 Architecting Web API Project With Repository Layer,Dependency Injection And Entity FrameWork 6.0 - CodeProject
[Route("addProduct")]
[Authorize]
public async Task<HttpResponseMessage> saveProductDetails(Products pod)
{
So as we are thinking to do like this it will make our application tightly couple.So if the object creating principle can be removed and done by some other third party object at run time then
it will be good for us and our application become loosly coupled.so to create object automatically during runtime we need to add some dll to our project.
So here i am adding Ninject to our application which will mainly take care of this things and makes our application independent.This whole strategy is considered as dependency Injection.
Dependency Injection:
Dependency injection is a technique to develop software in an independent way. Independent in the sense no module will depend on another module in the software development.So the
dependency injection technique will inject all dependencies of lower modules and provide these dependency to the higher module without direct contact.
Note:
High-level modules/classes implement business rules or logic in an application.
Low-level modules/classes deal with different operations; in other words, they mainly deal with writing databases logics,communicating with external entities,etc,
There are three basic type of Dependency Injection
1. Constructor Injection
2. property injection
3. Methods Injection
Constructor injection is the most commonly used Dependency injection.To use this we need two things-
1. Repository pattern archicture
2. A DI container
we have already implement our project in Repository pattern,so here we need a DI Container.Here i am using Ninject for this.
Double click this and configure your dependency like this in RegisterServices like this.
namespace MyProductAPI.App_Start
{
using System;
using System.Web;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Common;
using WebAPI.IBLL;
using WebAPI.BLL;
using System.Web.Http;
using WebApiContrib.IoC.Ninject;
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
https://www.codeproject.com/Articles/1192403/Architecting-Web-API-Project-With-Repository-Layer 7/16
27/7/2017 Architecting Web API Project With Repository Layer,Dependency Injection And Entity FrameWork 6.0 - CodeProject
/// <summary>
/// Stops the application.
/// </summary>
public static void Stop()
{
bootstrapper.ShutDown();
}
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
GlobalConfiguration.Configuration.DependencyResolver = new NinjectResolver(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IProductDetails>().To<ProductDetails>().InRequestScope();
}
}
}
Note:Please note this is a autogenerated code you need to take care only RegisterServices code.
Now go to your web api controller and create a constructor to inject the dependency like this.
IProductDetails iprod;
Now using this iprod i can call all my Bll methods and thus here i will create all my apis like this.
}
catch(Exception ex)
{
throw ex;
}
}
else
{
https://www.codeproject.com/Articles/1192403/Architecting-Web-API-Project-With-Repository-Layer 8/16
27/7/2017 Architecting Web API Project With Repository Layer,Dependency Injection And Entity FrameWork 6.0 - CodeProject
}
dict.Add("error", errordetails);
return Request.CreateResponse(HttpStatusCode.BadRequest, dict);
if (res == true)
{
var showmessage = "Product Saved Successfully.";
dict.Add("Message", showmessage);
return Request.CreateResponse(HttpStatusCode.OK, dict);
}
else
{
var showmessage = "Product Not Saved Please try again.";
dict.Add("Message", showmessage);
return Request.CreateResponse(HttpStatusCode.BadRequest, dict);
}
}
[Route("showproduct")]
[HttpGet]
}
else
{
var showmessage = "No product found.";
dict.Add("Message", showmessage);
return Request.CreateResponse(HttpStatusCode.OK, details);
}
}
so now our project is ready lets check using PostMan client.I have shown you only for create a new product,similarly if anyone want can check for all operation.
Here in the above context anyone can call the api can consume the data exposed by api and may be there is a chance of hack the sensitive information.So we have to protect our data and
only give permission to our authenticated user.So to implement this we need to implement Token Based Authentication here.
https://www.codeproject.com/Articles/1192403/Architecting-Web-API-Project-With-Repository-Layer 9/16
27/7/2017 Architecting Web API Project With Repository Layer,Dependency Injection And Entity FrameWork 6.0 - CodeProject
In Web API 2.0 onword if you see the token based authentication has alresdy implement and we need to utilise in a correct manner. So give security to our application we have to do
following steps.
Create some authenticate user for this application.(This process can be possible by create users in identity DB)
Before each request verify the user if it is authenticate user then only give the resources.(This process can be done by successfully signin and getting Token.)
So the Authntication process works like this.
So here finally we need a valid Token which is generated by successfully loggedin to the system.
To work on all these we have Identity concept.It provide database also.Please note also the code found in Account Control is a part of that.Here i am showing the Registration Part only.
if (!result.Succeeded)
{
return GetErrorResult(result);
}
return Ok();
}
And Model Related to this is found in Model Folder of "AccountBinding Model".Here is the model code.
[Required]
https://www.codeproject.com/Articles/1192403/Architecting-Web-API-Project-With-Repository-Layer 10/16
27/7/2017 Architecting Web API Project With Repository Layer,Dependency Injection And Entity FrameWork 6.0 - CodeProject
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
Note that it has a seperate DBcontext called "ApplicationDbContext" which is found in IdentityModel.cs.
namespace MyProductAPI.Models
{
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit https://go.microsoft.com/fwlink/?
LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager, string authenticationType)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
// Add custom user claims here
return userIdentity;
}
}
//
NOTE:-For both the DBContext the Connection name is "DefaultConnection". As a result of this it will create the identity in Temp database.
So the identity related all tables are created in Temp Database and this user will be saved there.
Now if you check the ASPNETUsers then you will find the User.
https://www.codeproject.com/Articles/1192403/Architecting-Web-API-Project-With-Repository-Layer 11/16
27/7/2017 Architecting Web API Project With Repository Layer,Dependency Injection And Entity FrameWork 6.0 - CodeProject
Now we need to check the authenticity of a user and if it is a valid user we will grant our resources.so for this we need to call the SignIn api.
Here in Startup.auth.cs the SignIn Endpoint is exposed.
namespace MyProductAPI
{
public partial class Startup
{
public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
}
}
}
So here you can change the endPoint as per your routing.Now it will poing like
/token.
https://www.codeproject.com/Articles/1192403/Architecting-Web-API-Project-With-Repository-Layer 12/16
27/7/2017 Architecting Web API Project With Repository Layer,Dependency Injection And Entity FrameWork 6.0 - CodeProject
As this is a valid user so the token is generated.Now User the following attribute in the api method which you want to protact.In my show Product i have Used Authorize attribute so
unauthincated user can able to access this.lets try to access this without any access token.
}
else
{
var showmessage = "No product found.";
dict.Add("Message", showmessage);
return Request.CreateResponse(HttpStatusCode.OK, details);
So here we seen that if an API is marked as an Authorized apI,It cannot accessable by unknown or unauthorized User,It can only be accessed by authorized user.
Now to check this pass the token in API Header and try to access the GET API.
https://www.codeproject.com/Articles/1192403/Architecting-Web-API-Project-With-Repository-Layer 13/16
27/7/2017 Architecting Web API Project With Repository Layer,Dependency Injection And Entity FrameWork 6.0 - CodeProject
NOTE: While passing token in Header we need to append the Token with Bearer as shown above.
Points of Interest
So in this way we can create a web api project and secure our API methods.If you have any doubt on this please comment or mail me in my mail Id.
In my next article i will show how to work on Password Change,Sending mail for conforming account.Hope you will like this article.
History
1.0.0.0
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
Share
EMAIL TWITTER
INDIAN|PROGRAMMER|BLOGGER
Achievements:
https://www.codeproject.com/Articles/1192403/Architecting-Web-API-Project-With-Repository-Layer 14/16
27/7/2017 Architecting Web API Project With Repository Layer,Dependency Injection And Entity FrameWork 6.0 - CodeProject
* C# Cornor MVP-2015
* C# Cornor MVP-2016
* C# Cornor July 2016 Winner
* C# Cornor Feb 2016 Winner
* C# Cornor JulY 2015 Winner
* C# Cornor JUNE 2015 Winner
* 17th JULY 2017 - Article of the Day - ASP.NET Community (Creating Your Own Validation Attribute In
MVC And Web API 2.0).
* 28th June 2017 - Article of the Day - ASP.NET Community (Exploring State Management In
WebService).
* 31th may 2017 - Article of the Day - ASP.NET Community (Creating Shopping Cart Application From
Scratch In MVC - Part Two).
* 21th may 2017 - Article of the Day - ASP.NET Community (Displaying Image In WebGrid And Changing
Background Color Of Webgrid Row On Mouse Hover).
* 12th may 2017 - Article of the Day - ASP.NET Community (Tips And Tricks To Improve WEB API
Performance).
* 3rd may 2017 - Article of the Day - ASP.NET Community (Asynchronous Communications Using RabbitMQ
Message Broker).
* 27th April 2017 - Article of the Day - ASP.NET Community (Push Notification To Android Mobile
Using GCM (Google Cloud Messaging)).
* 23rd April 2017 - Article of the Day - ASP.NET Community (Working with Lookup In LINQ).
* Sept 2016-Featured article for the september 2016 in c# cornor(When To Use Abstract Class and
Interface In Real Projects).
* May 2017-Featured article for the May 2017in c# cornor (Important Tips To Write Clean Code In
C#)
*22nd July 2017-Won 1st price for best WEB Dev article in Code Project(Architecting Web API Project With Repository Layer,Dependency Injection And Entity FrameWork 6.0)
* July 20, 2017-Article of the Day - ASP.NET Community (Model Class Validation Testing Using Nunit)
CRUD Operations Using the Generic Repository Pattern and Dependency Window Tabs (WndTabs) Add-In for DevStudio
Injection in MVC
Search Comments Go
Great writing.
Member 13295990 5-Jul-17 22:46
Excellent Article
Member 13290814 3-Jul-17 8:45
This is the best WEB API article i have read still now.
Great piece of writing with detailed explanation.
I am voting 5/5.
https://www.codeproject.com/Articles/1192403/Architecting-Web-API-Project-With-Repository-Layer 15/16
27/7/2017 Architecting Web API Project With Repository Layer,Dependency Injection And Entity FrameWork 6.0 - CodeProject
Hi sir i will update the article and upload the Source code.
Refresh 1
General News Suggestion Question Bug Answer Joke Praise Rant Admin
Permalink | Advertise | Privacy | Terms of Use | Mobile Layout: fixed | fluid Article Copyright 2017 by Debendra0256
Seleccionar idioma ▼
Web02 | 2.8.170713.1 | Last Updated 24 Jun 2017 Everything else Copyright © CodeProject, 1999-2017
https://www.codeproject.com/Articles/1192403/Architecting-Web-API-Project-With-Repository-Layer 16/16