使用TT模板+mvc+wcf实现简单查询

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

       今天是除夕,小编的这篇博客是掐着点儿发的,在此,祝各位小伙伴新年快乐,身体健康,万事如意;喜从天降,欣喜若狂;喜气盈门,好事成双;好人好运,金玉满堂;神采飞扬,如愿以偿,财源滚滚来,福如东海长;伴随着除夕的脚步,小编接手的档案管理项目也渐渐步入正轨,从开始的需求分析,到使用Axure画原型图,再到使用powerdesigner设计实体,生成数据库,一直到昨天刚刚通了一条线,这一路走来,小编懂得了很多,无路学习还是和团队小伙伴的交流合作,有时候,总是会因为大家意见不统一,搞的大家吹胡子瞪眼,但有时也会因为某件事情圆满成功而欢欣鼓舞,小组中最开心的事儿莫过于有人开会迟到买吃的了,哈哈,暴露了小编的本质,开头说了这么多,接下来,小编就跟分享一下,如果利用TT模板+mvc+wcf实现简单查询,有不同意见的小伙伴欢迎讨论交流......

        首先,搭建好框架,分别为mvc,wcf,B,D,在这里说明一下在这里我们使用TT模板,TT模板好陌生的名字,小编以前都不知道在这个世界上有她的存在,再此,对不住了TT,TT在背后默默无闻的奉献着,犹如蜡烛,燃烧自己照亮别人,直到这次接手项目,小编才有幸见到她犹抱琵琶半遮面的神奇,小编简单来介绍一下TT模板,也叫T4模板,全称Text Template Transformation Toolkit是微软官方在VisualStudio 2008中开始使用的代码生成引擎。在 Visual Studio 中,“T4 文本模板”是由一些文本块和控制逻辑组成的混合模板,它可以生成文本文件。 在 Visual C# 或 Visual Basic 中,控制逻辑编写为程序代码的片段。生成的文件可以是任何类型的文本,例如网页、资源文件或任何语言的程序源代码。现在的VS中只要与代码生成相关的场景基本上都能找T4的身影,比如MVC的视图模板,Entity Framwork的DataContext模板等等。所以B层和D层都是生成好的,不需要我们自己再动手敲了,搭建好的框架如下所示:

        

        好的,接下来,小编就开始着手敲代码了,第一步,我们需要在服务契约里面建立接口,定义方法,代码如下所示:

        

