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 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. As 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.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

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


Repository.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 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 DBContext for specify 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

return mainContext.Database.SqlQuery<Clients>("ClientsUserInfo_Selectall {0},{1}", parameters.ToArray());

 

        }

    }

}

Note:  mainContext.Database.SqlQuery will help to use the stored procedure with entity parameters, and will return client object. object[] parameters   will help you to pass parameters to stored procedure.

Create function to return 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 clientInfo;

        }

 

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

Ebook Download
View all
Learn
View all