Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Desing patterns with using EntityFramework?

Interface

public interface IDinnerRepository 
{
    IQueryable<Dinner> FindAllDinners();
    IQueryable<Dinner> FindDinnersByText(string q);

    Dinner GetDinner(int id);

    void Add(Dinner dinner);
    void Delete(Dinner dinner);

    void Save();
}

Class that is inherited from above interface

public class DinnerRepository : NerdDinner.Models.IDinnerRepository
{
    NerdDinnerEntities db = new NerdDinnerEntities();

    Public IQueryable<Dinner> FindDinnersByText(string q)
    {
         return db.Dinners.Where(d => d.Title.Contains(q)
             || d.Description.Contains(q)
             || d.HostedBy.Contains(q));
    }

    public IQueryable<Dinner> FindAllDinners()
    {
         return db.Dinners;
    }

    public Dinner GetDinner(int id)
    {
        return db.Dinners.SingleOrDefault(d => d.DinnerID == id);
    }

    public void Add(Dinner dinner)
    {
        db.Dinners.AddObject(dinner);
    }

    public void Delete(Dinner dinner)
    {
        foreach (RSVP rsvp in dinner.RSVPs.ToList())
            db.RSVPs.DeleteObject(rsvp);

        db.Dinners.DeleteObject(dinner);
   }

   public void Save()
   {
        db.SaveChanges();
   }
}

Usage in a program

public class DinnerOperation
{
    DinnerRepository dr = new DinnerRepository();

    // insert
    public void InsertDinner()
    {
        Dinner dinner = dr.GetDinner(5);
        dr.Dinner.Add(dinner);
        dr.Save();
    }

    // delete
    public void DeleteDinner()
    {
        Dinner dinner = dr.GetDinner(5);
        dr.Dinner.Delete(dinner);
        dr.Save();
    }
}

And without using repository design pattern...

public class DinnerOperation
{
    DinnerEntities entity = new DinnerEntities();

    // insert
    public void InsertDinner()
    {
        Dinner dinner = entity.Dinners.Find(5);
        entity.Dinner.Add(dinner);
        entity.SaveChanges();
    }

    // delete
    public void DeleteDinner()
    {
        Dinner dinner = entity.Dinners.Find(5);
        entity.Dinner.Remove(dinner);
        entity.SaveChanges();
    }
}

Question

I cant understand, in here, Why did we use design pattern? When using the Repository design pattern with Entity Framework in this way does not mean anything.

How can I use design pattern with entitiy framework? When does it make sense to use design pattern with entity framework?

like image 425
AliRıza Adıyahşi Avatar asked Jan 29 '13 09:01

AliRıza Adıyahşi


People also ask

Is Entity Framework a design pattern?

Factory Method, an Entity Framework ObjectContext provides a CreateObjectSet<T> method, which creates an ObjectSet<T> for the given type. Since those object-sets are the main approach to access the entities within a context, I would say it is also a very important pattern used in EF.

What design patterns are used throughout the .NET framework?

The . NET Framework uses the IEnumerable and IEnumerator interfaces to implement the Iterator pattern.

Does Entity Framework implement repository pattern?

Entity Framework already implements a repository pattern. DbContext is your UoW (Unit of Work) and each DbSet is the repository. Implementing another layer on top of this is not only redundant, but makes maintenance harder. People follow patterns without realizing the purpose of the pattern.

What design pattern does .NET core use?

The MVC (Model-View-Controller) design pattern is a design pattern that's actually been around for a few decades, and it's been used across many different technologies, everything from Smalltalk to C++ to Java and now in C# and .


1 Answers

You almost got it. First, refactor your dinner operation class so that the repository implementation could be injected into it.

public class DinnerOperation
{
  private IDinnerRepository dr;
  public DinnerOperation( IDinnerRespository repository ) {
     this.dr = repository;
  }

  // insert
  public void InsertDinner()
  {
      Dinner dinner = dr.GetDinner(5);
      dr.Dinner.Add(dinner);
      dr.Save();
  }

  // delete
  public void DeleteDinner()
  {
      Dinner dinner = dr.GetDinner(5);
      dr.Dinner.Delete(dinner);
      dr.Save();
  }
}

Then implement different repositories:

public class EntityFrameworkDinnerRepository : IDinnerRepository
public class NHibernateDinnerRepository : IDinnerRepository
public class Linq2SqlDinnerRepository : IDinnerRepository
public class MockDinnerRepository : IDinnerRepository
....

and then use any repository you want:

var repository = new ....Repository();
var operation = new DinnerOperation( repository );

operation.GetDinner(5);

Repositories are used to abstract your concrete data providers and thus making your architecture more felible.

Want to switch to nHibernate?

Painful, if you have EntityFramework everywhere. Easy, if you use repositories, you just inject another repository and your business logic doesn't have to change.

Want to unit test your business logic?

Painful, if you are stick to a concrete data provider. Easy, if you have repositories, you just inject an inmemory repository which doesn't even use the database.

Got it now?

like image 95
Wiktor Zychla Avatar answered Oct 11 '22 05:10

Wiktor Zychla