<span style="font-size:18px;">using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.Serialization;using System.ServiceModel;using ITOO.Archives.Contracts;namespace ITOO.Archives.Contracts{    //服务协定定义    [ServiceContract]     public interface IQuery    {        //要公开的服务方法        [OperationContract]        //定义一个查询的方法        List<Query> query();    }}</span>
        第二步:在总结口后面加上逗号和刚才在服务契约里面写的接口,代码如下所示:

<span style="font-size:18px;"><span style="font-size:18px;">using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.Serialization;using System.ServiceModel;namespace ITOO.Archives.Contracts{    //服务协定定义    [ServiceContract]    //在总接口后面写上相应的分接口,具体实现写在分接口    public interface IArchivesService : ITest,IQuery    {    }}</span></span>
       第三步:在服务工厂中添加方法,得到该接口的方法,代码如下所示:

       

<span style="font-size:18px;">using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.Text;using ITOO.Archives.Contracts;namespace ITOO.Archives.Contracts{    public class ServiceFactory    {        private static readonly SortedList<string, IArchivesService> _serviceBusiness = new SortedList<string, IArchivesService>();        #region 基础方法        /// <summary>        /// 获取中心管理系统所有的业务方法        /// </summary>        /// <param name="endpointName"></param>        /// <returns></returns>        public static IArchivesService GetServiceBusiness(string endpointName)        {            IArchivesService iServices;            iServices = CreateWCFInterface(endpointName);            if (_serviceBusiness.ContainsKey(endpointName))            {                iServices = _serviceBusiness[endpointName];            }            else            {                if (true)                {                    iServices = CreateWCFInterface(endpointName);                }                else                {                    //暂时不用                    // iServices = CreateLocalInterface();                }                _serviceBusiness.Add(endpointName, iServices);            }            return iServices;        }               private static IArchivesService CreateWCFInterface(string endpointName)        {            return ServiceProxyFactory.Create<IArchivesService>(endpointName);        }        /// <summary>        ///         /// </summary>        /// <typeparam name="T">接口</typeparam>        /// <returns></returns>        public static T Create<T>()        {            return (T)GetServiceBusiness("Service");        }        /// <summary>        ///         /// </summary>        /// <typeparam name="T">接口</typeparam>        /// <param name="endpointName"></param>        /// <returns></returns>        public static T Create<T>(string endpointName)        {            // WuliuFactory.Create<IWlTool>("wuliu")            return (T)GetServiceBusiness(endpointName);        }        #endregion        #region 实体服务        /// <summary>        /// 学生信息        /// </summary>        /// <returns></returns>        public static ITest GetAllrecordSchoolCensusManage()        {            return GetServiceBusiness("BasicHttpBinding_Services");        }        #endregion        #region 实体服务        /// <summary>        /// 异动表信息        /// </summary>        /// <returns></returns>        public static IQuery GetRecordChange()        {            return GetServiceBusiness("BasicHttpBinding_Services");        }        /// <summary>        /// 档案借阅信息        /// </summary>        /// <returns></returns>        public static ITestTwo GetRecordBorrow()        {            return GetServiceBusiness("BasicHttpBinding_Services");        }        #endregion    }}</span>
        第四步:写数据契约,小编理解的这块就跟我们当时敲三层的时候中的实体层特别相似,代码如下:

        

<span style="font-size:18px;">using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.Serialization;namespace ITOO.Archives.Contracts{    [DataContract]    public class Query    {        //数据契约(DataContract) 服务契约定义了远程访问对象和可供调用的方法,数据契约则是服务端和客户端之间要传送的自定义数据类型,一旦声明一个类型为DataContract,那么该类型就可以被序列化在服务端和客户端之间传送        [DataMember]        public string name { get; set; }        [DataMember]        public string ratity { get; set; }        [DataMember]        public string reason { get; set; }        [DataMember]        public string sex { get; set; }        [DataMember]        public string studentID { get; set; }        [DataMember]        public string time { get; set; }        [DataMember]        public string type { get; set; }            }}</span>
       第五步:在WcfService中创建类实现契约里面的方法,代码如下所示:

       

<span style="font-size:18px;">/************************************************* 作者:丁国华小组:  ArchivesModel说明:测试小例子创建日期:2015年2月6日 14:46:27版本号:1.0**********************************************/using System;using System.Collections.Generic;using System.Linq;using System.Web;using ITOO.Archives.BLL;using ITOO.Archives.Model;using ITOO.Archives.DAL;using ITOO.Library.Core;using ITOO.Archives.Contracts;using ITOO.Archives.IBLL;using ITOO.Archives.BLLFactory;using AutoMapper;namespace ITOO.Archives.WCFService{    public partial class ArchivesService : IQuery    {        /// <summary>        /// 查询档案借阅情况        /// </summary>        /// <returns></returns>        public List<Query> query()        {            List<Query> listQuery = new List<Query>();            //工厂创建B层            IT_recordChangeBLL recBLL = BLLAbstractFactory.GetT_recordChangeBLL();            //创建一个关系            Mapper.CreateMap<T_recordChange, Query>();            //调用底层方法            var result = recBLL.LoadEnities(u => u.name != "0").ToList();            //转换实体(实体的属性名称、数据类型必须一样)           listQuery = Mapper.Map<List<T_recordChange>, List<Query>>(result);           return listQuery;        }    }}</span>
        第六步:这个时候开始着手写前端代码,创建控制器,代码如下所示:

<span style="font-size:18px;">using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using System.Text;using ITOO.Archives.Contracts;namespace ITOO.Archives.Client.Controllers{    public class queryController : Controller     {              //调用ServiceFactory的GetRecordChange方法        private IQuery queryService = ServiceFactory.GetRecordChange();        //        public ActionResult queryIndex()        {            List<Query> listResult = queryService.query();            ViewData["data"] = listResult;            return View();        }    }}</span>
        第七步:添加视图+前端代码,如下所示:

         

<span style="font-size:18px;">@{    }<script type="text/javascript" src="../../Scripts/KongJianJS/KongJianJS.js"></script><script type="text/javascript" src="../../Scripts/KongJianJS/KongJianJS.js"></script><script src="../../Scripts/CourseTypeJS/js/jquery.easyui.min.js" type="text/javascript" ></script><script src="../../Content/jquery-easyui-1.3.2/locale/easyui-lang-zh_CN.js"></script><link rel="stylesheet" type="text/css" href="../../Scripts/CourseTypeJS/js/css/easyui/icon.css"/><link rel="stylesheet" type="text/css" href="../../Scripts/CourseTypeJS/js/css/common/common.css"/><link href="../../Content/index.css" rel="stylesheet" /><link href ="../../CSS/index.css" rel ="stylesheet"/><h2>queryIndex</h2><div>          <table id="dg" class="easyui-datagrid" style="width: 600px; height: 300px" >              <thead>                  <tr>                      <th data-options="field:'name',width:148,sortable:true">ID</th>                      <th data-options="field:'ratity',width:148,sortable:true">姓名</th>                      <th data-options="field:'reason',width:148,sortable:true">性别</th>                      <th data-options="field:'sex',width:148,sortable:true">呵呵</th>                      <th data-options="field:'studentID ',width:148,sortable:true">哈哈</th>                      <th data-options="field:'time',width:148,sortable:true">嘿嘿</th>                      <th data-options="field:'type',width:148,sortable:true">嘻嘻</th>                  </tr>              </thead>              @foreach (ITOO.Archives.Contracts.Query enQuery in ViewData["data"] as List<ITOO.Archives.Contracts.Query>)          {              <tr>                  <td>@enQuery.name </td>                  <td>@enQuery.ratity  </td>                  <td>@enQuery.reason  </td>                  <td>@enQuery.sex  </td>                <td>@enQuery.studentID  </td>                <td>@enQuery.time  </td>                <td>@enQuery.type  </td>            </tr>          }          </table>      </div> </span>
        还有一个特别需要提醒各位小伙伴的是,由于这个不能单个把某个页面设成起始页,那如果我们完成某个小功能的时候,想要看看运行效果怎么办呢?代码设置如下:

<span style="font-size:18px;">using System;using System.Collections.Generic;using System.Data.Entity;using System.Data.Entity.Infrastructure;using System.Linq;using System.Web;using System.Web.Mvc;using System.Web.Routing;namespace ITOO.Archives.Client{    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明,    // 请访问 http://go.microsoft.com/?LinkId=9394801    public class MvcApplication : System.Web.HttpApplication    {        public static void RegisterGlobalFilters(GlobalFilterCollection filters)        {            filters.Add(new HandleErrorAttribute());        }        public static void RegisterRoutes(RouteCollection routes)        {            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");            //routes.MapRoute(            //    "Default", // 路由名称            //    "{controller}/{action}/{id}", // 带有参数的 URL            //    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值            //);            routes.MapRoute(                "Default", // 路由名称                "{controller}/{action}/{id}", // 带有参数的 URL                new { controller = "query", action = "queryIndex", id = UrlParameter.Optional }            );            //routes.MapRoute(            //    "Default", // 路由名称            //    "{controller}/{action}/{id}", // 带有参数的 URL            //    new { controller = "testTwo", action = "testTwoIndex", id = UrlParameter.Optional }            //);        }        protected void Application_Start()        {            AreaRegistration.RegisterAllAreas();            // 默认情况下对 Entity Framework 使用 LocalDB            Database.DefaultConnectionFactory = new SqlConnectionFactory(@"Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True");            RegisterGlobalFilters(GlobalFilters.Filters);            RegisterRoutes(RouteTable.Routes);        }    }}</span>
        至此一个使用TT模板+mvc+wcf实现一个简单查询的例子就完成了,效果如下:

        

        数据库中的数据是小编瞎添加的,不规范,还请各位小伙伴见谅。

        小编寄语:该博文主要跟大家分享一下小编在做项目过程中收获的点点滴滴,对于wcf和mvc的理解还很浅,有待继续学习,有不同意见的小伙伴还请多多指教,利用TT模板和mvc以及wcf实现简单的查询,说实话,刚开始的时候真的不会,又有种那个时候敲机房的感觉,憋了好长时间,终于一条线通了,高兴ing,档案管理项目,未完待续......


           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值