以下代码工作得很好,但我有一些问题与它的设计相关。
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<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;
}
}
答案 0 :(得分:1)