团队开发框架实战——一个简单的CRUD示例

新思想、新技术、新架构——更好更快的开发现代ASP.NET应用程序

Tdf团队开发框架分层架构图

团队开发框架实战——一个简单的CRUD示例_第1张图片
Tdf团队开发框架分层架构图.png

Tdf团队开发框架汇集了一些比较流行的技术和开源项目,也把自己的程序架构、部分代码风格、前端表现简单做了一些展示。
由于这个功能实在太简单,没有使用到领域服务、领域事件,这里可能只能说明一件事件:没有复杂业务逻辑的功能使用此DDD框架,并不会增加代码量,反而我认为这样的代码量差不多已经少到极致了。

1 Tdf.Domain.Entities

Role.cs

using System;
using Tdf.Domain.Entities;
using Tdf.Domain.Entities.Auditing;

namespace Tdf.Domain.Act.Entities
{
    public class Role : Entity
        , IFullAudited
        , IMustHaveTenant
    {
        public Guid TenantId { get; set; }
        public string Name { get; set; }
        public string DisplayName { get; set; }
        public bool IsStatic { get; set; }
        public bool IsDefault { get; set; }
        public bool IsDeleted { get; set; }
        public Guid? DeleterUserId { get; set; }
        public DateTime? DeletionTime { get; set; }
        public Guid? LastModifierUserId { get; set; }
        public DateTime? LastModificationTime { get; set; }
        public Guid? CreatorUserId { get; set; }
        public DateTime CreationTime { get; set; }
        public int DisOrder { get; set; }
        public int State { get; set; }
    }
}

2 Tdf.EntityFramework

ActDbContext.cs

public virtual IDbSet Roles { get; set; }
modelBuilder.Configurations.Add(new RoleMap());

RoleMap.cs

using System.Data.Entity.ModelConfiguration;
using Tdf.Domain.Act.Entities;

namespace Tdf.EntityFramework.Act.Mapping
{
    public class RoleMap : EntityTypeConfiguration
    {
        public RoleMap()
        {
            // Primary Key
            this.HasKey(t => t.Id);

            // Table & Column Mappings
            this.ToTable("Act_Role");

            this.Property(t => t.Id).HasColumnName("RoleId");
        }
    }
}

3 Tdf.Application

Dtos

CreateRoleInput.cs

using System;
using Tdf.Application.Services.Dto;

namespace Tdf.Application.Act.RoleMgr.Dtos
{
    public class CreateRoleInput : IInputDto
    {
      
        public Guid TenantId { get; set; }
        public string Name { get; set; }
        public string DisplayName { get; set; }
        public bool IsStatic { get; set; }
        public bool IsDefault { get; set; }
        public bool IsDeleted { get; set; }
        public Guid? DeleterUserId { get; set; }
        public DateTime? DeletionTime { get; set; }
        public Guid? LastModifierUserId { get; set; }
        public DateTime? LastModificationTime { get; set; }
        public Guid? CreatorUserId { get; set; }
        public DateTime CreationTime { get; set; }
        public int DisOrder { get; set; }
        public int State { get; set; }
    }
}

UpdateRoleInput.cs

using System;

namespace Tdf.Application.Act.RoleMgr.Dtos
{
    public class UpdateRoleInput : CreateRoleInput
    {
        public Guid Id { get; set; }
    }
}

RoleDto.cs

using System;

namespace Tdf.Application.Act.RoleMgr.Dtos
{
    public class RoleDto
    {
        public Guid Id { get; set; }
        public Guid TenantId { get; set; }
        public string Name { get; set; }
        public string DisplayName { get; set; }
        public bool IsStatic { get; set; }
        public bool IsDefault { get; set; }
        public bool IsDeleted { get; set; }
        public Guid? DeleterUserId { get; set; }
        public DateTime? DeletionTime { get; set; }
        public Guid? LastModifierUserId { get; set; }
        public DateTime? LastModificationTime { get; set; }
        public Guid? CreatorUserId { get; set; }
        public DateTime CreationTime { get; set; }
        public string DisOrder { get; set; }
        public string State { get; set; }
    }
}

IRoleAppService.cs

using System.Threading.Tasks;
using Tdf.Application.Act.RoleMgr.Dtos;
using Tdf.Application.Services;
using Tdf.Application.Services.Dto;

namespace Tdf.Application.Act.RoleMgr
{
    public interface IRoleAppService : IApplicationService
    {
        Task Create(CreateRoleInput model);
        Task Update(UpdateRoleInput model);
        Task GetInfo(DetailInfoInput model);
        Task BatchDelete(BatchDeleteInput model);
        JsonMessage GetPage(PageInput model);
    }
}

RoleAppService

using AutoMapper;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Tdf.Application.Act.RoleMgr.Dtos;
using Tdf.Application.Services.Dto;
using Tdf.Domain.Act.Entities;
using Tdf.Domain.Repositories;
using Tdf.Domain.Uow;
using Tdf.Utils.Excp;
using Tdf.Utils.Networking;

namespace Tdf.Application.Act.RoleMgr
{
    public class RoleAppService : IRoleAppService
    {
        private readonly IUnitOfWork _uow;

        private readonly IRepository _roleRepository;

        public RoleAppService(IUnitOfWork uow, IRepository roleRepository)
        {
            this._roleRepository = roleRepository;
            this._uow = uow;
        }

