Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9206089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:06:07+00:00 2026-06-18T00:06:07+00:00

Interface public interface IDinnerRepository { IQueryable<Dinner> FindAllDinners(); IQueryable<Dinner> FindDinnersByText(string q); Dinner GetDinner(int id); void

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:06:09+00:00Added an answer on June 18, 2026 at 12:06 am

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Interface { public: static const int i = 1; static const double d
I have an interface public interface Foo<T> { public void bar(String s, T t);
I have the following interface: public interface ILogger { void Debug(string message, params object[]
I have the following interface : public interface IOuputDestination { void Write(String s); }
Hi have the following interface: public interface KeyValueStore { public void put(String key, byte[]
I have an interface class public interface AsyncTaskExecuteCommand { public Object executeCommand(String jsonLocation) throws
And i have one interface public interface IMethod { String Add(string str); Boolean Update(string
I have this interface: public interface ISectionListItem { public int getLayoutId(); public void setProps(View
Consider the following interface: public interface IMyCallback { void SomeEvent(int someArg); } which is
I have the following code: public class Interface { public void exec(){ try {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.