我正在我的一个基于 ASP.NET MVC4 和 N 层架构的项目中实现存储库模式。对于如何使用存储库模式实现自定义成员资格,我有点困惑。到目前为止,这是我实现课程的方式。
//通用存储库
public interface IRepository<T> where T : class
{
void Add(T entity);
void Delete(T entity);
void Update(T entity);
IQueryable<T> GetAll();
T FindBy(Expression<Func<T, bool>> expression);
IQueryable<T> FilterBy(Expression<Func<T, bool>> expression);
}
//存储库基类
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);
}
}
我不太确定我走对了吗?如果是,我如何实现我的 UserService 类。
如果不是,我需要实施哪些更改。
对此的任何帮助都是非常可观的。在此先感谢*强文本*