Microsoft practice using the Unity register type

I have several scenarios where it is difficult for me to match types.

Scenario 1:

 public AccountController()
            : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
        {
        }

        public AccountController(UserManager<ApplicationUser> userManager)
        {
            UserManager = userManager;
        }

Type definition

 public class ApplicationUser : IdentityUser
    {
    }

 // Summary:
    //     Implements IUserStore using EntityFramework where TUser is the entity type
    //     of the user being stored
    //
    // Type parameters:
    //   TUser:
    public class UserStore<TUser> : IUserLoginStore<TUser>, IUserClaimStore<TUser>, IUserRoleStore<TUser>, IUserPasswordStore<TUser>, IUserSecurityStampStore<TUser>, IUserStore<TUser>, IDisposable where TUser : global::Microsoft.AspNet.Identity.EntityFramework.IdentityUser
    {
 ....
  }

public class UserManager<TUser> : IDisposable where TUser : global::Microsoft.AspNet.Identity.IUser
    {
....
  }

Matching Attempt

  container.RegisterType(typeof(IUserStore<>), typeof(ApplicationUser));

            container.RegisterType(typeof(IDisposable), typeof(UserManager<>));

Error: it is not possible to merge an object of type "Main.Models.ApplicationUser" to enter "Microsoft.AspNet.Identity.IUserStore 1[Main.Models.ApplicationUser]'. With out mapping, i get this error: The current type, Microsoft.AspNet.Identity.IUserStore1 [Main.Models.ApplicationUser], is an interface and cannot be constructed, are you missing a type mapping?

Scenario 2:

 public RegisterController(
             IUserDataStorage<HandyGuy> session)
        {
            this.handySession = session;
        }

Type definition

 public class HttpUserDataStorage<T> : IUserDataStorage<T>
  where T : class
    {
        public T Access
        {
            get { return HttpContext.Current.Session[typeof(T).FullName] as T; }
            set { HttpContext.Current.Session[typeof(T).FullName] = value; }
        }
    }

Matching Attempt:

 container.RegisterType(typeof(IUserDataStorage<>), typeof(HttpUserDataStorage<>));

Error: no errors. But I always have null in handySession.Access

+4
source share
2 answers

emodendroket, Entity Framework.

.

 container.RegisterType(typeof(UserManager<>),
            new InjectionConstructor(typeof(IUserStore<>)));
            container.RegisterType<Microsoft.AspNet.Identity.IUser>(new InjectionFactory(c => c.Resolve<Microsoft.AspNet.Identity.IUser>()));
            container.RegisterType(typeof(IUserStore<>), typeof(UserStore<>));
            container.RegisterType<IdentityUser, ApplicationUser>(new ContainerControlledLifetimeManager());
            container.RegisterType<DbContext, ApplicationDbContext>(new ContainerControlledLifetimeManager());

 container.RegisterType<UserManager<ApplicationUser>>(new HierarchicalLifetimeManager());
            container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager());
            container.RegisterType<DbContext, ApplicationDbContext>(new HierarchicalLifetimeManager());
+11

, IOC MVC5

  • asp.net mvc 5 ( )
  • use unity = > install-package unity.mvc
  • WIF ( Windows)
+2

Source: https://habr.com/ru/post/1541627/


All Articles