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.