0

I am building a WPF application (storage system), which would be running for a very long time and therefore I am interested in what option should I go with when using Entity Framework with Repository Patterns.

What I did, is that I have separated my Data Access Layer (DAL) into a separate class library referenced by the main project. For the technologies, I have decided to use Entity Framework 6 & SQLite database (plus SQLite plugins for EF).

In the main project, except for other things (such as Prism) I am also using Unity for IoC while working with my ViewModels.

This is how I am registering my repository under Bootstrapper.cs class:

Container.RegisterType<IUserRepository, UserRepository>(new TransientLifetimeManager());

The reason why I have decided to use TransientLifetimeManager was to make sure, that this class is re-created every time ViewModel is created and thus ensuring that new data is being fetched from the database.

Here is a (oversimplified) repository with only two methods:

public interface IUserRepository
{
    UserLevel TryLoginUser(User user);
    string RegisterNewUser(User user);
    void Dispose();
}

With sample implementation:

public class UserRepository : IUserRepository
{
    public enum UserLevel { Regular, Supervisor, Admin, Invalid };

    private readonly DataContext _context;
    public UserRepository()
    {
        _context = new DataContext();
    }

    public UserLevel TryLoginUser(User user)
    {
        return UserLevel.Invalid;
    }

    public string RegisterNewUser(User user)
    {
        return null;
    }
}

What users will be doing is, that they will be switching between 2 (or three) main windows thorough the lifetime of an application. In order to make sure, that the database data from Entity Framework is in sync with the database, what I have decided to do is to initialize new DataContext in the constructor of the method itself (which reloads the data).

When I have explored the memory usage while navigating back and forth between my windows (and calling various repositories), I have noticed that the memory usage starts to go up, which means that, clearly, there is something wrong with my method of initializing and injecting Repositories.

Finally, in regards to this topic, I have also tried to somehow register my DataContext.cs (which is very simple and just inherits from DbContext) so that it could be injectable into the repositories; however I had even problems registering it (as it lacks interface definition, Unity didn't allow me to register it).

I would highly appreciate any help or recommendations in this matter.

Robert J.
  • 2,631
  • 8
  • 32
  • 59
  • you have registered implementation for IUserRepository: `RegisterType`. Client classes don't need to know that UserRepository implementation works with `DataContext`. So why register it anywhere? – ASh Sep 16 '17 at 14:55
  • also: `public enum UserLevel` is a nested class of UserRepository. interface IUserRepository uses that enum. IUserRepository and UserRepository are coupled (every time projects needs IUserRepository, it also must reference UserRepository) – ASh Sep 16 '17 at 14:57
  • So you are saying that my current method of creating new `DataContext` within repositories is correct? So where can I have a memory leak then? – Robert J. Sep 16 '17 at 15:08
  • DbContext implements IDisposable. I suppose UserRepository should do it as well and dispose its resources. – ASh Sep 16 '17 at 15:13
  • https://stackoverflow.com/questions/33041113/c-sharp-entity-framework-correct-use-of-dbcontext-class-inside-your-repository – ASh Sep 16 '17 at 15:14

0 Answers0