Null Base UnitOfWork - EntityFramework With Repository Pattern - UserManager

北战南征 提交于 2019-12-10 21:48:30

问题


I am creating simple web api/ SPA application using EntityFramework, IUnitOfWork, Repository pattern, Unity DI along with Asp.net Identity.

1: Unity configuration

public static void RegisterTypes(IUnityContainer container)
        {
            // TODO: Register your types here
            container.RegisterType<DataContext>(new PerResolveLifetimeManager());
            container.RegisterType<IUnitOfWork, UnitOfWork>(new PerResolveLifetimeManager());
            container.RegisterType<IUserStore<User, Guid>, UserRepository>(new HierarchicalLifetimeManager());
            container.RegisterType<IUserRepository, UserRepository>(new HierarchicalLifetimeManager());
            container.RegisterType<IRoleStore<Role, Guid>, RoleRepository>(new HierarchicalLifetimeManager());
            container.RegisterType<IUserManager, ApplicationUserManager>(new HierarchicalLifetimeManager());
            container.RegisterType<IRoleManager, ApplicationRoleManager>(new HierarchicalLifetimeManager());
            container.RegisterType<IUserService, UserService>(new HierarchicalLifetimeManager());
        }

2: UnitOfWork

public class UnitOfWork : IUnitOfWork
    {
        protected DataContext _context = null;

        public UnitOfWork(DataContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("Context argument cannot be null in UnitOfWork.");
            }

        this._context = context;
    }

    public void SaveChanges()
    {
        this._context.SaveChanges();
    }

    public async Task SaveChangesAsync()
    {
        await this._context.SaveChangesAsync();
    }

    public void Dispose()
    {
        if (this._context != null)
        {
            this._context.Dispose();
        }
    }
}

3: GenericRepository

public abstract class GenericRepository<T> : IGenericRepository<T>, IDisposable
      where T : class 
    {
        protected DataContext _context;
        protected readonly IDbSet<T> _dbset;

        public GenericRepository(DataContext context)
        {
            if (context == null)
            {
                throw new ApplicationException("DbContext cannot be null.");
            }

            _context = context;
            _dbset = context.Set<T>();
        }
//Other code ignored

}

4:UserRepository - implements IUserStore

public class UserRepository: GenericRepository<User>,IUserRepository
        {
            public UserRepository(DataContext context)
                : base(context)
            {

                if (context == null)
                    throw new ArgumentNullException("DbContext cannot be null.");

                this._context = context;
            }

//Other Methods ignored for bravity

}

5: ApplicationUserManager

public class ApplicationUserManager : UserManager<User, Guid>, IUserManager
    {
        public ApplicationUserManager(IUserRepository userRepository)
            : base(userRepository)
        {
            this.UserValidator = new UserValidator<User, Guid>(this)
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail = true
            };
            // Configure validation logic for passwords
            this.PasswordValidator = new PasswordValidator
            {
                RequiredLength = 6,
                RequireNonLetterOrDigit = true,
                RequireDigit = true,
                RequireLowercase = true,
                RequireUppercase = true,
            };

        }
    }

6:BaseService

public class BaseService
    {
        IUnitOfWork _unitOfWork = null;
        public BaseService(IUnitOfWork unitOfWork)
        {
            if (unitOfWork == null)
            {
                throw new ArgumentNullException("DbContext cannot be null.");
            }
        }

        public void Dispose()
        {
            if (this._unitOfWork != null)
            {
                this._unitOfWork.Dispose();
                this._unitOfWork = null;
            }
        }
    }

7: UserService

public class UserService : BaseService,IUserService
    {
        private IUserManager _userManager;
        private IUnitOfWork _unitOfWork;

        public UserService(IUserManager userManager, IUnitOfWork unitOfWork) 
            : base(unitOfWork)
        {
            _userManager = userManager;
            _unitOfWork = unitOfWork;

        }


        public async Task<IdentityResult> RegisterUser(CreateUserBindingModel usr)
        {
            var user = new User();

            user.Email = usr.Email;
            user.UserName = usr.Email;



       IdentityResult result = await _userManager.CreateAsync(user,usr.Password);
            _unitOfWork.SaveChanges();

            return result;
        }

    }

8: Finally APi UserController with Register method.

namespace Angular.Api.Controllers
{
    [RoutePrefix("api/user")]
    public class UserController : ApiController
    {
        private IUserService _userService;


        public UserController()
        {

        }
        public UserController(IUserService userService)
        {
            _userService = userService;

        }


        [Route("Register")]
        public IHttpActionResult Register(CreateUserBindingModel usr)
        {



              _userService.RegisterUser(usr);



                return Ok();   



        }
    }
}

Question: When I call Api to register User it finishes without an error but no user added to database.

Debug Screenshot link https://www.dropbox.com/s/k3pnstqwnwdlux4/RegisterScreen.PNG?dl=0 as I could not post the image due to account restriction.

Debug screen shows base unit of work is null? I am not sure why. Please let me know what is wrong here, thanks.

来源:https://stackoverflow.com/questions/29195043/null-base-unitofwork-entityframework-with-repository-pattern-usermanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!