以下代码运行良好,但我有一些与它的设计相关的问题。
博客条目.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; }
}
博客控制器.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<T>"/> 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)
- 还有其他改进设计或代码的想法吗?