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 8962107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:03:34+00:00 2026-06-15T16:03:34+00:00

Today I would like to implement repository pattern + unit of work pattern for

  • 0

Today I would like to implement repository pattern + unit of work pattern for database connection. Is my code bellow correct?? Because this is my first implementation unit of work for database connection.

First repository:

public class NotesRepository : INotesRepository
{
    private DatabaseContext context;

    public NotesRepository(DatabaseContext context)
    {
        this.context = context;
    }

    public IQueryable<Notes> GetAllNotes()
    {
        return (from x in context.Notes
                select x);
    }
}

Second repository:

public class CommentsRepository : ICommentsRepository
{
    private DatabaseContext context;

    public CommentsRepository(DatabaseContext context)
    {
        this.context = context;
    }

    public IQueryable<Comments> GetAllComments()
    {
        return (from x in context.Comments
                select x);
    }
}

Unit of work class for database connection:

public class UnitOfWork
{
    private DatabaseContext context = new DatabaseContext();
    private INotesRepository notesRepository;
    private ICommentsRepository commentsRepository;

    public INotesRepository NotesRepository
    {
        get
        {
            if (this.notesRepository == null)
            {
                this.notesRepository = new NotesRepository(context);
            }
            return notesRepository;
        }
    }

    public ICommentsRepository CommentsRepository
    {
        get
        {
            if (this.commentsRepository == null)
            {
                this.commentsRepository = new CommentsRepository(context);
            }
            return commentsRepository;
        }
    }
}

And controller in which I can use many repositories with single database connection:

public class HomeController : Controller
    {
        private UnitOfWork unitOfWork = new UnitOfWork();        

        public ViewResult Index()
        {
            var a = unitOfWork.NotesRepository.GetAllNotes();
            var b = unitOfWork.CommentsRepository.GetAllComments();

            return View();
        }
    }
  • 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-15T16:03:35+00:00Added an answer on June 15, 2026 at 4:03 pm

    Your implementation is very right 🙂

    But I advice you to use IUnitOfWork interface and pass the DatabaseContext instance in the constructor.

    Another thing to do is to make a SaveChanges method in the UnitOfWork to commit the data to the database.

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

Sidebar

Related Questions

This is something I would like to see while doing my day today programming
greetings, today i have started to learn java using netbeans ide. i would like
I joined this site today hoping someone would be kind enough to explain to
I was working on some code earlier today, when I realized, Hey! This code
I would like to know this to understand why some games like Mario is
I ran into this today when unit testing a generic dictionary. System.Collections.Generic.Dictionary<int, string> actual,
Today I would like to ask what does the => SomeType mean. I found
Today I found LiquiBase project and I think it is very interesting. I would
Today, I went for an interview and the interviewer asked me how I would
Today at work, I was given a task to fix a bug within our

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.