Null Base UnitOfWork - 具有存储库模式的EntityFramework - UserManager

时间:2015-03-22 13:37:54

标签: entity-framework asp.net-web-api repository-pattern

我正在使用EntityFramework,IUnitOfWork,Repository模式,Unity DI以及Asp.net Identity创建简单的web api / SPA应用程序。

1:Unity配置

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 - 实现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:最后使用Register方法的APi UserController。

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();   



        }
    }
}

问题:当我呼叫Api注册用户时,它完成没有错误,但没有用户添加到数据库。

调试屏幕截图链接https://www.dropbox.com/s/k3pnstqwnwdlux4/RegisterScreen.PNG?dl=0,因为帐户限制我无法发布图片。

调试屏幕显示基本工作单元是否为空?我不知道为什么。请告诉我这里有什么问题,谢谢。

0 个答案:

没有答案