团队开发框架应用示例_01

引用关系

Model

1 School.cs

ViewModel

2 SchoolDto.cs

3 AddSchoolViewModel.cs

4 EditSchoolViewModel.cs

BLL

5 ISchoolService.cs

6 SchoolService.cs

WebApi

7 PqaProfile.cs  CreateMap

8 UnityConfig.cs RegisterType

9 SchoolController.cs

Web

10 SchoolController.cs

12 School/Add.cshtml

13 School/Edit.cshtml

14 School/Index.cshtml

15 School/Info.cshtml

 


Relative URI


method


WebApi


Service


Route


/api/school


get


Get


schoolService.List


 


/api/school/GetInfo?id=id


get


GetInfo


schoolService.Info


[Route("api/school/GetInfo")]

 


/api/school/


post


Post


schoolService.Add


 


/api/school/id


put


Put


schoolService.Edit


 


/api/school/ id


delete


Delete


schoolService.LogicDel


 

 

 

Get      ->  schoolService.List(pageindex.Value, pagesize.Value, keyword, new ClaimHelper().UserID);

[Route("api/school/GetInfo")]

GetInfo  ->  schoolService.Info(id);

Post     ->  schoolService.Add(entity, new ClaimHelper().UserID);

Put      ->  schoolService.Edit(entity);

Delete   ->  schoolService.LogicDel(id);

数据库设计:

Model

School.cs

using System;
using PPQA.Core.Data;

namespace PPQA.Model.Pqa
{
    public class School : EntityBase<Guid>
    {
        /// <summary>
        /// 学校名称
        /// </summary>
        public string SchoolName { get; set; }

        /// <summary>
        /// 所属教育局ID
        /// </summary>
        public Guid EdbId { get; set; }

        /// <summary>
        /// 状态值
        /// </summary>
        public int State { get; set; }

        /// <summary>
        /// 排序
        /// </summary>
        public int DisOrder { get; set; }

        /// <summary>
        /// 创建人ID
        /// </summary>
        public Guid CreateUserId { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }
    }
}

ViewModel

SchoolDto.cs

using System;

namespace PPQA.ViewModel.Pqa.Dtos
{
    public class SchoolDto
    {
        public Guid Id { get; set; }

        /// <summary>
        /// 学校名称
        /// </summary>
        public string SchoolName { get; set; }

        /// <summary>
        /// 所属教育局ID
        /// </summary>
        public Guid EdbId { get; set; }

        /// <summary>
        /// 状态值
        /// </summary>
        public int State { get; set; }

        /// <summary>
        /// 排序
        /// </summary>
        public int DisOrder { get; set; }

        /// <summary>
        /// 创建人ID
        /// </summary>
        public Guid CreateUserId { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }
    }
}

AddSchoolViewModel.cs

namespace PPQA.ViewModel.Pqa.ViewModels
{
    public class AddSchoolViewModel
    {
        /// <summary>
        /// 学校名称
        /// </summary>
        public string SchoolName { get; set; }
    }
}

EditSchoolViewModel.cs

using System;

namespace PPQA.ViewModel.Pqa.ViewModels
{
    public class EditSchoolViewModel : AddSchoolViewModel
    {
        // 只需传入ID
        public Guid Id { get; set; }
    }
}

BLL

ISchoolService.cs

using PPQA.ViewModel.Pqa.Dtos;
using PPQA.ViewModel.Pqa.ViewModels;
using System;

namespace PPQA.IBLL.Pqa
{
    public interface ISchoolService
    {
        Core.Data.EasyUiList List(int pageindex, int pagesize, string keyword, Guid userId);

        void LogicDel(Guid id);

        void Add(AddSchoolViewModel model, Guid userId);

        void Edit(EditSchoolViewModel model);

        SchoolDto Info(Guid id);
    }
}

SchoolService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using PPQA.Core.Data;
using PPQA.Core.Erro;
using PPQA.Model.Pqa;
using PPQA.ViewModel.Pqa.Dtos;
using PPQA.ViewModel.Pqa.ViewModels;
using PPQA.DAL;
using PPQA.IBLL.Pqa;

namespace PPQA.BLL.Pqa
{
    public class SchoolService : ISchoolService
    {
        private DataBaseContext db;
        IUnitOfWork<DataBaseContext> unitWork;

        public SchoolService(DataBaseContext db, IUnitOfWork<DataBaseContext> unitWork)
        {
            this.db = db;
            this.unitWork = unitWork;
        }

