发布一个Codesmith 模版,生成Linq 增删除改。看看有什么问题,请大家指点
这些只是个人总结来写在的模版。请大家指点。 暂时我把它 生成的代码放到 BLL层,也不知道这样子分层合不合适。
<%@ CodeTemplate Language="C#" ResponseEncoding="UTF-8" TargetLanguage="Text" Src="" Inherits="" Debug="False" Description="Template description here." %>
<%@ Property Name="NameSpaceOfModel" Type="System.String" Default="ZdSoft.Models" Category="Object" Description="NameSpace of Models" %>
<%@ Property Name="NameSpaceOfBll" Type="System.String" Default="ZdSoft.Bll" Category="Object" Description="NameSpace of Bll" %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the mapping file is based on" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Property Name="RemoveTablePrefix" Type="System.String" Default="Tb" Category="Object" Description="The prefix to remove from table names" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
/*****************************************************************
// Copyright (C) 2011-2099 ZdSoft Corporation
// All rights reserved.
//
// Author: <%= Author %>
// Create Date: <%= DateTime.Now.ToString() %>
// Usage: <%= Description %>
//
// RevisionHistory
// Date Author Description
*****************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using <%=NameSpaceOfModel%>;
namespace <%=NameSpaceOfBll%>
{
public class <%=SourceTable.Name.Replace(RemoveTablePrefix,"")%>
{
public EntitiesDataContext Entities = new EntitiesDataContext();
/// <summary>
/// 还回所有行
/// </summary>
/// <returns></returns>
public IQueryable<<%=SourceTable.Name%>> GetAll()
{
return Entities.<%=SourceTable.Name%>;
}
/// <summary>
/// 根据主键还回实体
/// </summary>
/// <param name="id">主键</param>
public <%=SourceTable.Name%> GetEntityById(string id)
{
return Entities.<%=SourceTable.Name%>.FirstOrDefault(o => o.<%=SourceTable.PrimaryKey.MemberColumns[0].Name%>.Equals(id));
}
/// <summary>
/// 添加实体
/// </summary>
/// <param name="entity">实体</param>
public void Add(<%=SourceTable.Name%> entity)
{
Entities.<%=SourceTable.Name%>.InsertOnSubmit(entity);
}
/// <summary>
/// 删除实体
/// </summary>
/// <param name="entity">实体</param>
public void Delete(<%=SourceTable.Name%> entity)
{
Entities.<%=SourceTable.Name%>.DeleteOnSubmit(entity);
}
/// <summary>
/// 根据主键批量删除
/// </summary>
/// <param name="primaryKey">主键数组</param>
public void Delete(string[] primaryKey)
{
if (primaryKey == null || primaryKey.Length <= 0) return;
var entities = from o in Entities.<%=SourceTable.Name%>
where primaryKey.Contains(o.<%=SourceTable.PrimaryKey.MemberColumns[0].Name%>)
select o;
Entities.<%=SourceTable.Name%>.DeleteAllOnSubmit(entities);
}
/// <summary>
/// 根据传入实体批量删除
/// </summary>
/// <param name="entities">实体列表</param>
public void Delete(List<<%=SourceTable.Name%>> entities)
{
if(entities==null||entities.Count==0) return;
Entities.<%=SourceTable.Name%>.DeleteAllOnSubmit(entities);
}
/// <summary>
/// 保存修改(新增、删除、修改)
/// </summary>
public void Save()
{
Entities.SubmitChanges();
}
}
}
<script runat="template">
public string Author="wunaigong";
public string Description="业务逻辑 数据的增删除改查";
private DatabaseSchema _sourceDatabase;
[Category("Database")]
[Description("Database that the mapping file should be based on.")]
//public DatabaseSchema SourceDatabase {
// get { return _sourceDatabase; }
// set { _sourceDatabase = value; }
// }
//获取数据库字段的描述 column.Name
public string GetColumnDes(ColumnSchema column)
{
if(column.Description.Trim().ToString()!=string.Empty)
{
return column.Description.Trim().ToString();
}else
return column.Name.ToString().Replace("_","");
}
//C#还回数据类型
public string CSharpType(ColumnSchema column)
{
if (column.Name.EndsWith("TypeCode")) return column.Name;
switch (column.DataType)
{
case DbType.AnsiString: return "string";
case DbType.AnsiStringFixedLength: return "string";
case DbType.Binary: return "byte[]";
case DbType.Boolean: return "bool";
case DbType.Byte: return "byte";
case DbType.Currency: return "decimal";
case DbType.Date: return "DateTime";
case DbType.DateTime: return "DateTime";
case DbType.Decimal: return "decimal";
case DbType.Double: return "double";
case DbType.Guid: return "Guid";
case DbType.Int16: return "short";
case DbType.Int32: return "int";
case DbType.Int64: return "long";
case DbType.Object: return "object";
case DbType.SByte: return "sbyte";
case DbType.Single: return "float";
case DbType.String: return "string";
case DbType.StringFixedLength: return "string";
case DbType.Time: return "TimeSpan";
case DbType.UInt16: return "ushort";
case DbType.UInt32: return "uint";
case DbType.UInt64: return "ulong";
case DbType.VarNumeric: return "decimal";
default:
{
return "__UNKNOWN__" + column.NativeType;
}
}
}
</script>