13Jun

Web API 2 With Repository pattern

Web API 2 With Repository pattern

How to create Repository pattern in web API 2.

Overview of Repository Pattern

RepositoryChartFlow

The repository pattern is intended to create an abstraction layer between the data access layer and the business logic layer of an application. It is a data access pattern that prompts a more loosely coupled approach to data access. We create the data access logic in a separate class, or set of classes called a repository with the responsibility of persisting the application's business model.

Using the Repository Pattern has many advantages:

1. Your business logic can be unit tested without data access logic.

2. The database access code can be reused

3. Your database access code is centrally managed so easy to implement any database

     access policies, like caching

4. It’s easy to implement domain logics.

5. Your domain entities or business entities are strongly typed with annotations; and more.

 

Steps to create repository pattern in web API 2.

Here I am going to show a small example how to retrieve and insert data using webApi2.

In this project, I have created 3 separate folders.

Services, ViewModel, Repository.

Folders

Create View Model Classes

    public class EmpViewModel

    {

        public int ID { get; set; }

        public string Name { get; set; }

        public string Address { get; set; }

        public string Phone { get; set; }

    }

Create Generic Interface.

    public interface IGenericDetails

    {

        IEnumerable GetAll();

        IEnumerable GetByID(int id);

        bool Insert(EmpViewModel dt);

        bool Update(EmpViewModel dt, int id);

        bool Delete(EmpViewModel dt, int id);

     }

Create API Controller

Controller1 

Controller2

 

Create Repository

    public class EmployeeDetails : IGenericDetails

    {

        ApiDBEntities db = new ApiDBEntities();

        public IEnumerable GetAll()

        {

            var items = db.Tbl_DetailsRepository.Select(i =>

                       new EmpViewModel { ID = i.ID, Name = i.Name, Address = i.Address,

                                         Phone = i.Phone });

            return items.ToList();

        }

        public IEnumerable GetByID(int id)

        {

            var result = db.Tbl_DetailsRepository.Where(i => i.ID == id).Select(i =>

                         new EmpViewModel {ID = i.ID, Name = i.Name, Address = i.Address,

                                   Phone = i.Phone});

            return result.ToList();

        }

        public bool Insert(EmpViewModel dt)

        {

            Tbl_DetailsRepository dr = new Tbl_DetailsRepository();

            dr.Name = dt.Name;

            dr.Address = dt.Address;

            dr.Phone = dt.Phone;

            db.Tbl_DetailsRepository.Add(dr);

            var Result = db.SaveChanges();

            if (Result == 1)

            {

                return true;

            }

            else

            {

                return false;

            }

        }

 

Conclusion :-

reason to use the repository pattern is to allow the separation of your business logic and/or your UI from System.Data.Entity. There are numerous advantages to this, including real benefits in unit testing.

                   as we know every 2 years new ORM will be launch in the market. So if we use/follow this pattern it will be very easy to switch to any ORM by changing only the connection.

Related

Building a RESTful API using python and Django

What you Need? Python 2.6+ Django 1.5+ django-tastypie  Sqllite3Django is Free and Open Source ...

Read More >

How to Customize the Microsoft Dynamics NAV 2013 R2 Splash screen Image

I think I got late with this post, but I was unaware that most of us still don’t know how to c...

Read More >

Continuous Integration/ Continuous Deployment VSTS

Following the below steps you can build and deploy your ASP.NET  app to Azure from either Visua...

Read More >

What is DevOps and why Enterprises are serious about it?

The term DevOps is becoming increasingly familiar as more and more companies are interested in imple...

Read More >

Sending Test reports by Email using Office 365, Gmail

Wouldn’t it be great if Test Report are sent automatically across team as soon the Test Execut...

Read More >

Quality is Our Mantra – Successful Completion of the Surveillance Audit

Canarys has successfully completed the Surveillance Audit and has been certified for ISO 9001:2008. ...

Read More >

VSTS Area Uploader

VSTS Area Uploader helps you to upload Areas (with multiple level) to your VSTS Team Projects from a...

Read More >

T.F.S Webinars Registration

h2{ margin-bottom:10px;}#wrapper{ margin: -40px auto 0; }#wrapper h1{ color:#FFF; text-align:center...

Read More >

SSRS(Sql Server Reporting Services) Reports

What is SSRS reports?SSRS stands for SQL Server Reporting services a server based report generation ...

Read More >

Microsoft Dynamics NAV 2013 R2 Chart Generator

In general chart means visual display of information, this is one such feature that is introduced in...

Read More >

Share

Post a Comment

  • Recent
  • Popular
  • Tag
Tags
Monthly Archive
Subscribe
Name

Contact Us
  • *
  • *