ABP之应用服务层Application

1  构建Dto

Dto的作用及好处可参考这篇博文http://www.cnblogs.com/farb/p/4930968.html

创建两个Dto类,用于接受列表数据与表单提交

AgencyListDto

[AutoMapFrom(typeof(Agency))]
    public class AgencyListDto: EntityDto
    {
        public CreateTenantInput TenantInput { get; set; }

        public int TenantId { get; set; }

        public string Code { get; set; }

        public string Name { get; set; }

        public string Address { get; set; }

        public string PhoneNumber { get; set; }
    }

CreateAgencyInput

[AutoMap(typeof(Agency))]
    public class CreateAgencyInput
    {
        public CreateTenantInput TenantInput { get; set; }

        public int TenantId { get; set; }

        public string Code { get; set; }

        public string Name { get; set; }

        public string Address { get; set; }

        public string PhoneNumber { get; set; }
    }

2  创建AppService

IAgencyAppService

public interface IAgencyAppService : IApplicationService
    {
        [HttpGet]
        ListResultDto<AgencyListDto> GetAgencys();
        [HttpPost]
        Task CreateAgency(CreateAgencyInput input);
    }

HttpGet需要NuGet添加Microsoft.AspNet.WebApi.Core包


AgencyAppService

public class AgencyAppService : MyABPAppServiceBase, IAgencyAppService
    {
        private readonly IRepository<Agency, long> _agencyRepository;

        private readonly TenantManager _tenantManager;
        private readonly RoleManager _roleManager;
        private readonly EditionManager _editionManager;
        private readonly IAbpZeroDbMigrator _abpZeroDbMigrator;

        public AgencyAppService(
            IRepository<Agency, long> agencyRepository,
            TenantManager tenantManager,
            RoleManager roleManager,
            EditionManager editionManager,
            IAbpZeroDbMigrator abpZeroDbMigrator)
        {
            _agencyRepository = agencyRepository;
            _tenantManager = tenantManager;
            _roleManager = roleManager;
            _editionManager = editionManager;
            _abpZeroDbMigrator = abpZeroDbMigrator;
        }

        public ListResultDto<AgencyListDto> GetAgencys()
        {
            try
            {
                //调用Agency仓储的特定方法GetAllList
                var agencys = _agencyRepository.GetAllList();

                //用AutoMapper自动将List<Agency>转换成List<AgencyListDto>
                return new ListResultDto<AgencyListDto>(
                    agencys.MapTo<List<AgencyListDto>>()
                );
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public async Task CreateAgency(CreateAgencyInput input)
        {
            try
            {
                //Create tenant-------------------------------------分割线,分割线里面代码为copy框架创建Tenant代码
                CreateTenantInput tenantInput = input.TenantInput;
                var tenant = tenantInput.MapTo<Tenant>();
                tenant.ConnectionString = input.TenantInput.ConnectionString == ""
                    ? null
                    : SimpleStringCipher.Instance.Encrypt(input.TenantInput.ConnectionString);

                var defaultEdition = await _editionManager.FindByNameAsync(EditionManager.DefaultEditionName);
                if (defaultEdition != null)
                {
                    tenant.EditionId = defaultEdition.Id;
                }

                CheckErrors(await TenantManager.CreateAsync(tenant));
                await CurrentUnitOfWork.SaveChangesAsync(); //To get new tenant's id.

                //Create tenant database
                _abpZeroDbMigrator.CreateOrMigrateForTenant(tenant);

                //We are working entities of new tenant, so changing tenant filter
                using (CurrentUnitOfWork.SetTenantId(tenant.Id))
                {
                    //Create static roles for new tenant
                    CheckErrors(await _roleManager.CreateStaticRoles(tenant.Id));

                    await CurrentUnitOfWork.SaveChangesAsync(); //To get static role ids

                    //grant all permissions to admin role
                    var adminRole = _roleManager.Roles.Single(r => r.Name == StaticRoleNames.Tenants.Admin);
                    await _roleManager.GrantAllPermissionsAsync(adminRole);

                    //Create admin user for the tenant
                    var adminUser = User.CreateTenantAdminUser(tenant.Id, input.TenantInput.AdminEmailAddress, User.DefaultPassword);
                    CheckErrors(await UserManager.CreateAsync(adminUser));
                    await CurrentUnitOfWork.SaveChangesAsync(); //To get admin user's id

                    //Assign admin user to role!
                    CheckErrors(await UserManager.AddToRoleAsync(adminUser.Id, adminRole.Name));
                    await CurrentUnitOfWork.SaveChangesAsync();
                }
                //----------------------------------------------------------------------------------------------------------

                var agency = input.MapTo<Agency>();

                agency.TenantId = tenant.Id;

                await _agencyRepository.InsertAsync(agency);
                await CurrentUnitOfWork.SaveChangesAsync();
            }
            catch (Exception e)
            {

            }
        }
    }
View Code

默认api项目不做改动,访问api路径,可以正常返回数据

原文地址:https://www.cnblogs.com/flywei/p/6496501.html