lundi 7 septembre 2015

How repository design pattern help us from various perspective ASP.Net MVC

i was reading a article on repository design pattern from this url http://ift.tt/1P0rnl2

here see the code for repository design pattern

public interface ICityRepository
    {

        void Add(City b);

        void Edit(City b);

        void Remove(string Id);

        IEnumerable<City> GetCity();

        City FindById(int Id);

    }


public class CityRepository : ICityRepository

    {

        CityDataContext context = new CityDataContext();

        public void Add(Core.Entities.City b)

        {

            context.Cities.Add(b);

            context.SaveChanges();



        }



        public void Edit(Core.Entities.City b)

        {

            context.Entry(b).State = System.Data.Entity.EntityState.Modified;

        }



        public void Remove(string Id)

        {

            City b = context.Cities.Find(Id);

            context.Cities.Remove(b);

            context.SaveChanges();

        }



        public IEnumerable<Core.Entities.City> GetCity()

        {

            return context.Cities;

        }



        public Core.Entities.City FindById(int Id)

        {

            var c = (from r in context.Cities where r.Id == Id select r).FirstOrDefault();

            return c;

        }

    }

see the code and tell me the repository design pattern restrict us to access any city related data. see the interface code first

there are only few function called

void Add(City b);
void Edit(City b);
void Remove(string Id);
IEnumerable<City> GetCity();
City FindById(int Id);

there is FindByID but i may have requirement like FindBy phone no or find by name etc then again i need to add more code in interface and also in class which extend the interface. suppose i design repository class and later i may have any requirement come to fetch city by name or by zip code etc then again i need to change code in repository.

i just feel this repository pattern isolate business class from data access. before people design class like customer and customer class directly call data access layer code to do the CRUD operation but with repository pattern there another layer comes which wont allow customer or any domain class to access data access layer class....that it but other than that how it is helping us to code faster.

anyone would mind to discuss how to design repository patter code in such a way only one time which can solve all our data access problem or issue. looking for best advise. thanks

Aucun commentaire:

Enregistrer un commentaire