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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:56:42+00:00 2026-05-20T14:56:42+00:00

i’m very new to the repository pattern, but so far i like how i

  • 0

i’m very new to the repository pattern, but so far i like how i can use it. I found this implementation on codeplex. My questions now are:

am i using the repository pattern in the correct manner?

and

can anyone provide a better solution for my example beneath?

Here is an example (i’m using poco) where i have fetched a location with lazyloading turned off earlier in the process. After some ui interaction i want to update the location properties (eg. name) and the related users (including add and remove):


using (var repo = RepositoryHelper.GetLocationRepository())
using (var repo2 = RepositoryHelper.GetUserRepository(repo.UnitOfWork))
{
    repo.Add(location);
    repo.UnitOfWork.Context.ObjectStateManager.ChangeObjectState(location, EntityState.Modified);

    foreach (var user in location.Users)
    {
        repo2.Add(user);
        if (user.Id != 0)
        {
            repo2.UnitOfWork.Context.ObjectStateManager.ChangeObjectState(user, EntityState.Unchanged);
        }
    }

    foreach (var user in _remove.Where(user => user.Id != 0))
    {
        repo2.Delete(user);
    }

    repo2.UnitOfWork.Commit();
}

The example is working, although i’m very confused about how to the the Attach command. I thought it is used if the object comes from another context?! But if i try that, i always get an exception, that the object is in use by another context. And lets say, i’m detaching the location, i cannot access the Users collection right after that.

Another question (and i think it’s closly related to this): i’ve always read, that the database connection should be as short as possible. Therefor i added the IDisposable interface to the repositories, so i can use them in the same way as an ObjectContext. However i found some examples (i think also in the wpf application framework), where this is not the usual approach. So, what words should i follow?

Kind Regards,

matt

  • 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-20T14:56:43+00:00Added an answer on May 20, 2026 at 2:56 pm

    The key question: Why do you want to use repository and unit of work (UoW) patterns?

    People usually use these patterns to separate EF code to internal repository and UoW implementation. Because of that upper layers will be completely independent on EF.

    Your implementation doesn’t provide this separation. It is just a strange wrapper around ObjectContext and ObjectSet. In such case you don’t need repository and UoW at all and you can use EF classes directly. Even with EF classes you can still make your code testable (another wrong reason why people sometimes introduce repositories and UoW).

    To your second question. Lifetime of ObjectContext should be short as possible but it doesn’t mean that you should create new context for each query. ObjectContext handles connection internally and it opens it only when it is needed. The reason why ObjectContext should be used for short period of time is because it also implements UoW pattern and moreover IdentityMap patterns (I described implications here). In WPF application ObjectContext usually lives as long as the window or control presenting data for modification.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is

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.