Note that there are some explanatory texts on larger screens.

plurals
  1. POShould repositories be implemented as singletons as best practice?
    primarykey
    data
    text
    <p>I have a small webapp that uses <code>EntityFramework</code> to store stuff via repositories into the database.</p> <p>What I've done so far (based on all the tutorials I read) is create a repository where I need it, as shown below:</p> <p>In <code>CustomMembershipProvider</code>:</p> <pre><code> public CustomMembershipProvider() { _userRepository = new UserRepository(new TenantApplicationContext()); } </code></pre> <p>In my <code>RegisterController</code>:</p> <pre><code> public TenantRepository TenantRepository { get; set; } public UserRepository UserRepository { get; set; } protected override void Initialize(RequestContext requestContext) { if (MembershipService == null) { MembershipService = new AccountMembershipService(); } if (TenantRepository == null) { TenantRepository = new TenantRepository(TenantApplicationContext); } if (UserRepository == null) { UserRepository = new UserRepository(TenantApplicationContext); } base.Initialize(requestContext); } </code></pre> <p>The point is, that I instantiate the <code>UserRepository</code> twice. This becomes a problem when I create a <code>User</code> in one instance, and try to retrieve it in the other instance, <strong>and I did not call</strong> <code>SaveChanges</code> in between.</p> <p>The problem lies here:</p> <pre><code>// Snippet from the register controller class if (!UserRepository.Exists(model.AccountableEmailAddress)) { // 1 - Create the user via a custom MembershipProvider // Note, the CustomMembershipProvider has it's own instance of UserRepository var createStatus = MembershipService.CreateUser( model.AccountableUser, model.Password, model.AccountableEmailAddress); if (createStatus == MembershipCreateStatus.Success) { // Left out irrelevant code AdministerUserAndTenant(tenant.Name, model.AccountableEmailAddress); } } private void AdministerUserAndTenant(string tenantName, string emailAddress) { // 2 - Try to retrieve the user from a different (!) instance of UserRepository var user = UserRepository.GetUser(emailAddress); var tenant = TenantRepository.GetTenantByName(tenantName); tenant.Users.Add(user); TenantApplicationContext.SaveChanges(); } </code></pre> <p>I hope you can still follow, tried to leave out unnecessary parts. </p> <p>What is the best way to deal with issues like this? </p> <p>PS: I'm not very fond of the <code>Singleton</code> pattern, so if possible don't go there :).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload