在我的MVC应用程序中,我使用的是Rob Conery在MVC店面视频系列中展示的架构,它对我来说就像魅力一样。
库=>服务+过滤器=>控制器=>查看
我试图模仿要达到什么和管理待办事项这样
EDIT1:在库中改变的IList IQueryable的到和过滤器
库
public interface IRepository
{
IQueryable<Vote> GetVotes();
IQueryable<Story> GetStories();
}
服务为得到你想要的
public class Service : IService
{
private IRepository _repository;
public Service(IRepository repository)
{
_repository = repository;
if (_repository == null) throw new InvalidOperationException("Repository cannot be null");
}
public IList<Vote> GetUserVotes(int userID)
{
return _repository.GetVotes().WithUserID(userID).ToList();
}
public IList<Story> GetNotVotedStories(IList<Vote> votes)
{
return _repository.GetStories().WithoutVotes(votes).ToList();
}
}
过滤器来过滤你的故事和用户投票(这些基本上是扩展方法)。不是最好的实施摆在那里,但你以后可以改写
public static class Filters
{
public static IQueryable<Vote> WithUserID(this IQueryable <Vote> qry, int userID)
{
return from c in qry
where c.UserID == userID
select c;
}
public static IQueryable<Story> WithoutVotes(this IQueryable <Story> qry, IList <Vote> votes)
{
return from c in qry
where votes.Where(x => x.StoryID == c.StoryID).ToList().Count > 0
select c;
}
}
然后你就可以在控制器通过当前用户名,而不是在DAL或视图像你不得不这样做之前
public class HomeController : Controller
{
private readonly IRepository _repository;
private readonly IService _service;
public HomeController()
{
_repository = new Repository();
_service = new Service.Service(_repository);
}
public ActionResult Index()
{
var userVotes = _service.GetUserVotes(CurrentUserID);
var unvotedStories = _service.GetNotVotedStories(userVotes);
return View(unvotedStories);
}
}
这可以让你以避免添加用户相关UserVotedOn属性到您的故事模型
那么,当我说DAL技术上是一个BLL。它包装了datacontext。当我说ViewData我的意思是我有ViewData类,我传递到强类型的意见。感谢您的输入,我会把它浸入其中。 – 2009-02-09 18:41:01
我自己一直在试图想出“完美”的MVC架构。经过几个月的试验后,我可以说完美的架构和理想的关注分离是不可能的(谁会怀疑)。你只需要认同你自己并做出一些妥协。 – User 2009-02-09 18:49:26