项目重构之路

项目重构之路:架构篇

项目重构之路_第1张图片
系统架构

项目重构之路:实现篇

1、改成PaperOutDto;
2、从BLL中PaperService抽取出PaperDAL;
3、前端提取出PaperPublic.js;
4、PaperController、IPaperService、PaperService合并同类项,分为如下四种情况:

报表,不分页情况
报表,分页情况
图表
导出

PaperController

using Boer.QAOE.Dto.Pqa.OutDto;
using Boer.QAOE.IBLL.Pqa;
using System.Collections.Generic;
using System.Web.Http;

namespace Boer.QAOE.Web.Controllers
{
    public class PaperController : ApiController
    {
        private readonly IPaperService paperService;
        public PaperController(IPaperService _paperService)
        {
            paperService = _paperService;
        }

        [Route("api/Paper/GetPaperReport")]
        [HttpPost]
        public List> GetPaperReport(PaperPagingOutDto entity)
        {
            entity.TenantID = new ClaimHelper().TenantId;
            return paperService.GetPaperReport(entity);
        }

        [Route("api/Paper/GetPaperPagingReport")]
        [HttpPost]
        public Dictionary GetPaperPagingReport(PaperPagingOutDto entity)
        {
            entity.TenantID = new ClaimHelper().TenantId;
            return paperService.GetPaperPagingReport(entity);
        }

        [Route("api/Paper/GetPaperChart")]
        [HttpPost]
        public Dictionary GetPaperChart(PaperOutDto entity)
        {
            entity.TenantID = new ClaimHelper().TenantId;
            return paperService.GetPaperChart(entity);
        }

        [Route("api/Paper/GetPaperExport")]
        [HttpPost]
        public string GetPaperExport(PaperPagingOutDto entity)
        {
            entity.TenantID = new ClaimHelper().TenantId;
            return paperService.GetPaperExport(entity);
        }
    }
}

IPaperService

using Boer.QAOE.Dto.Pqa.OutDto;
using System.Collections.Generic;

namespace Boer.QAOE.IBLL.Pqa
{
    public interface IPaperService
    {
        List> GetPaperReport(PaperPagingOutDto entity);
        Dictionary GetPaperPagingReport(PaperPagingOutDto entity);
        Dictionary GetPaperChart(PaperOutDto entity);
        string GetPaperExport(PaperPagingOutDto entity);
    }
}

PaperPublic.js

// PaperPublic.js 文件    

function GetDto(dataGrid, queryType) {
    var ajaxData = {};

    ajaxData.QueryType = queryType;

    var examCourseID = $("#txtExamCourseId").val();
    if (!examCourseID.isEmpty()) {
        ajaxData.ExamCourseID = examCourseID;
    }

    var orgIDList = $("input[name='school']:checked").map(function () {
        return $(this).val();
    }).get().join(',');
    if (!orgIDList.isEmpty()) {
        ajaxData.orgIDList = orgIDList;
    }

    var classIDList = $("input[name='classes']:checked").map(function () {
        return $(this).val();
    }).get().join(',');

    if (!classIDList.isEmpty()) {
        ajaxData.ClassIDList = classIDList;
    }

    if (dataGrid.length != 0) {
        var pager = dataGrid.datagrid('getPager');
        if (pager) {
            if (pager.data("pagination")) {
                var options = pager.data("pagination").options;
                if (options) {
                    ajaxData.PageIndex = options.pageNumber;
                    ajaxData.PageSize = options.pageSize;
                }
            }
        }

        var orderby = '"' + dataGrid.datagrid('options').sortName + '"' + ' ' + dataGrid.datagrid('options').sortOrder;
        if (!orderby.isEmpty()) {
            ajaxData.SortName = orderby;
        }
    }

    return ajaxData;
}

PaperOutDto

