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

Ladislav Mrnka
  • 349,807
  • 56
  • 643
  • 654
Matthias
  • 14,822
  • 5
  • 36
  • 78

1 Answers1

1

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.

Community
  • 1
  • 1
Ladislav Mrnka
  • 349,807
  • 56
  • 643
  • 654
  • I need a bit time to think about the first part. But regarding the second: that means, that it is okay to have one a context created within the constructor of a viewmodel (and also disposed upon disposing the viewmodel)? – Matthias Mar 10 '11 at 13:38
  • Are we talking about WPF? If so I can't answer it because I'm not WPF developer and I have never used WPF or MVVM. But in case of common WinForms combined with MVP (model-view-presenter) pattern the good practice is to have single context per presenter. – Ladislav Mrnka Mar 10 '11 at 13:40
  • To the first part: sorry, but i'm a bit confused. i want to underline, that it is the very first time for me using the repository pattern and so the code above is just a starting point for me. i think you mean the fact, that i'm using the state manager? except that, all samples i've found are using the repository in some kind of this way. – Matthias Mar 10 '11 at 13:43
  • Yes you are exposing ObjectContext and accessing it directly. The reason why people are using repositories and Uow is to avoid / hide these interactions with EF specific code. – Ladislav Mrnka Mar 10 '11 at 13:45
  • okay, but this was meant to be my key question: It's wrong to use the objectcontext. But how can i rewrite my code by just using common IRepository members? – Matthias Mar 10 '11 at 13:47
  • 1
    Do you see the column with related question on the right side? Almost every day we have a new question about how to implement repository and UoW with EF. Just go through few of those or try to use search: http://stackoverflow.com/search?q=entity+framework+repository If it doesn't help I will provide you some sample. – Ladislav Mrnka Mar 10 '11 at 13:56