【问题标题】:Need help in implementing Repository Pattern with Custom Membership ASP.NET MVC在使用自定义成员资格 ASP.NET MVC 实施存储库模式方面需要帮助
【发布时间】:2012-10-29 10:17:59
【问题描述】:

我正在我的一个基于 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 类。

如果不是,我需要进行哪些更改。

对此的任何帮助都是非常可观的。提前致谢*强烈的文字*

【问题讨论】:

标签: asp.net-mvc-3 dependency-injection repository-pattern custom-membershipprovider


【解决方案1】:

您的IService&lt;T&gt; 看起来非常像您的IRepository&lt;T&gt; 界面。对我来说,这看起来像是一个完全无用的抽象。消费者可以直接使用IRepository&lt;T&gt; 抽象。如果您的意图是向IService&lt;T&gt; 实现添加功能(例如横切关注点);请改用decorators

此外,为什么有一个空的(非通用)IUserRepository?直接使用IRepository&lt;User&gt; 是没有用的。如果您打算稍后在此 IUserRepository 中添加额外的方法,请不要这样做。这个接口,以及这个接口的实现,将开始变得太大,变得难以维护,并且变得难以扩展你的代码库,因为你将违反Single Responsibility Principle。相反,为自定义用户特定操作提供自己的类(每个操作一个类),由通用接口隐藏,就像您对 IRepository&lt;T&gt; 所做的那样。

以下两篇文章描述了一种通过定义更小的重点类来创建更易于维护的系统的有吸引力的方法,每个类都封装一个查询或用例,这些类可以放置在您的存储库模式之上:

最后一点。您的 UserService 类看起来很像 .NET 框架中的 MembershipProvider 类。为什么不使用那个类?有一个very interesting library 允许您create a membership provider for MVC in a more structured, dependency-friendly way。你一定要看看。

【讨论】:

  • 是的,你完全正确。计划是在 BLL 层中使用装饰器。随着我继续进一步开发并审查相同的架构,架构也在同步发展。
  • 嗨 Steven,我是 ASP.NET MVC 的新手,想实现自己的登录,不想使用内置成员资格。我特此附上我的用户服务。您能否通过提供使用我的 UserService 类实现登录的源代码来帮助我。不需要整个项目。只需要登录视图和控制器。
  • 除非您想做一些非常奇特的事情,否则您应该使用由 ASP.NET 提供的Membership 抽象。但是,您需要编写自己的 MembershipProvider 实现(在该抽象操作之上)。通过这样做,您可以连接到 ASP.NET 的成员基础结构,这会带来很多好处。此外,您正在使用其他开发人员熟悉的模型。
  • 嗨,Steven,我根据您的建议对架构进行了更改。这对我帮助很大。我面临的问题是我能够从数据库中获取数据,但是当我尝试使用 unitofwork 类保存实体时,它不会向 DB 发起任何查询。可能通过 datacontext 不再开放。如果我向您提供源代码,您能帮我确定我做错了吗?对此的帮助是非常可观的。谢谢
  • @pbhalchandra:在 stackoverflow 上创建一个新问题,并在您这样做时发布该问题的链接。这使您可以从更多人那里获得帮助,而不仅仅是我。
猜你喜欢
  • 2010-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 2011-01-17
  • 2017-11-28
  • 2011-02-13
相关资源
最近更新 更多