alexmen

专注.net软件开发,项目管理体系PMBOK.

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

  本生成利用 RazorEngine 引擎 通过模板定义 生成实体和服务代码

  1.  模板设定

      实体模板

   

@model CodeGenerator.RazorBuilder.Templates.TemplateModel


using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using PDA.CustomAttribute;

namespace @(Model.NameSpace).Model
{

    /// <summary>
    /// 
    ///</summary>
    [Table("@(Model.TableName)")]
    public  class @(Model.TableName)
    {
    @foreach (var item in Model.ColumnList)
    {
        if (item.PK == 1)
        {
            @("        [Key]\r\n")

        }
        @("        public " + item.Type + " " + item.Name + " { get; set; }\r\n\r\n")
     }
    }
}

服务模板

@model CodeGenerator.RazorBuilder.Templates.TemplateModel


using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using PDA.DB;
using PDA.Model;
using PDA.IServices;


namespace @(Model.NameSpace).Services
{

    /// <summary>
    ///
    ///</summary>
    public class @(Model.TableName)Services:I@(Model.TableName)Services
    {
        public  int Insert(@(Model.TableName) model)
        {
             return SqlExtensions.Insert(model);
        }
        public  bool Update(@(Model.TableName) model)
        {
             return SqlExtensions.Update(model);
        }
        public  bool Delete(@(Model.TableName) model)
        {
             return SqlExtensions.Delete(model);
        }
        public  bool Delete(string key)
        {
            @(Model.TableName) model=Get(key);
           return SqlExtensions.Delete(model);
        }

        public  bool DeleteAll()
        {
           return SqlExtensions.DeleteAll<@(Model.TableName)>();
        }

        public  @(Model.TableName) Get(string key)
        {
            return SqlExtensions.Get<@(Model.TableName)>(key);
        }
        public  IList<@(Model.TableName)> Query(string whereCluase)
        {
            return SqlExtensions.Query<@(Model.TableName)>(whereCluase);
        }

    }


}

2.生成模板类

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using CodeGenerator.RazorBuilder.Templates;
using CodeGenerator.Sqlite;
using RazorEngine.Configuration;
using RazorEngine.Templating;

namespace CodeGenerator.RazorBuilder
{
    /// <summary>
    ///     生成模板
    /// </summary>
    public class BuilderTemplate
    {
        public static string Create(string nameSpace, string tableName, string templateName, DataTable dtColumns = null)
        {
            List<ColumnsModel> list = null;
            if (dtColumns != null)
            {
                list = ConvertList<ColumnsModel>(dtColumns);
                list.ForEach(c =>
                {
                    c.Type = DbTypeMap.FindType(c.Type);
                });
            }
            var model = new TemplateModel { ColumnList = list, NameSpace = nameSpace, TableName = tableName };
            var rootType = typeof(BuilderTemplate);

            return Render(rootType, templateName, model);
        }


        /// <summary>
        /// 模板读取方法
        /// </summary>
        /// <param name="rootType"></param>
        /// <param name="templateName"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        private static string Render(Type rootType, string templateName, object model)
        {
            var config = new TemplateServiceConfiguration
            {
                TemplateManager = new EmbeddedResourceTemplateManager(rootType)
            };
            var service = RazorEngineService.Create(config);
            return service.RunCompile(templateName, rootType, model);
        }


        public static List<T> ConvertList<T>(DataTable dt) where T : class
        {
            var list = new List<T>();
            dt.AsEnumerable().ToList().ForEach(r =>
            {
                var model = Activator.CreateInstance<T>();
                var properties = model.GetType().GetProperties().ToList();
                properties.ForEach(p =>
                {
                    if (!string.Equals(dt.Columns[p.Name].ColumnName, p.Name, StringComparison.CurrentCultureIgnoreCase))
                        return;
                    p.SetValue(model, r[p.Name] != DBNull.Value ? r[p.Name] : null, null);
                });
                list.Add(model);
            });

            return list;
        }
    }
}

3.运行效果图

 

项目源码下载地址:http://download.csdn.net/detail/shulei521/9593548

另附一个支持多种类型的数据库代码生成器源码: http://download.csdn.net/detail/shulei521/9408766

posted on 2016-08-03 11:19  alexmen  阅读(1980)  评论(0编辑  收藏  举报