服务存储库模式设计考虑

时间:2012-08-21 15:05:11

标签: asp.net-mvc-3 design-patterns repository-pattern

以下代码工作得很好,但我有一些问题与它的设计相关。

BlogEntry.cs

    public class BlogEntry : EntityBase
    {
       /// <summary>
       /// Gets or sets the blog entry comments.
       /// </summary>
       public virtual ICollection<BlogEntryComment> BlogEntryComments { get; set; }
    }

BlogEntryComment.cs

public class BlogEntryComment : EntityBase//, IValidatableObject
{
    /// <summary>
    /// Gets or sets the comment.
    /// </summary>
    [StringLength(2500)]
    [Required(ErrorMessageResourceName = "Comment", ErrorMessageResourceType = typeof(Validation))]
    [AllowHtml]
    public string Comment { get; set; }

    /// <summary>
    /// Gets or sets the blog entry id.
    /// </summary>
    public Guid BlogEntryId { get; set; }

    /// <summary>
    /// Gets or sets the blog entry.
    /// </summary>
    public virtual BlogEntry BlogEntry { get; set; }

    /// <summary>
    /// Gets or sets the user id.
    /// </summary>
    public Guid UserId { get; set; }

    /// <summary>
    /// Gets or sets the author.
    /// </summary>
    public virtual User User { get; set; }
}

BlogController.cs

    [HttpPost]
    public virtual ActionResult PostComment(Guid id, BlogEntryComment blogEntryComment)
    {
        var blogEntry = this.BlogEntryService.GetById(id);

        if (blogEntry == null)
        {
            if (Request.IsAjaxRequest())
                return Json(new { success = false, message = "Blog entry not found" });

            return new HttpNotFoundWithViewResult("NotFound");
        }

        var user = UserService.GetByUsername(User.Identity.Name);

        if (user == null)
        {
            if (Request.IsAjaxRequest())
                return Json(new { success = false, message = "Unknown user!" });

            return new HttpUnauthorizedResult();
        }

        if (!ModelState.IsValid)
        {
            var errorModel = new BlogEntryDetail()
            {
                BlogEntry = blogEntry,
                HideNewCommentsForm = false
            };

            if (this.Request.IsAjaxRequest())
            {
                return PartialView(MVC.Blog.Views._CommentsControl, errorModel);
            }
            else
            {
                errorModel.RelatedBlogEntries = this.BlogEntryService.GetRelatedBlogEntries(false, blogEntry, 3).ToArray();
                return View(errorModel);
            }
        }

        blogEntryComment.User = user;
        blogEntryComment.BlogEntry = blogEntry;

        this.BlogEntryCommentService.Add(blogEntryComment);

        var model = new BlogEntryDetail()
        {
            BlogEntry = blogEntry,
            HideNewCommentsForm = true
        };

        if (this.Request.IsAjaxRequest())
        {
            return PartialView(MVC.Blog.Views._CommentsControl, model);
        }
        else
        {
            model.RelatedBlogEntries = this.BlogEntryService.GetRelatedBlogEntries(false, blogEntry, 3).ToArray();
            return View(model);
        }
    }

BlogEntryService.cs

public class BlogEntryService : GenericEntityService<BlogEntry>, IBlogEntryService
{
    /// <summary>
    /// Initializes a new instance of the <see cref="BlogEntryService"/> class.
    /// </summary>
    /// <param name="unitOfWork">The <see cref="IUnitOfWork"/>.</param>
    public BlogEntryService(IUnitOfWork unitOfWork)
        : base(unitOfWork.BlogEntries, unitOfWork)
    {
    }

GenericEntityService.cs

public abstract class GenericEntityService<T> : IGenericEntityService<T> where T : MVCBlog.Core.Entities.EntityBase
{
    /// <summary>
    /// Initializes a new instance of the <see cref="GenericEntityService&lt;T&gt;"/> class.
    /// </summary>
    /// <param name="repository">The <see cref="MVCBlog.Core.Repository.IRepository{T}"/>.</param>
    /// <param name="unitOfWork">The <see cref="IUnitOfWork"/>.</param>
    protected GenericEntityService(IRepository<T> repository, IUnitOfWork unitOfWork)
    {
        this.Repository = repository;
        this.UnitOfWork = unitOfWork;
    }

}

  • 评论应该使用BlogEntryService.AddComment(..)的方法添加到数据库中,还是像现在的实现一样使用自己的BlogEntryCommentService.Add(..)?
  • 我正在验证控制器中的User和BlogEntry,此验证应该是服务层的一部分吗?例如[Service] .AddComment(Guid blogEntryId,string username,string comment)
  • 改善设计或代码的任何其他想法?

1 个答案:

答案 0 :(得分:1)

  1. 根据SRP(Single Response Principal),它应该是BlogEntryCommentService.Add(..)。
  2. 我想,这不是服务层验证。