        public void Add(AddSchoolViewModel model, Guid userId)
        {
            var entity = Mapper.Map<AddSchoolViewModel, School>(model);

            entity.Id = Guid.NewGuid();
            entity.SchoolName = model.SchoolName;
            entity.EdbId = Guid.NewGuid();     // 所属教育局ID,暂时设个默认值
            entity.State = 1;                  // 1 正常显示,0 逻辑删除
            entity.DisOrder = 9999;
            entity.CreateUserId = userId;
            entity.CreateTime = DateTime.Now;  // 后续建议改成取服务器时间

            db.Schools.Add(entity);
            unitWork.Commit();
        }

        public void Edit(EditSchoolViewModel model)
        {
            var entity = Mapper.Map<EditSchoolViewModel, School>(model);

            db.Set<School>().Attach(entity);

            var SetEntry = ((System.Data.Entity.Infrastructure.IObjectContextAdapter)db).ObjectContext.ObjectStateManager.GetObjectStateEntry(entity);
            Type type = typeof(EditSchoolViewModel);

            foreach (var item in type.GetProperties())
            {
                if ("EntityState" == item.Name || "EntityKey" == item.Name)
                {
                    continue;
                }
                SetEntry.SetModifiedProperty(item.Name);
            }
            unitWork.Commit();
        }

        public SchoolDto Info(Guid id)
        {
            var entity = db.Schools.FirstOrDefault(p => p.Id == id);
            if (entity == null)
                throw new dsDbException("数据验证错误", new List<Core.ValidateItem>() { new Core.ValidateItem() { ErrMessage = "记录不存在!" } });
            return Mapper.Map<School, SchoolDto>(entity);
        }

        public Core.Data.EasyUiList List(int pageindex, int pagesize, string keyword, Guid userId)
        {
            var q = db.Schools.Where(p => p.State == 1);
            if (!String.IsNullOrEmpty(keyword))
            {
                q = q.Where(p => p.SchoolName.Contains(keyword));
            }
            return new Core.Data.EasyUiList() { total = q.Count(), rows = Mapper.Map<List<School>, List<SchoolDto>>(q.OrderBy(p => p.DisOrder).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList()) };
        }

        public void LogicDel(Guid id)
        {
            var entity = db.Schools.FirstOrDefault(p => p.Id == id);
            if (entity == null)
                throw new dsDbException("数据验证错误", new List<Core.ValidateItem>() { new Core.ValidateItem() { ErrMessage = "无法删除!" } });

            // state设为0,逻辑删除
            entity.State = 0;
            unitWork.Commit();
        }
    }
}

WebApi

AutoMapper-> Profiles->PqaProfile

PqaProfile.cs

CreateMap

#region School
CreateMap<School, SchoolDto>();
CreateMap<AddSchoolViewModel, School>();
CreateMap<EditSchoolViewModel, School>();
#endregion

App_Start->UnityConfig.cs

UnityConfig

container.RegisterType<ISchoolService, SchoolService>(new HierarchicalLifetimeManager());

SchoolController.cs

using PPQA.Core;
using PPQA.IBLL.Pqa;
using PPQA.ViewModel.Pqa.Dtos;
using PPQA.ViewModel.Pqa.ViewModels;
using PPQA.WebApi.Filters;
using System;
using System.Web.Http;

namespace PPQA.WebApi.Controllers
{
    [Authorize]
    [WebApiExceptionFilter]
    public class SchoolController : ApiController
    {
        private readonly ISchoolService schoolService;

        public SchoolController(ISchoolService schoolService)
        {
            this.schoolService = schoolService;
        }

        [PermissionFilter("school", (int)ActionEnum.Query)]
        public Core.Data.EasyUiList Get(int? pageindex, int? pagesize, string keyword)
        {
            if (!pageindex.HasValue) pageindex = 1;
            if (!pagesize.HasValue) pagesize = Core.Data.Pager.PageSize;
            // userId = new ClaimHelper().UserID
            return schoolService.List(pageindex.Value, pagesize.Value, keyword, new ClaimHelper().UserID);
        }

        [Route("api/school/GetInfo")]
        [PermissionFilter("school", (int)ActionEnum.Query)]
        public SchoolDto GetInfo(Guid id)
        {
            return schoolService.Info(id);
        }

        [PermissionFilter("school", (int)ActionEnum.Add)]
        public void Post([FromBody]AddSchoolViewModel entity)
        {
            schoolService.Add(entity, new ClaimHelper().UserID);
        }

        [PermissionFilter("school", (int)ActionEnum.Edit)]
        public void Put(Guid id, [FromBody]EditSchoolViewModel entity)
        {
            entity.Id = id;
            schoolService.Edit(entity);
        }

