gpt4 book ai didi

asp.net-mvc - 脂肪 Controller : How do i make it slim?

转载 作者:行者123 更新时间:2023-12-02 01:42:57 24 4
gpt4 key购买 nike

我正在使用 EF 6 和 MVC 5 开发博客引擎。

我决定不使用 Repository 模式或 UoW,因为它已经在框架级别的 EF 6 中实现。

解决方案包含以下层。

DataModels 层:它具有自动生成的简单 POCO 和 dbContext。

public partial class Article
{
public int Id { get; set; }
public string Slug { get; set; }
public string Title { get; set; }
public string PostBody { get; set; }
public System.DateTime CreatedOn { get; set; }
public bool IsPublished { get; set; }
public string Author { get; set; }
}

服务层:

public interface IBlogEngine
{
List<Article> GetFrontPageBlogPosts();
void SaveArticle(Article article);
List<Article> GetArticlesByStatus(string isPublished);
Article GetBySlug(string slug);
Article GetById(int id);
bool Exists(string slugUrl);
void Delete(int id);
}

IBlogEngine 实现。为简洁起见,省略了一些方法实现。

public class BlogEngine : IBlogEngine
{
private readonly dbContext _context;

public BlogEngine(DbContext context)
{
_context = context;
}


public void SaveArticle(Article article)
{
if (article.Id == 0)
{
_context.Articles.Add(article);
}
else
{
_context.Entry(article).State = EntityState.Modified;
}

_context.SaveChanges();
}



public Article GetBySlug(string slug)
{
return _context.Articles.SingleOrDefault(x => x.Slug == slug.Trim());
}


}

界面层

 public class ArticleController : Controller
{
private readonly IBlogEngine _engine;
public ArticleController(IBlogEngine engine)
{
_engine = engine;
}

[HttpGet]
public ActionResult Edit(string slug)
{
if (string.IsNullOrWhiteSpace(slug))
{
return HttpNotFound();
}

var article = _engine.GetBySlug(slug);

if (article == null)
{
return HttpNotFound();
}

var model = new EditViewModel { Id = article.Id, Slug = article.Slug,
Title = article.Title, PostBody = article.PostBody, IsPublished = true };

return View("Create", model);
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(EditViewModel blogPost)
{
if (!ModelState.IsValid)
{
return View("Create", blogPost);
}
// Get Article by Id
var article = _engine.GetById(blogPost.Id);

if (article == null)
{
return HttpNotFound();
}

// Update it
article.Id = blogPost.Id;
article.Title = blogPost.Title.Trim();
article.Slug = blogPost.Slug.ToUrlSlug();
article.PostBody = blogPost.PostBody;
article.CreatedOn = DateTime.UtcNow;
article.IsPublished = blogPost.IsPublished;
article.Author = User.Identity.Name;

// Save it
_engine.SaveArticle(article);

return RedirectToAction("Create", "Article");
}

}

问题考虑一个场景,用户完成编辑他的旧博客文章/文章并点击提交按钮来更新他的博客文章/文章。

我的 HTTP POST 编辑操作是否太胖了?我觉得 Controller 在这里做的事情太多了。

  1. 从数据库中获取现有文章

  2. 用 ViewModel 值更新它

  3. 从服务层调用SaveArticle方法。

我怎样才能让这个 Controller 节食?

服务层方法 SaveArticle 不应该执行从 Db 检索文章并使用新值更新它并调用 SaveChanges 方法的工作吗?

如果上述陈述是正确的,我如何将 ViewModel 传递给 ServiceLayer 方法?允许 ViewModels 泄漏到服务层不是一个错误的决定吗?

我该如何处理?我很困惑,需要一些帮助。

最佳答案

坦率地说,有时我也很困惑 Controller 应该做什么以及做多少来​​满足请求。

在我的大部分实现中,我会执行以下操作:

  1. 在 Post 方法中接受 viewModel 输入对象(输入值在客户端验证)。
  2. 检查模型状态。
  3. 将 View 模型对象转换为领域模型对象。我用 AutoMapper .
  4. 将其交给服务方法。它执行操作需要完成的操作。
  5. 根据操作返回合适的结果。

我是你,我会写:

    [HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(EditViewModel blogPost)
{
if (!ModelState.IsValid)
{
return View("Create", blogPost);
}

// Use AutoMapper for ViewModel to DomainModel conversion
var blogPostDomainModel = Mapper.Map<EditViewModel, BlogPost>(blogPost);

// Save it - Update the object in persistent store. It may throw
// exception if something wrong while updating the object. Having
// validated input from UI that should only happen due to server
// error.
_engine.SaveArticle(blogPostDomainModel);

return RedirectToAction("List", "Article");
}

关于asp.net-mvc - 脂肪 Controller : How do i make it slim?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27398267/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com