Asp.net mvc 3 需要帮助以自定义成员身份ASP.NET MVC实现存储库模式吗
我正在一个基于ASP.NET MVC4和N层体系结构的项目中实现存储库模式。对于如何使用存储库模式实现自定义成员身份,我有点困惑。以下是到目前为止我是如何实现我的类的 //通用存储库Asp.net mvc 3 需要帮助以自定义成员身份ASP.NET MVC实现存储库模式吗,asp.net-mvc-3,dependency-injection,repository-pattern,custom-membershipprovider,Asp.net Mvc 3,Dependency Injection,Repository Pattern,Custom Membershipprovider,我正在一个基于ASP.NET MVC4和N层体系结构的项目中实现存储库模式。对于如何使用存储库模式实现自定义成员身份,我有点困惑。以下是到目前为止我是如何实现我的类的 //通用存储库 公共接口i假设,其中T:class { 无效添加(T实体); 无效删除(T实体); 无效更新(T实体); IQueryable GetAll(); T FindBy(表达式); 可计量过滤器(表达式); } //存储库基类 公共抽象类存储库:IRepository,其中T:class { 私人标准文本(u标准文本
公共接口i假设,其中T:class
{
无效添加(T实体);
无效删除(T实体);
无效更新(T实体);
IQueryable GetAll();
T FindBy(表达式);
可计量过滤器(表达式);
}
//存储库基类
公共抽象类存储库:IRepository,其中T:class
{
私人标准文本(u标准文本);;
专用只读IDbSet _dbSet;
受保护的存储库(IDatabaseFactory databaseFactory)
{
DatabaseFactory=DatabaseFactory;
_dbSet=StnDataContext.Set();
}
受保护的IDatabaseFactory数据库工厂{get;private set;}
公共标准文本标准文本标准文本
{
获取{return}
}
公共无效添加(T实体)
{
_添加(实体);
_stndacontext.Commit();
}
公共作废删除(T实体)
{
_删除(实体);
}
公共无效更新(T实体)
{
_附加数据集(实体);
_stnDataContext.Entry(entity).State=EntityState.Modified;
_stndacontext.Commit();
}
公共IQueryable GetAll()
{
返回_dbSet.ToList().AsQueryable();
}
公共T FindBy(表达式)
{
返回FilterBy(表达式).SingleOrDefault();
}
公共可查询筛选器by(表达式)
{
返回GetAll().Where(表达式).AsQueryable();
}
//用户存储库界面
公共接口IUserRepository:IRepository
{
}
//用户存储库
public class UserRepository : Repository<User>, IUserRepository
{
public UserRepository(IDatabaseFactory databaseFactory)
: base(databaseFactory)
{
}
}
public类UserRepository:Repository,IUserRepository
{
公共用户存储库(IDatabaseFactory databaseFactory)
:base(数据库工厂)
{
}
}
//这是我的业务逻辑层
//通用服务接口
公共接口iSeries设备
{
无效添加(T实体);
无效删除(T实体);
无效更新(T实体);
IEnumerable GetAll();
T FindBy(表达式);
IEnumerable FilterBy(表达式);
}
//服务基础
公共类服务:iSeries,其中T:class
{
公共无效添加(T实体)
{
抛出新的NotImplementedException();
}
公共作废删除(T实体)
{
抛出新的NotImplementedException();
}
公共无效更新(T实体)
{
抛出新的NotImplementedException();
}
公共IEnumerable GetAll()
{
抛出新的NotImplementedException();
}
公共T FindBy(表达式)
{
抛出新的NotImplementedException();
}
公共IEnumerable筛选器by(表达式)
{
抛出新的NotImplementedException();
}
}
//用户服务界面
公共接口IUserService:iSeries设备
{
}
//用户服务实施
公共类UserService:Service,IUserService
{
专用只读IUserRepository\u userRepository;
私有只读IRoleRepository;
公共用户服务(IUserRepository userRepository、IRoleRepository roleRepository)
{
_userRepository=userRepository;
_roleRepository=roleRepository;
}
公共IList GetAllUsers()
{
返回_userRepository.GetAll().ToList();
}
公共用户GetUser(int-id)
{
返回_userRepository.FindBy(x=>x.UserId==id);
}
公共用户GetUser(字符串用户名)
{
返回_userRepository.FindBy(x=>x.Username.Equals(Username));
}
公共用户(用户)
{
_添加(用户);
}
公共IList GetUsersErrore(字符串roleName)
{
return _userRepository.FilterBy(x=>x.Role.RoleName.Equals(RoleName)).ToList();
}
公共IList getUsersErrore(int roleId)
{
返回_userRepository.FilterBy(x=>x.Role.RoleId==RoleId.ToList();
}
公共IList GetUsersErrorle(角色)
{
返回getUsersError(role.RoleId);
}
}
我不太确定我走的路是否正确?如果是,我如何实现我的UserService类
如果否,我需要实施哪些更改
这方面的任何帮助都是非常值得赞赏的。提前感谢*strong text*您的iSeries设备
看起来非常像您的IRepository
界面。对我来说,这看起来像是一个完全无用的抽象。消费者可以直接使用IRepository
抽象。如果您的意图是添加功能(例如横切关注点)到iSeries设备
实现;改用
此外,为什么有一个空的(非泛型)IUserRepository
?直接使用IRepository
是没有用的。如果您以后打算向这个IUserRepository
添加额外的方法,请不要这样做。这个接口和这个接口的实现将开始变得太大,很难维护,并且很难扩展您的部分代码库,因为您将违反。相反,为自定义特定于用户的操作提供它们自己的类(每个操作一个类),由一个通用接口隐藏,就像您对IRepository
所做的那样
以下两篇文章描述了一种创建更易于维护的系统的有吸引力的方法,即定义较小的集中类,每个集中类封装一个查询或用例,这些查询或用例可以放在存储库模式的顶部:
最后一点。你的
UserService
类看起来很像.NET framework中的MembershipProvider
类。为什么不使用该类呢?有一个允许你使用的类。你一定要看一看。这可能是肯定的,你是绝对正确的。计划是在BLL层中使用装饰器。archit随着我继续深入开发,体系结构也在不断发展。嗨,Steven,我是ASP.NET MVC的新手,希望实现我自己的登录,不想使用内置的
public abstract class Repository<T> : IRepository<T> where T : class
{
private STNDataContext _stnDataContext;
private readonly IDbSet<T> _dbSet;
protected Repository(IDatabaseFactory databaseFactory)
{
DatabaseFactory = databaseFactory;
_dbSet = StnDataContext.Set<T>();
}
protected IDatabaseFactory DatabaseFactory { get; private set; }
public STNDataContext StnDataContext
{
get { return _stnDataContext ?? (_stnDataContext = new STNDataContext()); }
}
public void Add(T entity)
{
_dbSet.Add(entity);
_stnDataContext.Commit();
}
public void Delete(T entity)
{
_dbSet.Remove(entity);
}
public void Update(T entity)
{
_dbSet.Attach(entity);
_stnDataContext.Entry(entity).State = EntityState.Modified;
_stnDataContext.Commit();
}
public IQueryable<T> GetAll()
{
return _dbSet.ToList().AsQueryable();
}
public T FindBy(Expression<Func<T, bool>> expression)
{
return FilterBy(expression).SingleOrDefault();
}
public IQueryable<T> FilterBy(Expression<Func<T, bool>> expression)
{
return GetAll().Where(expression).AsQueryable();
}
public interface IUserRepository : IRepository<User>
{
}
public class UserRepository : Repository<User>, IUserRepository
{
public UserRepository(IDatabaseFactory databaseFactory)
: base(databaseFactory)
{
}
}
public interface IService<T>
{
void Add(T entity);
void Delete(T entity);
void Update(T entity);
IEnumerable<T> GetAll();
T FindBy(Expression<Func<T, bool>> expression);
IEnumerable<T> FilterBy(Expression<Func<T, bool>> expression);
}
public class Service<T> : IService<T> where T : class
{
public void Add(T entity)
{
throw new NotImplementedException();
}
public void Delete(T entity)
{
throw new NotImplementedException();
}
public void Update(T entity)
{
throw new NotImplementedException();
}
public IEnumerable<T> GetAll()
{
throw new NotImplementedException();
}
public T FindBy(Expression<Func<T, bool>> expression)
{
throw new NotImplementedException();
}
public IEnumerable<T> FilterBy(Expression<Func<T, bool>> expression)
{
throw new NotImplementedException();
}
}
public interface IUserService : IService<User>
{
}
public class UserService : Service<User>, IUserService
{
private readonly IUserRepository _userRepository;
private readonly IRoleRepository _roleRepository;
public UserService(IUserRepository userRepository, IRoleRepository roleRepository)
{
_userRepository = userRepository;
_roleRepository = roleRepository;
}
public IList<User> GetAllUsers()
{
return _userRepository.GetAll().ToList();
}
public User GetUser(int id)
{
return _userRepository.FindBy(x => x.UserId == id);
}
public User GetUser(string userName)
{
return _userRepository.FindBy(x => x.Username.Equals(userName));
}
public void CreatUser(User user)
{
_userRepository.Add(user);
}
public IList<User> GetUsersForRole(string roleName)
{
return _userRepository.FilterBy(x => x.Role.RoleName.Equals(roleName)).ToList<User>();
}
public IList<User> GetUsersForRole(int roleId)
{
return _userRepository.FilterBy(x => x.Role.RoleId == roleId).ToList<User>();
}
public IList<User> GetUsersForRole(Role role)
{
return GetUsersForRole(role.RoleId);
}
}