        [PermissionFilter("school", (int)ActionEnum.Del)]
        public void Delete(Guid id)
        {
            schoolService.LogicDel(id);
        }

    }
}

前端展现

SchoolController.cs

using System.Web.Mvc;

namespace PPQA.Web.Controllers
{
    public class SchoolController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Add()
        {
            return View();
        }

        public ActionResult Edit()
        {
            return View();
        }

        public ActionResult Info()
        {
            return View();
        }
    }
}

School/Add.cshtml

@{
    ViewBag.Title = "新增学校";
}
<div style="padding:10px 40px 20px 40px">
    <table cellpadding="5">
        <tr>
            <td>学校名称:</td>
            <td><input class="easyui-textbox" type="text" id="txtSchoolName" data-options="required:true" /></td>
        </tr>
    </table>

    <div style="text-align:center;padding:5px">
        <a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm()">提 交</a>
    </div>
</div>

<script src="~/Scripts/layer/layer.js"></script>
<script>
    function submitForm() {
        var postdata = {};
        postdata.SchoolName = $("#txtSchoolName").textbox(‘getValue‘).trim();
        if (isEmpty(postdata.SchoolName)) {
            $.messager.alert(‘警告‘, ‘请输入学校名称!‘, ‘info‘);
            return;
        }

        BoerAjax("/api/school/", postdata, "post", function () {
            top.window.frames["ContentFrame"].DataReLoad();
            top.$(‘#dd‘).dialog("close");
        });
    }
</script>

School/Edit.cshtml

@{
    ViewBag.Title = "编辑学校";
}

<div style="padding:10px 40px 20px 40px">
    <table cellpadding="5">
        <tr>
            <td>学校名称:</td>
            <td><input class="easyui-textbox" type="text" id="txtSchoolName" data-options="required:true" /></td>
        </tr>
    </table>

    <div style="text-align:center;padding:5px">
        <a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm()">提 交</a>
    </div>
</div>

<input id="hiddenSchoolId" type="hidden" value="" />
<script src="~/Scripts/layer/layer.js"></script>
<script>
    function loadInfo() {
        var id = $("#hiddenSchoolId").val();
        if (id.isEmpty()) {
            $.messager.alert(‘警告‘, ‘未获取传入的信息!‘, ‘info‘);
            return;
        }

        BoerAjax("/api/school/GetInfo?id=" + id, null, "get", function (data) {
            $("#txtSchoolName").textbox("setValue", data.SchoolName);
        });

    }

    function submitForm() {
        var id = $("#hiddenSchoolId").val();
        var postdata = {};
        postdata.SchoolName = $("#txtSchoolName").textbox(‘getValue‘).trim();
        postdata.DisOrder = 999;
        if (isEmpty(postdata.SchoolName)) {
            $.messager.alert(‘警告‘, ‘请输入学校姓名!‘, ‘info‘);
            return;
        }
        BoerAjax("/api/school/" + id, postdata, "put", function () {
            top.window.frames["ContentFrame"].DataReLoad();
            $.messager.alert(‘警告‘, ‘保存成功!‘, ‘info‘);
            setTimeout(function () {
                top.$(‘#dd‘).dialog("close");
            }, 1000);

        });
    }
</script>

School/Index.cshtml

@{
    ViewBag.Title = "学校列表";
}
<div id="toolbar">
    <div>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-add" onclick="Add()" plain="true">新增学校</a>
        <label style="margin-left:20px">关键字:</label>
        <input id="txtKeyword" class="easyui-textbox" data-options="prompt:‘请输入学校‘" style="width:150px;height:25px" value="" />
        <a href="javascript:void(0)" class="easyui-linkbutton" onclick="searchInfo()" iconCls="icon-search">查询</a>
    </div>
</div>
<div id="dg">
</div>
<from id="myForm" enctype="application/x-www-form-urlencoded"></from>
<script src="~/Scripts/jquery-plugin/jquery.form.js"></script>
<script type="text/javascript">

    $(document).ready(function () {
        LoadData();
    });

    function LoadData() {
        var h = $(window).height();
        var dataGrid = $(‘#dg‘);
        var keyword = $("#txtKeyword").val();
        dataGrid.datagrid({
            idField: ‘Id‘,
            title: ‘学校列表‘,
            autoRowHeight: false,
            loadMsg: "数据加载中,请稍候...",
            pageList: [50, 40, 30, 20, 10],
            pageSize: 10,
            striped: true,
            height: h,
            fitColumns: true,
            singleSelect: true,
            rownumbers: true,
            pagination: true,
            nowrap: false,
            showFooter: false,
            toolbar: ‘#toolbar‘,
            columns: [[
                {
                    field: ‘cz‘, title: ‘操作‘, width: 180, halign: ‘center‘, align: ‘center‘, fixed: true,
                    formatter: function (value, row, index) {
                        return ‘<button type="button" class="btn btn-warning btn-xs" onclick="Edit(\‘‘ + row.Id + ‘\‘)"><span class="glyphicon glyphicon-edit"></span>编辑</button> <button type="button" class="btn btn-danger btn-xs" onclick="Del(\‘‘ + row.Id + ‘\‘,\‘‘ + row.SchoolName + ‘\‘)"><span class="glyphicon glyphicon-remove"></span>删除</button>‘;
                    }
                }, {
                    field: ‘SchoolName‘, title: ‘学校名称‘, width: 240, halign: ‘center‘, align: ‘left‘, fixed: true,
                    formatter: function (value, row, index) {
                        return value;
                    }
                }, {
                    field: ‘CreateTime‘, title: ‘创建时间‘, width: 150, halign: ‘center‘, align: ‘center‘, fixed: true,
                    formatter: function (value, row, index) {
                        if (value != null) {
                            var d = new Date(value);
                            return d.format("yyyy-MM-dd HH:mm:ss");
                        } else {
                            return "";
                        }
                    }
                }
            ]]
        });

        dataGrid.datagrid(‘getPager‘).pagination({
            displayMsg: ‘当前显示从 [{from}] 到 [{to}] 共[{total}]条记录‘,
            onSelectPage: function (pPageIndex, pPageSize) {
                //改变opts.pageNumber和opts.pageSize的参数值,用于下次查询传给数据层查询指定页码的数据
                var ajaxdata = {};
                ajaxdata.pageindex = pPageIndex;
                ajaxdata.pagesize = pPageSize;
                ajaxdata.keyword = keyword;
                BoerAjax("/api/school", ajaxdata, "get", function (data) {
                    dataGrid.datagrid(‘loadData‘, data);
                });
            }
        });

        BoerAjax("/api/school", { pageindex: 1, pagesize: dataGrid.datagrid(‘getPager‘).data("pagination").options.pageSize, keyword: keyword }, "get", function (data) {
            dataGrid.datagrid(‘loadData‘, data);
        });
    }

    $(window).resize(function () {
        onResize();
    });

    function onResize() {
        $("#dg").height = $(window).height();
        $("#dg").datagrid("options").width = $(window).width();
        $("#dg").datagrid("resize");
    }

    function searchInfo() {
        BoerAjax("/api/school", { pageindex: 1, pagesize: $(‘#dg‘).datagrid(‘getPager‘).data("pagination").options.pageSize, keyword: $("#txtKeyword").val() }, "get", function (data) {
            $(‘#dg‘).datagrid(‘loadData‘, data);
        });
    }

    function Del(id, name) {
        $.messager.confirm(‘确定删除‘, ‘您确定要删除学校[‘ + name + ‘]吗?‘, function (r) {
            if (r) {
                BoerAjax("/api/school/" + id, null, "delete", function (data) {
                    DataReLoad();
                });
            }
        });
    }

    function Edit(id) {
        top.$(‘#dd‘).dialog({
            title: ‘编辑学校‘,
            width: 400,
            height: 250,
            closed: false,
            cache: false,
            iconCls: ‘icon-edit‘,
            href: ‘/school/Edit?id=‘ + id,
            modal: true,
            onLoad: function () {
                top.$("#hiddenSchoolId").val(id);
                top.loadInfo();
            }
        }).dialog(‘center‘);
    }

    function Add() {
        top.$(‘#dd‘).dialog({
            title: ‘新增学校‘,
            width: 400,
            height: 250,
            closed: false,
            cache: false,
            iconCls: ‘icon-add‘,
            href: ‘/school/Add‘,
            modal: true
        }).dialog(‘center‘);
    }

    function DataReLoad() {
        $("#txtKeyword").val(‘‘);
        searchInfo();
    }

</script>

School/Info.cshtml

@model dynamic

@{
    ViewBag.Title = "title";
    Layout = "_Layout";
}

<h2>title</h2>

前端界面展现

时间: 2024-11-13 11:56:26

团队开发框架应用示例_01的相关文章

Android开源项目总结

Android经典的开源项目其实非常多,把自己熟悉的一些开源项目整理起来,希望能对Android开发同学们有所帮助 项目篇: 1.Apollo音乐播放器 就一个很好的播放器,但是实现的特别好!!! 地址:https://github.com/Splitter/android_packages_apps_apolloMod 2.Oschina客户端 OSChina网站客户端,wp版,ios版都有开源哦. 地址: https://github.com/oschina/android-app 3.Xa

Android--上百个Android开源项目分享

上百个Android开源项目分享,希望对android开发有帮助. Android PDF 阅读器 http://sourceforge.net/projects/andpdf/files/ 个人记账工具 OnMyMeans http://sourceforge.net/projects/onmymeans/develop Android电池监控 Android Battery Dog http://sourceforge.net/projects/andbatdog/ RSS阅读软件 Andr

android开源项目学习

FBReaderJ FBReaderJ用于Android平台的电子书阅读器,它支持多种电子书籍格式包括:oeb.ePub和fb2.此外还支持直接读取zip.tar和gzip等压缩文档. 项目地址:http://www.fbreader.org/FBReaderJ/ Angle Angle是一款专为Android平台设计的,适合快速开发的2D游戏引擎,基于OpenGL ES技术开发.该引擎全部用Java代码编写,并且可以根据自己的需要替换里面的实现. 项目地址:http://code.google

Android 开源项目集合

上百个Android开源项目分享,希望对android开发有帮助. Android PDF 阅读器 http://sourceforge.net/projects/andpdf/files/ 个人记账工具 OnMyMeans http://sourceforge.net/projects/onmymeans/develop Android电池监控 Android Battery Dog http://sourceforge.net/projects/andbatdog/ RSS阅读软件 Andr

android经典Demo(转载)

一篇不错的资源博文,转载分享给大家: 1.Android团队提供的示例项目  如果不是从学习Android SDK中提供的那些样例代码开始,可能没有更好的方法来掌握在Android这个框架上开发.由Android的核心开发团队提供了15个优秀的示例项目,包含了游戏.图像处理.时间显示.开始菜单快捷方式等.  地址: http://www.apkbus.com/android-13506-1-1.html 2. Remote Droid  RemoteDroid是一个Android应用,能够让用户

android 近百个源码项目【转】

http://www.cnblogs.com/helloandroid/articles/2385358.html Android开发又将带来新一轮热潮,很多开发者都投入到这个浪潮中去了,创造了许许多多相当优秀的应用.其中也有许许多多的开发者提供了应用开源项目,贡献出他们的智慧和创造力.学习开源代码是掌握技术的一个最佳方式.下面推荐几个应用开源项目,这些项目不仅提供了优秀的创意,也可以直接掌握 Android内核的接口使用: 1.Android团队提供的示例项目 如果不是从学习Android S

Android开发者必须深入学习的10个应用开源项目

Android开发又将带来新一轮热潮,很多开发者都投入到这个浪潮中去了,创造了许许多 多相当优秀的应用.其中也有许许多多的开发者提供了应用开源项 目,贡献出他们的智 慧和创造力. android开发, 开源项目Android开发又将带来新一轮热潮,很多开发者都投入到这个浪潮中去了,创造了许许多 多相当优秀的应用.其中也有许许多多的开发者提供了应用开源项 目,贡献出他们的智 慧和创造力.学习开源代码是掌握技术的一个最佳方式.下面推荐几个应用开源项目, 这些项目不仅提供了优秀的创意,也可以直接掌握

Android开发者必须深入学习的10个应用开源项目(不知道,想成大牛有点难)

1.Android团队提供的示例项目 如果不是从学习Android SDK中提供的那些样例代码开始,可能没有更好的方法来掌握在Android这个框架上开发.由Android的核心开发团队提供了15个优秀的示例项 目,包含了游戏.图像处理.时间显示.开始菜单快捷方式等. Android特效免费下载源码地址:http://www.itlanbao.com/ Android技术交流扣扣群:468184825 2.ArrowDownloadButton下载按钮从点击到下载完成特效 ArrowDownlo

读教材并提问

1.书中P14用图表明确的展示出了计算机科学和软件工程的不同侧重点,且两者相互之间也能用同样的形式来证明.既然如此,我们在有正确理论的前提下,为什么作者会偏向将其看成为工程,而不是想如何将工程越与理论相吻合? 答:理想总是和实践有出入的,我们必须通许更多的实践来达到理想. 2.在这个章节中我了解到了单元测试的重要性,问题出现在P24页小飞与阿超的对话,既然单元测试(每一个模块)要写的尽可能的详细来达到避免 bug的出现.例如同样的功能可以用10行代码构成,或许能用比其更简洁的代码写出,那么是否会