abp(net core)+easyui+efcore实现仓储管理系统——展现层实现增删改查之控制器(六)...

本文详细介绍使用ABP框架结合EasyUI与EF Core构建仓储管理系统的过程。从实体创建到数据库操作,再到应用服务的实现,逐步讲解如何通过MVC方式完成增删改查功能。涵盖Controller、View、ViewModel的开发及调试技巧。

abp(net core)+easyui+efcore实现仓储管理系统目录

abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一)

abp(net core)+easyui+efcore实现仓储管理系统——解决方案介绍(二)

abp(net core)+easyui+efcore实现仓储管理系统——领域层创建实体(三)

 abp(net core)+easyui+efcore实现仓储管理系统——定义仓储并实现 (四)

abp(net core)+easyui+efcore实现仓储管理系统——创建应用服务(五)

 

       通过前面三篇文章的介绍,我们学习了如何创建实体,如何创建数据库操作,如何创建应用服务。在上一文章中我们在应用层实现了对数据库的CURD操作。在本篇文章中,主要是使用常规的MVC方式来实现增删改查的功能,通过完善Controller、View、ViewModel,以及调试修改控制器来实现展示层的增删改查。最终实现效果如下图:

 

一、创建ModuleController

      ABP对ASP.NET Net Core MVC  Controllers进行了集成,通过ABP网站创建的项目会自动创建一个Controller基类,这个Controller基类继承自AbpController, 我们即可使用ABP附加给我们的以下强大功能:

  • 本地化
  • 异常处理
  • 对返回的JsonResult进行包装
  • 审计日志
  • 权限认证([AbpMvcAuthorize]特性)
  • 工作单元(默认未开启,通过添加[UnitOfWork]开启)

      我们创建的ABP.TPLMS项目,也同样创建了一个控制器基类,具体位置如下图。

 

      1. 在Visual Studio 2017的“解决方案资源管理器”中,右键单击在领域层“ABP.TPLMS.Web.Mvc”项目中的Controller目录。 选择“添加” > “新建项…”。如下图。

 

    2. 在弹出对话框“添加新项-ABP.TPLMS.Web.Mvc”中选择“控制器类”,然后在名称输入框中输入“ModuleController”,然后点击“添加”按钮。如下图。

 

      3.在Visual Studio 2017中打开我们刚才创建ModuleController.cs,并继承自TPLMSControllerBase,并增加列表与修改方法。通过构造函数注入对应用服务的依赖。具体代码如下。

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.AspNetCore.Mvc.Authorization;
using Abp.Runtime.Validation;
using ABP.TPLMS.Controllers;
using ABP.TPLMS.Modules;
using ABP.TPLMS.Modules.Dto;
using ABP.TPLMS.Web.Models.Module;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; 

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 

namespace ABP.TPLMS.Web.Controllers
{

    [AbpMvcAuthorize]
    public class ModuleController : TPLMSControllerBase
    {

        // GET: /<controller>/
        public IActionResult Index()
        {

            var output = _moduleAppService.GetAllAsync();
            var model = new EditModuleModalViewModel
            {
                Module = output.Result.Items.First(),
                Modules = output.Result.Items
            };
            return View(model);
        }
   
            private readonly IModuleAppService _moduleAppService;      

            public ModuleController(IModuleAppService moduleAppService)
            {
            _moduleAppService = moduleAppService;          

            }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(CreateUpdateModuleDto updateDto)
        {
            _moduleAppService.CreateAsync(updateDto);
            var output = _moduleAppService.GetAllAsync();
            return PartialView("_List", output.Result);
        }

     public IActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [DisableValidation]
        public ActionResult Edit(int id,EditModuleModalViewModel updateDto)
        {
            if (id != updateDto.Module.Id)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                   var module= AutoMapper.Mapper.Map<CreateUpdateModuleDto>(updateDto.Module);

                    _moduleAppService.UpdateAsync(module);           

                }
                catch (DbUpdateConcurrencyException ex)
                {
                    if (!DtoExists(updateDto.Module.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw ex;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            return View(updateDto);          
        }

        private bool DtoExists(long id)
        {
            return _moduleAppService.GetAllAsync().Result.Items.Any(e => e.Id == id);
        }

        // GET: Module/Edit/5
        public IActionResult Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var module =  _moduleAppService.GetAllAsync().Result.Items.SingleOrDefault(m => m.Id == id);

            if (module == null)
            {
                return NotFound();
            }
            var model = new EditModuleModalViewModel
            {
                Module = module
            };
            return View(model);
            //return Ok(cargo.Result);
        }

  // GET: Module/Delete/5
        public  IActionResult Delete(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }
                var module = _moduleAppService.GetAllAsync().Result.Items.SingleOrDefault(m => m.Id == id);

            if (module == null)
            {
                return NotFound();
            }

            var model = new EditModuleModalViewModel
            {
                Module = AutoMapper.Mapper.Map<CreateUpdateModuleDto>(module)
            };

            return View(model);
        }

        // POST: Module/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            try
            {
                await _moduleAppService.DeleteAsync(id);
            }
            catch (Exception ex)
            {
                return View(ex.Message);
                //throw;
            }
          return RedirectToAction(nameof(Index));
        }
}
}

        

         

转载于:https://www.cnblogs.com/chillsrc/p/11096690.html

<think>嗯,用户问的是在.NET Core ABP框架中如何对聚合根子实体进行增删改查同时进行。首先,我需要回忆一下ABP中的领域驱动设计(DDD)概念,特别是聚合根(Aggregate Root)和子实体的关系。聚合根负责维护其子实体的完整性和事务一致性,所以所有对子实体的操作都应该通过聚合根来完成。 接下来,用户提到的是同时进行增删改查,这意味着在一个事务中处理多个操作。需要确保这些操作在同一个工作单元内执行,以保证数据的一致性。ABP的工作单元(Unit of Work)机制会自动管理事务,只要在应用层方法上添加[UnitOfWork]特性即可。 然后,我需要考虑具体的实现步骤。首先,聚合根和子实体的定义是否正确。比如,聚合根类中应该有子实体的集合,通常是使用泛型集合如`Collection<T>`来确保正确的EF Core映射。子实体可能需要一个组合主键,包含聚合根的ID和自身的ID,这需要在DbContext的OnModelCreating方法中配置。 然后是应用服务的实现。用户需要获取聚合根,对子实体进行添加、更新、删除操作,然后通过仓储保存聚合根。这里需要注意,ABP的仓储默认跟踪聚合根的变化,所以当修改聚合根的子实体集合时,EF Core会自动检测这些变更。但是,直接操作集合可能不够明确,可能需要显式调用仓储的Update方法,或者确保变更被正确跟踪。 可能遇到的问题包括:如何正确删除子实体,是否需要从集合中移除还是标记为删除。在EF Core中,如果子实体是聚合根的一部分,删除子实体通常是从集合中移除,然后保存聚合根,EF Core会自动处理删除操作。但需要确认是否正确配置了级联删除或必要的映射。 另外,用户可能关心的是性能问题,比如多次数据库操作是否合并为一个。使用工作单元后,所有操作会在事务提交时一次性提交,所以不会有多次数据库往返的问题。 还需要考虑并发处理,确保在并发修改时数据的一致性。ABP的乐观并发控制可以通过配置实体的Version属性来实现,但用户的问题中没有提到,可能暂时不需要深入,但可以作为一个注意事项提及。 最后,示例代码的结构需要清晰,展示如何在一个应用服务方法中获取聚合根,修改子实体集合,然后保存。同时,要提醒用户注意仓储的使用,确保在更新聚合根时调用Update方法,尽管某些情况下EF Core的变更跟踪可能自动处理,但显式调用会更可靠。 可能还需要提到领域服务的使用,如果操作复杂,涉及多个聚合根或业务逻辑,可能需要引入领域服务来封装逻辑,保持应用层的简洁。不过用户的问题主要是增删改查,所以应用服务直接处理应该足够。 总结步骤:定义聚合根和子实体,配置DbContext,实现应用服务方法,处理子实体的增删改,利用工作单元确保事务。同时,代码示例要准确,比如使用_collection.RemoveAll进行删除,使用Add方法添加新实体,修改现有实体的属性等。</think>在ABP框架中处理聚合根子实体的批量操作时,需严格遵循领域驱动设计原则。以下是具体实现步骤和示例: 一、领域层定义(关键结构) ```csharp // 聚合根 public class Order : AggregateRoot<Guid> { public string OrderNo { get; set; } // 使用Collection保证EF Core正确映射 public ICollection<OrderItem> Items => _items; private readonly List<OrderItem> _items; } // 子实体(需组合主键) public class OrderItem : Entity<Guid> { public Guid OrderId { get; } // 聚合根ID public string ProductName { get; set; } public int Quantity { get; set; } // 必须包含无参构造函数 private OrderItem() { } internal OrderItem(Guid id, Guid orderId, string productName, int quantity) { Id = id; OrderId = orderId; ProductName = productName; Quantity = quantity; } } ``` 二、DbContext配置 ```csharp protected override void OnModelCreating(ModelBuilder modelBuilder) { // 配置组合主键 modelBuilder.Entity<OrderItem>(b => { b.HasKey(x => new { x.OrderId, x.Id }); b.HasOne<Order>().WithMany(x => x.Items).HasForeignKey(x => x.OrderId); }); } ``` 三、应用服务实现(核心逻辑) ```csharp public class OrderAppService : ApplicationService { private readonly IRepository<Order, Guid> _orderRepository; public async Task UpdateOrderItems(UpdateOrderItemsDto input) { // 获取聚合根(包含子项) var order = await _orderRepository .Include(o => o.Items) .FirstOrDefaultAsync(o => o.Id == input.OrderId); // 删除操作 order._items.RemoveAll(item => input.DeletedItems.Contains(item.Id)); // 修改现有项 foreach (var itemDto in input.UpdatedItems) { var item = order._items.First(i => i.Id == itemDto.Id); item.ProductName = itemDto.ProductName; item.Quantity = itemDto.Quantity; } // 新增项 foreach (var itemDto in input.NewItems) { order.AddItem( ObjectMapper.Map<OrderItemDto, OrderItem>(itemDto) ); } // ABP会自动跟踪变更(但推荐显式Update) await _orderRepository.UpdateAsync(order); } } ``` 四、关键技术要点 1. **事务控制**:ABP工作单元默认自动开启事务,确保原子性操作 2. **变更跟踪**:EF Core会自动检测集合变化,但需注意: - 必须通过聚合根访问子集合(保护内部集合) - 推荐使用`Include()`预加载子项 3. **性能优化**:建议添加`[UnitOfWork(isTransactional: true)]`特性 4. **并发控制**:可通过聚合根添加`[ConcurrencyCheck]`版本字段 五、最佳实践建议 1. 在聚合根内部添加领域方法控制操作: ```csharp public class Order { internal void AddItem(OrderItem item) { // 领域逻辑校验 if (_items.Count >= 100) throw new BusinessException("ExceedMaxItems"); _items.Add(item); } } ``` 2. 使用AutoMapper配置: ```csharp CreateMap<OrderItemDto, OrderItem>() .ForMember(dest => dest.OrderId, opt => opt.Ignore()); ``` 3. 推荐DTO结构: ```csharp public class UpdateOrderItemsDto { public Guid OrderId { get; set; } public List<OrderItemDto> NewItems { get; set; } public List<OrderItemDto> UpdatedItems { get; set; } public List<Guid> DeletedItems { get; set; } } ``` 这种实现方式保证了: - 事务完整性:所有操作在单个事务中提交 - 领域封装性:业务规则内聚在聚合根中 - 性能优化:单次数据库往返完成所有操作 - 数据一致性:严格遵循聚合根边界约束 实际开发中应根据业务场景适当添加: 1. 乐观并发控制 2. 领域事件发布 3. 操作审计日志 4. 验证逻辑前置检查
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值