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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:47:24+00:00 2026-05-27T12:47:24+00:00

I am learning EF4.1 in conjunction with MVVM and in one of these tutorials

  • 0

I am learning EF4.1 in conjunction with MVVM and in one of these tutorials they create a MainWindowViewModel with a Repository object that they use in calling another view model (EmployeeListViewModel). Here is the code:

public MainWindowViewModel()
{
    IObjectContextAdapter adapter = ((IObjectContextAdapter)new SidekickEntities());
    _vmRepository = new GenericRepository(adapter.ObjectContext);
    EmployeeListViewModel viewModel = new EmployeeListViewModel(_vmRepository);
    this.ViewModels.Add(viewModel);
}

public EmployeeListViewModel(GenericRepository repository)
{

    if (repository == null)
    {
        throw new ArgumentNullException("repository");
    }

    _employeeRepository = repository;
    this.AllEmployees = new ObservableCollection<Employee>(_employeeRepository.GetAll<Employee>());
}

What I am wondering is, why is the repository created in the MainWindowViewModel and then passed into EmployeeListViewModel? Why not just create the repository in EmployeeListViewModel like this:

public MainWindowViewModel()
{
    EmployeeListViewModel viewModel = new EmployeeListViewModel();
    this.ViewModels.Add(viewModel);
}

public EmployeeListViewModel()
{
    IObjectContextAdapter adapter = ((IObjectContextAdapter)new SidekickEntities());
    _employeeRepository = new GenericRepository(adapter.ObjectContext);
    this.AllEmployees = new ObservableCollection<Employee>(_employeeRepository.GetAll<Employee>());
}

I am pretty new to EF but it seems a little cleaner to create a repository in each separate ViewModel. Wouldn’t the repository then be cleaned up when the ViewModel in question is no longer used instead of keeping it around until the closing of MainWindowViewModel or does this create too many repository instances?

  • 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-05-27T12:47:25+00:00Added an answer on May 27, 2026 at 12:47 pm

    The problem you will run into when using a separate Context for each ViewModel is that entities that have links to each other come from different contexts so you will have to manually work this out when trying to update something (see Attaching and Detaching Objects)

    This has to do with a pattern called Unit Of Work.

    UoW helps you with encapsulating functions that should work together. In your case the ObjectContext implements the UoW pattern and is passed to the repository in the constructor.

    You should share your ObjectContext between functions that work together and should be seen as one Unit Of Work.

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

Sidebar

Related Questions

When learning C# for the first time, I was astonished that they had no
I learning Perl and I want to create a simple application that gets all
Currently we have 2 databases that we are going to use. One comes from
I have been learning MVVM/EF4 (for C#) recently and have followed Julie Lerman's videos.
Learning about Explicit Cursors and trying to create my frst one.: SET SERVEROUTPUT ON
I'm learning EF4.1 against the northwind database. I have created a POCO class like
Learning a lot in my few years of programming that the best projects are
i'm learning EF4 and would like someone to recommend the best practice on how
Learning Ruby. Needed to create a hash of arrays. This works... but I don't
Learning spine.js I completed both the tutorials no problem, seems like a great framework,

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.