        public async Task Create(CreateRoleInput model)
        {

            var entity = Mapper.Map(model);
            entity.Id = Guid.NewGuid();

            await _roleRepository.InsertAsync(entity);
            await _uow.SaveChangesAsync();

            return ServiceResult.GetErrorMsgByErrCode(0);
        }

        public async Task Update(UpdateRoleInput model)
        {
            Role entity = await _roleRepository.SingleAsync(b => b.Id == model.Id);
            entity.Name = model.Name;
            entity.DisplayName = model.DisplayName;
            entity.State = model.State;
            entity.DisOrder = model.DisOrder;

            await _roleRepository.UpdateAsync(entity);
            await _uow.SaveChangesAsync();

            return ServiceResult.GetErrorMsgByErrCode(0);
        }

        public async Task GetInfo(DetailInfoInput model)
        {
            var entity = await _roleRepository.SingleAsync(b => b.Id == model.Id);

            var jsonMsg = ServiceResult.GetErrorMsgByErrCode(0);
            jsonMsg.Result = Mapper.Map(entity);
            return jsonMsg;
        }

        public async Task BatchDelete(BatchDeleteInput model)
        {
            if (model.IdList.Count == 0)
                throw new CustomException(-1, "没有删除的对象!");

            Role entity;
            foreach (var item in model.IdList)
            {
                entity = await _roleRepository.SingleAsync(b => b.Id == item);
                await _roleRepository.DeleteAsync(entity);
            }

            var cnt = await _uow.SaveChangesAsync();
            var jsonMsg = ServiceResult.GetErrorMsgByErrCode(0);
            jsonMsg.Result = $"成功删除{cnt}条数据";
            return jsonMsg;
        }

        public JsonMessage GetPage(PageInput model)
        {
            if (string.IsNullOrEmpty(model.OrderBy))
                model.OrderBy = "DisOrder";

            object entity;
            int total = 0;

            if (!string.IsNullOrEmpty(model.Keyword))
            {
                entity = _roleRepository.Get(p => p.Name.Contains(model.Keyword), model.OrderBy, model.PageIndex, model.PageSize);
                total = _roleRepository.Count(p => p.Name.Contains(model.Keyword));
            }
            else
            {
                entity = _roleRepository.Get(model.OrderBy, model.PageIndex, model.PageSize);
                total = _roleRepository.Count();
            }

            var jsonMsg = ServiceResult.GetErrorMsgByErrCode(0);
            jsonMsg.Result = new PageOutput() { Total = total, Records = Mapper.Map>(entity) };
            return jsonMsg;

        }
    }
}

4 Tdf.WebApi

** UnityConfig.cs **

container.RegisterType(new HierarchicalLifetimeManager());
container.RegisterType(new HierarchicalLifetimeManager());

container.RegisterType, ActRepositoryBase>(new HierarchicalLifetimeManager());
container.RegisterType(new HierarchicalLifetimeManager());

** ActProfile.cs **

CreateMap();
CreateMap();
CreateMap();

ActRoleController.cs

using System;
using System.Threading.Tasks;
using System.Web.Http;
using Tdf.Application.Act.RoleMgr;
using Tdf.Application.Act.RoleMgr.Dtos;
using Tdf.Application.Services.Dto;

namespace Tdf.WebApi.Controllers.ActMgr
{
    public class ActRoleController : ApiController
    {

        private readonly IRoleAppService _roleAppService;

        public ActRoleController(IRoleAppService roleAppService)
        {
            _roleAppService = roleAppService;
        }

        [Route("api/ActRole/CreateRole")]
        [HttpPost]
        public async Task CreateRole([FromBody]CreateRoleInput input)
        {
            string defaultGuid = "{41643A10-7D28-4692-94FA-4A427FE22147}";
            input.TenantId = new Guid(defaultGuid);
            input.IsStatic = true;
            input.IsDefault = true;
            input.IsDeleted = false;
            input.CreatorUserId = Guid.NewGuid();
            return await _roleAppService.Create(input);
        }

        [Route("api/ActRole/UpdateRole")]
        [HttpPost]
        public async Task UpdateRole([FromBody]UpdateRoleInput input)
        {
            return await _roleAppService.Update(input);
        }

        [Route("api/ActRole/GetPageRole")]
        [HttpGet]
        public JsonMessage GetPageRole([FromUri]PageInput input)
        {
            return _roleAppService.GetPage(input);
        }

        [Route("api/ActRole/GetRoleInfo")]
        [HttpGet]
        public async Task GetRoleInfo([FromUri]DetailInfoInput input)
        {
            return await _roleAppService.GetInfo(input);
        }

        [Route("api/ActRole/BatchDeleteRole")]
        [HttpPost]
        public async Task BatchDeleteRole([FromBody]BatchDeleteInput input)
        {
            return await _roleAppService.BatchDelete(input);
        }

    }
}

5 Tdf.Web

roleList.html




    
    
    
    
    
    
    


    
新增 修改 删除 查询

roleDetail.html




    
    
    
    
    
    
    


    
角色名称:
角色描述:
使用状态:
显示顺序:

界面截图

团队开发框架实战——一个简单的CRUD示例_第2张图片
roleList.png
团队开发框架实战——一个简单的CRUD示例_第3张图片
roleDetail.png

你可能感兴趣的:(团队开发框架实战——一个简单的CRUD示例)