Note that there are some explanatory texts on larger screens.

plurals
  1. POService repository pattern design considerations
    text
    copied!<p>The following code is working just fine but I have a few questions realated to it's design.</p> <p>BlogEntry.cs</p> <pre><code> public class BlogEntry : EntityBase { /// &lt;summary&gt; /// Gets or sets the blog entry comments. /// &lt;/summary&gt; public virtual ICollection&lt;BlogEntryComment&gt; BlogEntryComments { get; set; } } </code></pre> <p>BlogEntryComment.cs</p> <pre><code>public class BlogEntryComment : EntityBase//, IValidatableObject { /// &lt;summary&gt; /// Gets or sets the comment. /// &lt;/summary&gt; [StringLength(2500)] [Required(ErrorMessageResourceName = "Comment", ErrorMessageResourceType = typeof(Validation))] [AllowHtml] public string Comment { get; set; } /// &lt;summary&gt; /// Gets or sets the blog entry id. /// &lt;/summary&gt; public Guid BlogEntryId { get; set; } /// &lt;summary&gt; /// Gets or sets the blog entry. /// &lt;/summary&gt; public virtual BlogEntry BlogEntry { get; set; } /// &lt;summary&gt; /// Gets or sets the user id. /// &lt;/summary&gt; public Guid UserId { get; set; } /// &lt;summary&gt; /// Gets or sets the author. /// &lt;/summary&gt; public virtual User User { get; set; } } </code></pre> <p>BlogController.cs</p> <pre><code> [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); } } </code></pre> <p>BlogEntryService.cs</p> <pre><code>public class BlogEntryService : GenericEntityService&lt;BlogEntry&gt;, IBlogEntryService { /// &lt;summary&gt; /// Initializes a new instance of the &lt;see cref="BlogEntryService"/&gt; class. /// &lt;/summary&gt; /// &lt;param name="unitOfWork"&gt;The &lt;see cref="IUnitOfWork"/&gt;.&lt;/param&gt; public BlogEntryService(IUnitOfWork unitOfWork) : base(unitOfWork.BlogEntries, unitOfWork) { } </code></pre> <p>GenericEntityService.cs</p> <pre><code>public abstract class GenericEntityService&lt;T&gt; : IGenericEntityService&lt;T&gt; where T : MVCBlog.Core.Entities.EntityBase { /// &lt;summary&gt; /// Initializes a new instance of the &lt;see cref="GenericEntityService&amp;lt;T&amp;gt;"/&gt; class. /// &lt;/summary&gt; /// &lt;param name="repository"&gt;The &lt;see cref="MVCBlog.Core.Repository.IRepository{T}"/&gt;.&lt;/param&gt; /// &lt;param name="unitOfWork"&gt;The &lt;see cref="IUnitOfWork"/&gt;.&lt;/param&gt; protected GenericEntityService(IRepository&lt;T&gt; repository, IUnitOfWork unitOfWork) { this.Repository = repository; this.UnitOfWork = unitOfWork; } </code></pre> <p>}</p> <ul> <li>The comment should be added to the database with a method of BlogEntryService.AddComment(..) or with it's own BlogEntryCommentService.Add(..) as with current implementation?</li> <li>I'm validating User and BlogEntry in the controller, this validation should be part of the service layer? for example [Service].AddComment(Guid blogEntryId, string username, string comment)</li> <li>Any other idea to improve the desing or code?</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload