gpt4 book ai didi

asp.net-mvc-3 - N 层服务层验证显示表示层中的业务逻辑错误

转载 作者:行者123 更新时间:2023-12-03 12:18:24 24 4
gpt4 key购买 nike

我正在从 ASP.NET Web 窗体的旧方法转换为 ASP.NET MVC。我有一个正在处理的项目,数据库中有大约 40-50 个表。我决定使用 Entity Framework 作为我的数据访问层。我还决定在 EF 上放置一个存储库层和工作单元抽象,这样我就不会被它束缚,这样我就可以进行单元测试。最后,我想让我的 Controller “瘦”,所以我正在考虑为我的业务逻辑实现一个业务“服务”层。

我正在努力解决的问题是如何将业务逻辑错误从我的服务层传播到我的 Presentation UI 层,以便显示适当的错误?请注意,我正在尝试寻找一种非 MVC 特定的解决方案,因为此服务/业务逻辑层可能会用于 MVC 应用程序(控制台应用程序、Web 服务等)以外的其他事物

关于一些代码...

假设我有一个像这样的 POCO/数据/域模型:

public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public bool IsActive { get; set; }

// other properties (navigation, etc)...
}

像这样的 Entity Framework 流畅配置/映射类:

public class CategoryMap : EntityTypeConfiguration<Category>
{
public CategoryMap()
{
this.HasKey(c => c.Id);
this.Property(c => c.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); // auto increment identity in our DB schema
this.Property(c=> c.Name)
.IsRequired() // defined as NOT NULL in DB schema so we put a constraint here
.HasMaxLength(150); // defined as varchar(150) in DB schema so we put a constraint here
this.Property(c=> c.Description)
.IsRequired(); // defined as NOT NULL in DB schema so we put a constraint here

// fluent config for related entities (navigation properties) would go here...
}
}

像这样封装多个存储库的工作单元:

public class UnitOfWork : IUnitOfWork
{
private readonly MyDbContext context;
private CategoryRepository catRepo;

public UnitOfWork()
{
this.context = new MyDbContext();
}

public ICategoryRepository Categories
{
get { return this.catRepo?? (this.catRepo= new CategoryRepository (this.context)); }
}
}

像这样的服务/业务逻辑层:

public class CategoryService : ICategoryService
{
private readonly IUnitOfWork unitOfWork;
public CategoryService(IUnitOfWork uow) // injected by IoC
{
this.unitOfWork = uow;
}

public Category CreateNewCategory(Category category)
{
if (category == null)
{
throw new ArgumentNullException("category cannot be null");
}

// Simple business logic here to make sure another category with this name does not already exist.
int count = this.unitOfWork.Categories.Count(cat => cat.Name == category.Name);
if (count > 0)
{
// *** This is the error I want the user to see in the UI ***
throw new Exception("Sorry - a category with that name already exists!");
}
}
}

还有这样的 Controller :

public ManageCategoriesController : Controller
{
ICategoryService catSvc;
public ManageCategoriesController(ICategoryService svc) // injected by IoC
{
this.catSvc = svc;
}


[HttpPost]
public ActionResult(CategoryCreateModel createModel) // my View Models / Create Models have Data Annotations on them
{
if (ModelState.IsValid)
{
// use of AutoMapper to map from View Model to domain model...
Category cat = Mapper.Map<CategoryCreateModel , Category>(createModel);
this.catSvc.CreateNewCategory(cat); // ***need to get potential errors from Service and display on form.***
return this.RedirectToAction("Index");
}
}
}

首先,任何人都可以告诉我使用 View 模型是否正确吗?我觉得每个域模型几乎都有三个 View 模型(创建、编辑、查看/列表)。

其次,我的 EF 配置/映射类负责数据库约束。其中一些约束(例如最大长度)也是 View 模型中的数据注释,可以轻松显示在 UI 上。但是我可以在哪里显示我的自定义业务逻辑错误?

最佳答案

首先,您对 MVC 的总体方法对我来说看起来不错 :-)

其次,您很可能希望在 View 模型上使用 DataAnnotation 来进行模型验证。看看this blog post以获得有关在 ASP.MVC 中使用它的良好介绍。

如果自定义验证不适用于数据注释,您可以在 Controller 中执行以下操作:

try
{
// the following exception could be thown by some nested validation logic
// e.g. while processing a post request
throw new ValidationException("the error description");
}
catch (ValidationException exception)
{
ModelState.AddModelError("", exception.Message);
}

关于asp.net-mvc-3 - N 层服务层验证显示表示层中的业务逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18081446/

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