namespace Boer.QAOE.Dto.Pqa.OutDto
{
    public interface IPaging
    {
        int PageIndex { get; set; }
        int PageSize { get; set; }
        string SortName { get; set; }
    }

    public interface IQueryAanalysis
    {
        int QueryType { get; set; }
    }

    public class PaperOutDto : IQueryAanalysis
    {
        public string ExamCourseID { get; set; }
        public string OrgIDList { get; set; }
        public string ClassIDList { get; set; }
        public string TenantID { get; set; }
        public int QueryType { get; set; }
}

    public class PaperPagingOutDto : PaperOutDto, IPaging
    {
        public int PageIndex { get; set; }
        public int PageSize { get; set; }
        public string SortName { get; set; }
    }

}

重构PaperDAL

重构前代码

 var sql = "";
IsSortSpecial = false;
// 1 不分页 2 分页 3 图表 4 导出
switch (entity.QueryType)
{
    case 11: // 试卷总体分析->总体分析
        sql = PaperDAL.GetPaperGeneralSql(entity);
        break;
    case 12: // 试卷总体分析->明细分析
        sql = PaperDAL.GetPaperDetialSql(entity);
        break;
    case 13: // 试卷分析->题型分析
        sql = PaperDAL.GetQuestionTypeSql(entity);
        break;

    case 21:   // 试卷分析->客观题统计
        sql = PaperDAL.GetObjectiveQuestionsSql(entity);
        break;
    case 22:   // 试卷分析->学校试题均分统计,试卷分析->题目分组分析 
        sql = PaperDAL.GetSchoolQuestionsAvgSql(entity);
        break;
    case 23:   // 试卷分析->学校试题分析
        IsSortSpecial = true;
        sql = PaperDAL.GetSchoolQuestionsSql(entity);
        break;
    case 24:   // 试卷分析->班级试题分析
        IsSortSpecial = true;
        sql = PaperDAL.GetClassQuestionsSql(entity);
        break;

    case 31:   // 试卷分析->题型分析
        sql = PaperDAL.GetQuestionTypeChartSql(entity);
        break;
    case 32:   // 试卷分析->学生考试状态评价
        sql = PaperDAL.GetStudentExamStateSql(entity);
        break;

    case 41:
        ExportTitle = "试卷总体分析";
        sql = PaperDAL.GetPaperDetialSql(entity);
        break;
    case 42:
        ExportTitle = "客观题统计";
        sql = PaperDAL.GetObjectiveQuestionsSql(entity);
        break;
    case 43:
        ExportTitle = "学校小题均分统计";
        sql = PaperDAL.GetSchoolQuestionsAvgSql(entity);
        break;
    case 44:
        ExportTitle = "学校试题分析";
        sql = PaperDAL.GetSchoolQuestionsSql(entity);
        break;
    case 45:
        ExportTitle = "班级试题分析";
        sql = PaperDAL.GetClassQuestionsSql(entity);
        break;
    case 46:
        ExportTitle = "题型分析";
        IsTemplate = true;
        TemplateFile = "TxfxTemplet1.xls";
        PaperSize = "A4";
        sql = PaperDAL.GetQuestionTypeSql(entity);
        break;
    case 47:
        ExportTitle = "题目分组分析";
        sql = PaperDAL.GetSchoolQuestionsAvgSql(entity);
        break;
}
return sql;           

重构后代码

Paper paper = repository.Get(entity.QueryType);

ExportTitle = paper.ExportTitle;
IsTemplate = paper.IsTemplate;
TemplateFile = paper.TemplateFile;
PaperSize = paper.PaperSize;
IsSortSpecial = paper.IsSortSpecial;

Type t = Type.GetType("Boer.QAOE.DAL.Pqa.PaperDAL");
object dObj = Activator.CreateInstance(t);
MethodInfo method = t.GetMethod(paper.MethodName);
var sql = method.Invoke(dObj, new Object[] { entity }).ToString();

return sql;

你可能感兴趣的:(项目重构之路)