Generic Repository Pattern With Entity Framework & LINQ

Entity Framework

The Entity Framework helps developers to work with real data in the form of objects and properties without the underlying database tables and columns where the data is stored. With the Entity Framework, developers can work at a higher level of abstraction when they deal with data and can create/maintain data-oriented applications with less code than in traditional applications. Since Entity Framework is a component of the .NET Framework, Entity Framework applications can run on any computer with the .NET Framework starting with version 3.5 SP1 installed. 

EntityFramework-1.jpg

LINQ

LINQ is easier to transform data into objects. LINQ reduces the amount of work one must do to translate between object-oriented code and data paradigms such as hierarchical, flat-file, messages, relational, and more. It doesn't eliminate the "Impedance Mismatch" because one must still be reasonable about the data in its native form, and the bridge from here to there is (IMO) much shorter. LINQ also helps to reduce SQL injection attacks. 

Repository Pattern

The Repository Pattern helps to get more control over data. For example strongly typed business entities help to identify problems at compile time itself. This pattern helps with centrally managed, consistent access rules and logic. Another benefit is that when using the repository pattern you don't need to have any knowledge of the data source or data layer. The Repository Pattern is a layer between the Data Access Layer (DAL) and the Business Logic Layer (BLL).

EntityFramework-2.jpg
 

Implementations

Create objects User Info and Clients to manage the data:

    public class UserInfo

    {

        public string UserName { get; set; }

        public string Password { get; set; }

    }

 

    public class Clients

    {

        public int ClientID { get; set; }

        public int UserID { get; set; }

        public string clientName { get; set; }

        public bool mobileVersion { get; set; }

        public bool tabletVersion { get; set; }

    }

Create a generic interface for the repository class to handle any objects:

using System.Collections.Generic;

using mvc_model.Models.Objects;

 

namespace mvc_model.Models

{

    //used this method so we can use any object with this interface

    interface UserInterFace<T> where T:class

    {

        IEnumerable<T> GetUserObject(UserInfo userObject);

    }
 

    }

Note:
The method <T> where T:class will help to use any objects with the interface.

Create a DBContext for the database connection string.

using System.Data.Entity;

 

namespace mvc_model.Models.Dal

{

    public class MainContext : DbContext

    {

        //specify about the connection string in web config

 

        public MainContext() : base("DefaultConnection") { }

    } 
Create Repository Class to manage the data.
 

using System.Collections.Generic;

using System.Linq;

using mvc_model.Models.Objects;

 

namespace mvc_model.Models.Dal.Repository

{

   //implementing Iterator Pattern

    public class UserRepository: UserInterFace<Clients>

    {

        public MainContext mainContext = new MainContext(); 

        public IEnumerable<Clients> GetUserObject(UserInfo userObject)
         {
               object[] parameters = { userObject.UserName, userObject.Password }; 
                         //used stored procedures with paramerts to retrive data from Entity Framework                        

        }

    }

}

Note: mainContext.Database.SqlQuery will help to use the Stored Procedure with entity parameters, and will return a client object. The object[] parameters will help you to pass parameters to the Stored Procedure.

Create a function to return the client object.

public Clients GetName(UserInfo userObject)
{

   //in repository class used Iterator Pattern
 var user = from m in userRepository.GetUserObject(userObject)

  select m;
  var clientInfo = user.FirstOrDefault()
return client

Note: in the final section LINQ is used to retrieve data from the repository layer. FirstOrDefault() will help to avoid bugs if the returned data is null.

Up Next
    Ebook Download
    View all
    Learn
    View all