大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > C#技巧 > 自己使用的一个.NET轻量开发结构

自己使用的一个.NET轻量开发结构

关键词:.NET轻量开发结构  阅读(1724) 赞(15)

[摘要]本文是对自己使用的一个.NET轻量开发结构的讲解,对学习C#编程技术有所帮助,与大家分享。

三个文件夹,第一个是放置前端部分,第二个是各种支持的类文件,第三个是单元测试文件。

Core文件类库


放置的是与数据库做交互的文件,以及一些第三方类库,还有与数据库连接的文件

1.Lasy.Validator是一个基于Attribute验证器,我觉得这种验证方式在挺便捷的,具体可以在这里查看到http://www.cnblogs.com/over140/archive/2009/07/04/1515892.html
2.ToJson是一个将数据转换成json格式的字符串的一个类库,很便捷
3.NPinyin是一个将中文转换成拼音的一个类别

app.config是一个连接数据库的配置文件,两个数据库一个是测试用的,一个是调试用的

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="QuyouCore.Properties.Settings.ClubConnectionString"
            connectionString="Data Source=192.168.1.214;Initial Catalog=Club;User ID=sa;Password=123456"
            providerName="System.Data.SqlClient" />
        <add name="QuyouCore.Properties.Settings.ClubTestConnectionString"
            connectionString="Data Source=192.168.1.214;Initial Catalog=Club_Test;User ID=sa;Password=123456" />
    </connectionStrings>
</configuration>

点击core文件夹进入数据库操作

1.DataAccess

DataAccess中主要就是一些数据库的增删改查操作了

Connection.cs是连接数据的文件,主要根据前端文件夹下面的bin文件中的XML文件来操作是连接哪个数据库,用XML就可以很方便的不重新编译直接换数据库

<?xml version="1.0" encoding="utf-8" ?>
<connectionStringToUse>
  <!-- this value can be Local (pc), Production (server), or Test (test)-->
  <environment>Production</environment>
</connectionStringToUse>
 using System;
 using System.Web;
 using System.Xml;
 using QuyouCore.Core.Domain;
 using QuyouCore.Properties;
 
 namespace QuyouCore.Core.DataAccess.Impl
 {
     public class Connection
     {
         public Connection()
         {
             ConnString = GetCurrentConnectionString();
         }
         public ClubDataContext GetClubContext()
         {
             var vcd = new ClubDataContext(ConnString);
             //vcd.Log = new DebuggerWriter();
             return vcd;
         }
         public string GetCurrentConnectionString()
         {
             var connString = string.Empty;
             try
             {
                 var doc = new XmlDocument();
                 doc.Load(HttpContext.Current.Request.PhysicalApplicationPath + "bin/ConnectionStringToUse.xml");
                 var xnl = doc.GetElementsByTagName("environment");
                 var xe = (XmlElement)xnl[0];
                 switch (xe.InnerText.ToLower())
                 {
                     case "local":
                         connString = Settings.Default.ClubConnectionString;
                         break;
                     case "production":
                         connString = Settings.Default.ClubServerConnectionString;
                         break;
                     case "test":
                         connString = Settings.Default.ClubTestConnectionString;
                         break;
                 }
             }
             catch (Exception e)
             {
                 connString = Settings.Default.ClubTestConnectionString;
             }
             return connString;
         }
 
         public string ConnString
         {
             get;
             set;
         }
     }
 }

BaseRepository.cs文件是做一些通用的Linq查询语句,还有就是写Log操作,将各个查询语句记录下来,便于调试,这里用到的Log工具是log4net

using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using QuyouCore.Core.Domain;
using QuyouCore.Core.Enum;
using QuyouCore.Core.Util;
using System;
using System.Linq.Expressions;
namespace QuyouCore.Core.DataAccess.Impl
{
    public class BaseRepository
    {
        protected BaseRepository()
        {
            Conn = new Connection();
            DataContext = new RepositoryContext(Conn.ConnString);
        }
        #region Properties
        /// <summary>
        /// Gets or sets the data context.
        /// </summary>
        /// <value>The data context.</value>
        private RepositoryContext DataContext { get; set; }
        protected Connection Conn { get; set; }
        #endregion

        #region Entity and metadata functions
        /// <summary>
        /// 分页
        /// </summary>
        protected DataPageList<TEntity> GetEntitiesByPage<TEntity>(IQueryable query, int page, int size, DataContext context)
        {
            context.Log = FileLog.Out;
            var data = DataPageList<TEntity>.Create<TEntity>(query, page, size);
            WriteLog(FileLog.FileInfo.ToString());
            return data;
        }
        /// <summary>
        /// 根据条件获取一条数据
        /// </summary>
        protected TEntity GetEntity<TEntity>(Expression<Func<TEntity, bool>> queryFunc) where TEntity : class //Entity
        {
            DataContext.Log = FileLog.Out;
            var results = from entity in DataContext.GetTable<TEntity>()
                          select entity;
            results = results.Where(queryFunc);
            var result = results.FirstOrDefault();
            WriteLog(FileLog.FileInfo.ToString());
            return result;
        }
        /// <summary>
        /// 根据条件获取数据信息列
        /// </summary>
        protected List<TEntity> GetEntities<TEntity>(Expression<Func<TEntity, bool>> queryFunc) where TEntity : class //Entity 
        {
            DataContext.Log = FileLog.Out;
            var query = from entity in DataContext.GetTable<TEntity>()
                          select entity;
            query = query.Where(queryFunc);
            var results = query.ToList();
            WriteLog(FileLog.FileInfo.ToString());
            return results;
        }
        protected List<TEntity> GetEntities<TEntity>(IQueryable<TEntity> query) where TEntity : class //Entity 
        {
            DataContext.Log = FileLog.Out;
            var results = query.ToList();
            WriteLog(FileLog.FileInfo.ToString());
            return results;
        }
        protected IQueryable<TEntity> GetQueryEntities<TEntity>(Expression<Func<TEntity, bool>> queryFunc) where TEntity : class //Entity 
        {
            var query = from entity in DataContext.GetTable<TEntity>()
                        select entity;
            return query.Where(queryFunc);
        }
        /// <summary>
        /// 获取所有数据
        /// </summary>
        protected List<TEntity> GetEntities<TEntity>() where TEntity : class //Entity 
        {
            DataContext.Log = FileLog.Out;
            var query = from entity in DataContext.GetTable<TEntity>()
                          select entity;
            var results = query.ToList();
            WriteLog(FileLog.FileInfo.ToString());
            return results;
        }
        #endregion


        protected void WriteLog(string log)
        {
            WriteLog(log, EnumLog.Level.Info);
        }
        protected void WriteLog(string log, EnumLog.Level level)
        {
            switch (level)
            {
                case EnumLog.Level.Debug: 
                    LogHelper.GetInstance().Debug(log);
                    break;
                case EnumLog.Level.Error:
                    LogHelper.GetInstance().Error(log);
                    break;
                case EnumLog.Level.Warn:
                    LogHelper.GetInstance().Warn(log);
                    break;
                case EnumLog.Level.Fatal:
                    LogHelper.GetInstance().Fatal(log);
                    break;
                case EnumLog.Level.Info:
                    LogHelper.GetInstance().Info(log);
                    break;
                default:
                    LogHelper.GetInstance().Info(log);
                    break;
            }
            //LogHelper.GetInstance().Logger.Repository.Shutdown();
        }
    }
}

下面的代码是一些简单的增删改查,但是一些比较难的SQL语句,有时候比较棘手,你可以使用一个翻译工具,将SQL转换成,叫做Linqer,使用操作也很简单,操作界面如图。

using System.Collections.Generic;
using System.Linq;
using QuyouCore.Core.Domain;

namespace QuyouCore.Core.DataAccess.Impl
{
    public class KeywordRepository : BaseRepository, IKeywordRepository
    {
        public int Insert(tb_Keyword data)
        {
            using (var cd = Conn.GetClubContext())
            {
                cd.tb_Keyword.InsertOnSubmit(data);
                cd.SubmitChanges();
                return data.KeywordTypeId;
            }
        }

        /// <summary>
        /// 根据主键更新表内容
        /// </summary>
        public void Update(tb_Keyword data)
        {
            using (var cd = Conn.GetClubContext())
            {
                var update = cd.tb_Keyword.Single(t => t.KeywordId == data.KeywordId);
                update.KeywordTypeId = data.KeywordTypeId;
                update.Name = data.Name;
                update.Pinyin = data.Pinyin;
                cd.SubmitChanges();
            }
        }

        /// <summary>
        /// 根据编号将信息更新为删除状态
        /// </summary>
        public void Delete(int id)
        {
            using (var cd = Conn.GetClubContext())
            {
                var update = cd.tb_Keyword.Single(t => t.KeywordId == id);
                update.Active = false;
                cd.SubmitChanges();
            }
        }

        public List<tb_Keyword> GetKeywordsByTypeId(tb_Keyword data)
        {
            return GetEntities<tb_Keyword>(
                t => t.Active == data.Active &&
                     t.KeywordTypeId == data.KeywordTypeId
                );
        }
    }
}

2.Domain

Domain文件里面的Club.dml内容是自动生成的

在这里面还放了一些,数据库表格的扩展字段以及一个分页类DataPageList.cs

namespace QuyouCore.Core.Domain
{
    public partial class tb_Keyword
    {
        //public static readonly string TableName = "tb_Keyword";
        //public static readonly string ColumnKeywordId = "KeywordId";
        //public static readonly string ColumnKeywordTypeId = "KeywordTypeId";
        //public static readonly string ColumnName = "Name";
        //public static readonly string ColumnAddTime = "AddTime";
    }
}
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 
 namespace QuyouCore.Core.Domain
 {
     /// <summary>
     /// 数据列表
     /// </summary>
     /// <typeparam name="T">类型</typeparam>
     public class DataPageList<T> : IEnumerable
     {
         #region 成员变量
 
         private System.Linq.IQueryable<T> query;
         private int allRecordCount;
         private int thisPageRecordCount;
         private int pageSize;
         private int pageCount;
         private int indexPage;
         private bool canFirstPage;
         private bool canPrevPage;
         private bool canNextPage;
         private bool canLastpage;
         private List<T> listValue;
 
         #endregion
 
 
         #region 构造函数
 
         //私有构造函数,只能通过内部构造
         private DataPageList(System.Linq.IQueryable<T> Query, int IndexPage, int PageSize)
         {
             query = Query;
             indexPage = IndexPage;
             pageSize = PageSize;
             PageAttrList = new List<PageAttr>();
             Update();
         }
 
         private DataPageList(System.Linq.IQueryable<T> Query)
             : this(Query, 1, 0)
         {
         }
 
         #endregion
 
 
         #region 属性
 
         public int AllRecordCount
         {
             get { return allRecordCount; }
         }
 
         public int ThisPageRecordCount
         {
             get { return thisPageRecordCount; }
         }
 
         public int PageSize
         {
             get { return pageSize; }
             set { pageSize = value; }
         }
 
         public int PageCount
         {
             get { return pageCount; }
         }
 
         public int IndexPage
         {
             get { return indexPage; }
             set { indexPage = value; }
         }
 
         public bool CanFirstPage
         {
             get { return canFirstPage; }
         }
 
         public bool CanPrevPage
         {
             get { return canPrevPage; }
         }
 
         public bool CanNextPage
         {
             get { return canNextPage; }
         }
 
         public bool CanLastPage
         {
             get { return canLastpage; }
         }
 
         public List<T> Value
         {
             get { return listValue; }
         }
 
         public class PageAttr
         {
             public int Page { get; set; }
         }
 
         public List<PageAttr> PageAttrList
         {
             get; set;
         }
         #endregion
 
         #region 索引器
 
         public T this[int index]
         {
             get { return listValue[index]; }
             set { listValue[index] = value; }
         }
 
         #endregion
 
         #region 公开方法
 
         /**/
 
         /// <summary>
         /// 创建自身实例
         /// </summary>
         /// <typeparam name="N">类型,如果此处为匿名类型,请用var 定义实体,且类后的T为object(其实什么也不重要,只是用于可以调用当前类的静态方法)</typeparam>
         /// <param name="Query">Linq查询语句</param>
         /// <param name="type">元素类型</param>
         /// <param name="IndexPage">开始页</param>
         /// <param name="PageSize">每页大小</param>
         /// <returns></returns>
         public static DataPageList<N> Create<N>(System.Linq.IQueryable Query, int IndexPage, int PageSize)
         {
             System.Linq.IQueryable<N> query = (System.Linq.IQueryable<N>) Query;
             return new DataPageList<N>(query, IndexPage, PageSize);
         }
 
         //public static DataPageList<N> Create<N>(System.Linq.IQueryable Query, int IndexPage, int PageSize)
         //{
         //    return Create<N>(Query, IndexPage, PageSize);
         //}
 
         /**/
 
         /// <summary>
         /// 更新数据
         /// </summary>
         public virtual void Update()
         {
             allRecordCount = query.Count();
             PageAttrList.Clear();
             if (pageSize > 0)
             {
                 query = query.Skip((indexPage - 1) * pageSize).Take(pageSize);
                 thisPageRecordCount = query.Count();
                 //计算分页结果
                 pageCount = (allRecordCount%pageSize == 0) ? (allRecordCount/pageSize) : allRecordCount/pageSize + 1;
                 if (indexPage > 1) canFirstPage = true;
                 canPrevPage = true;
                 if (indexPage < pageCount) canNextPage = true;
                 canLastpage = true;
             }
             else
             {
                 thisPageRecordCount = allRecordCount;
                 pageCount = indexPage = 1;
                 canFirstPage = canPrevPage = canNextPage = canLastpage = false;
             }
             for (var cur = 1; cur <= pageCount; cur++)
             {
                 PageAttrList.Add(new PageAttr {Page = cur});
             }
             listValue = query.ToList();
         }
 
         /**/
 
         /// <summary>
         /// 实现枚举接口,不过返回的是object
         /// </summary>
         /// <returns></returns>
         public IEnumerator GetEnumerator()
         {
             return listValue.GetEnumerator();
         }
 
         #endregion
     }
 }

3.Entity

这里面打算放的是一些额外的数据字段,例如一些方法的参数集合、公共的JSON返回格式等

Prompt.cs就是返回JSON格式的

 using System.Collections.Generic;
 using LitJson;
 namespace QuyouCore.Core.Entity
 {
     public class Prompt
     {
         public int Number
         {
             get; set;
         }
 
         public string Msg
         {
             get;
             set;
         }
 
         public static string ToJson(int number, string msg)
         {
             var prompt = new Prompt {Number = number, Msg = msg};
             return JsonMapper.ToJson(prompt);
         }
 
         public static string ToJson<T>(int number, string msg, List<T> data)
         {
             var prompt = new PromptTemplate<T> { Number = number, Msg = msg, DataList = data };
             return JsonMapper.ToJson(prompt);
         }
     }
 }

4.Enum

这里放的是一些数据字典

namespace QuyouCore.Core.Enum
{
    public class EnumLog
    {
        public enum Level
        {
            Fatal = 1,
            Error = 2,
            Warn = 3,
            Debug = 4,
            Info = 5
        }
    }
}

5.Service

这里面放了一些业务逻辑,比如根据条件查询等,因为打算在API、Web、Wap上面共用代码,所以就想把一些通用的逻辑整合在一起,这里用到了一种单例模式,使用的工具是StructureMap

 using System.Collections.Generic;
 using Lasy.Validator;
 using QuyouCore.Core.Entity;
 using QuyouCore.Core.Enum;
 using StructureMap;
 
 namespace QuyouCore.Core.Service.Impl
 {
     public abstract class BaseService
     {
         protected abstract IContainer ConfigureDependencies();//单例配置化
         
         protected string PromptToJson(int prompt)
         {
             return Prompt.ToJson(prompt, EnumCommon.GetPrompt()[prompt]);
         }
 
         protected string PromptToJson<T>(int prompt, List<T> dataList)
         {
             return Prompt.ToJson(prompt, EnumCommon.GetPrompt()[prompt], dataList);
         }
 
         /// <summary>
         /// 用Attribute做字段验证
         /// </summary>
         /// <typeparam name="T"></typeparam>
         /// <param name="dict"></param>
         /// <returns></returns>
         protected string ValidateParameters<T>(Dictionary<string, object> dict)
         {
             var validator = new Validator(typeof(T));
             if (!validator.Validate(dict).BoolResult)
             {
                 return validator.Validate(dict).ErrorStringWithoutFormat;
             }
             return string.Empty;
         }
     }
 }
 using System.Collections.Generic;
 using QuyouCore.Core.DataAccess;
 using QuyouCore.Core.DataAccess.Impl;
 using QuyouCore.Core.Domain;
 using StructureMap;
 using QuyouCore.Core.Util;
 namespace QuyouCore.Core.Service.Impl
 {
     public class KeywordService : BaseService, IKeywordService
     {
         protected override IContainer ConfigureDependencies()
         {
             return new Container(x =>
             {
                 x.For<IKeywordTypeRepository>().Use<KeywordTypeRepository>();
                 x.For<IKeywordRepository>().Use<KeywordRepository>();
             });
         }
         private IKeywordTypeRepository InitKeywordTypeRepository()
         {
             var container = ConfigureDependencies();
             return container.GetInstance<IKeywordTypeRepository>();
         }
         private IKeywordRepository InitKeywordRepository()
         {
             var container = ConfigureDependencies();
             return container.GetInstance<IKeywordRepository>();
         }
 
         public string Insert(tb_Keyword data)
         {
             var keywordRepository = InitKeywordRepository();
             var result = keywordRepository.Insert(data);
             return PromptToJson(result > 0 ? Const.PromptSuccess : Const.PromptFailure);
         }
 
         public string Update(tb_Keyword data)
         {
             var keywordRepository = InitKeywordRepository();
             keywordRepository.Update(data);
             return PromptToJson(Const.PromptSuccess);
         }
 
         public string Delete(int id)
         {
             var keywordRepository = InitKeywordRepository();
             keywordRepository.Delete(id);
             return PromptToJson(Const.PromptSuccess);
         }
 
         public string GetSelectedKeywords(tb_KeywordType data)
         {
             var typeRepository = InitKeywordTypeRepository();
             var keywordRepository = InitKeywordRepository();
 
             data.Active = true;
             data.ParentTypeId = 0;
             //根据线路类型 版块 类别查询出关键字类别
             var allTypes = new List<tb_KeywordType>(); //一级 二级的关键字类别 现在只分到二级 TODO
             var types = typeRepository.GetSelectedTypesByParentId(data);
             foreach (var tbKeywordType in types)
             {
                 allTypes.Add(tbKeywordType);
                 data.ParentTypeId = tbKeywordType.KeywordTypeId;
                 var children = typeRepository.GetSelectedTypesByParentId(data);
                 if (children.Count <= 0) continue;
                 allTypes.AddRange(children);
             }
 
             //查询关键字内容
             //var allKeywords = new List<tb_Keyword>();
             var allKeywords = new List<tb_KeywordType>();
             var condition = new tb_Keyword {Active = true};
             foreach (var tbKeywordType in allTypes)
             {
                 //allKeywords.Add(new tb_Keyword
                 //{
                 //    KeywordTypeName = tbKeywordType.Name,
                 //    IsKeywordType = true,
                 //    Pinyin = string.Empty,
                 //    Name = string.Empty
                 //});
                 condition.KeywordTypeId = tbKeywordType.KeywordTypeId;
                 var keywords = keywordRepository.GetKeywordsByTypeId(condition);
                 if (keywords.Count <= 0)
                 {
                     allKeywords.Add(tbKeywordType);
                     continue;
                 }
                 tbKeywordType.Keywords = keywords;
                 allKeywords.Add(tbKeywordType);
             }
             return PromptToJson(Const.PromptSuccess, allKeywords);
         }
     }
 }

6.Util

主要放些小工具类,比如Log写工具、cookie操作、图片压缩类等

Tests文件类库

健壮的代码是需要有单元测试来做保障的,所以单元测试必不可少。用到的工具是NUnit,测试的点可以发挥大家的想象力。怎么测都行。

 using System;
 using NUnit.Framework;
 using QuyouCore.Core.DataAccess;
 using QuyouCore.Core.DataAccess.Impl;
 using QuyouCore.Core.Domain;
 using StructureMap;
 
 namespace QuyouTests.core.DataAccess
 {
     [TestFixture]
     public class KeywordRepositoryTest
     {
         private IKeywordRepository _keywordModel;
 
         [TestFixtureSetUp]
         public void Init()
         {
             var container = new Container(x => x.For<IKeywordRepository>().Use<KeywordRepository>());
             _keywordModel = container.GetInstance<IKeywordRepository>();
         }
 
         [Test]
         public void TestInsert()
         {
             var data = new tb_Keyword
             {
                 KeywordTypeId = 1,
                 Name = "塔里木",
                 AddTime = DateTime.Now,
                 Pinyin = "talimu",
                 Active = true
             };
             var result = _keywordModel.Insert(data);
             Assert.Greater(result, 0);
         }
 
         [Test]
         public void TestUpdate()
         {
             var data = new tb_Keyword
             {
                 KeywordId = 1,
                 KeywordTypeId = 2,
                 Name = "莫干山",
                 Pinyin = "moganshan"
             };
             _keywordModel.Update(data);
         }
 
         [Test]
         public void TestDelete()
         {
             _keywordModel.Delete(1);
         }
     }
 }

Quyou文件夹

这里就是与页面操作的代码,脚本、CSS等都在里面

Presenter文件夹里面的类是用于操作界面逻辑,以及过滤传过来的数据

 using System;
 using NPinyin;
 using Quyou.admin.Interface;
 using QuyouCore.Core.Domain;
 using QuyouCore.Core.Entity;
 using QuyouCore.Core.Service;
 using QuyouCore.Core.Service.Impl;
 using QuyouCore.Core.Util;
 using StructureMap;
 using System.Collections.Generic;
 
 namespace Quyou.admin.Presenter
 {
     public class KeywordAjaxPresenter : BasePresenter, IKeywordAjaxPresenter
     {
         protected override IContainer ConfigureDependencies()
         {
             return new Container(x =>
             {
                 x.For<IKeywordTypeService>().Use<KeywordTypeService>();
                 x.For<IKeywordService>().Use<KeywordService>();
             });
         }
 
         private IKeywordTypeService InitKeywordTypeService()
         {
             var container = ConfigureDependencies();
             return container.GetInstance<IKeywordTypeService>();
         }
 
         private IKeywordService InitKeywordService()
         {
             var container = ConfigureDependencies();
             return container.GetInstance<IKeywordService>();
         }
 
         private string ValidInsertOrUpdate(KeywordTypetData input)
         {
             input.PlayClass = FilterParamters(input.PlayClass);
             input.PlayModel = FilterParamters(input.PlayModel);
             input.PlayType = FilterParamters(input.PlayType);
             input.Name = FilterParamters(input.Name);
             input.Sort = FilterParamters(input.Sort);
             input.ParentTypeId = FilterParamters(input.ParentTypeId);
             var dict = new Dictionary<string, object>
             {
                 {"PlayClass", input.PlayClass},
                 {"PlayModel", input.PlayModel},
                 {"PlayType", input.PlayType},
                 {"Name", input.Name},
                 {"Sort", input.Sort},
                 {"ParentTypeId", input.ParentTypeId}
             };
             if (!string.IsNullOrEmpty(input.KeywordTypeId))
             {
                 input.KeywordTypeId = FilterParamters(input.KeywordTypeId);
                 dict.Add("KeywordTypeId", input.KeywordTypeId);
             }
             return ValidateParameters<KeywordTypetData>(dict);
         }
         public string InsertKeywordType(KeywordTypetData input)
         {
             var valid = ValidInsertOrUpdate(input);
             if (valid.Length > 0)
                 return valid;
 
             var keywordType = new tb_KeywordType
             {
                 PlayClass = int.Parse(input.PlayClass),
                 PlayModel = int.Parse(input.PlayModel),
                 PlayType = int.Parse(input.PlayType),
                 Name = input.Name,
                 AddTime = DateTime.Now,
                 Sort = int.Parse(input.Sort),
                 ParentTypeId = int.Parse(input.ParentTypeId),
                 Active = true
             };
             
             var keywordTypeService = InitKeywordTypeService();
             return keywordTypeService.Insert(keywordType);
         }
 
         /// <summary>
         /// 修改关键字类别
         /// </summary>
         /// <param name="input"></param>
         /// <returns></returns>
         public string EditKeywordType(KeywordTypetData input)
         {
             var valid = ValidInsertOrUpdate(input);
             if (valid.Length > 0)
                 return valid;
 
             var keywordType = new tb_KeywordType
             {
                 KeywordTypeId = int.Parse(input.KeywordTypeId),
                 PlayClass = int.Parse(input.PlayClass),
                 PlayModel = int.Parse(input.PlayModel),
                 PlayType = int.Parse(input.PlayType),
                 Name = input.Name,
                 Sort = int.Parse(input.Sort),
                 ParentTypeId = int.Parse(input.ParentTypeId)
             };
 
             var keywordTypeService = InitKeywordTypeService();
             return keywordTypeService.Update(keywordType);
         }
 
         /// <summary>
         /// 删除关键字类别
         /// </summary>
         /// <param name="input"></param>
         /// <returns></returns>
         public string DelKeywordType(KeywordTypetData input)
         {
             input.KeywordTypeId = FilterParamters(input.KeywordTypeId);
             var dict = new Dictionary<string, object>
             {
                 {"KeywordTypeId", input.KeywordTypeId}
             };
             var valid = ValidateParameters<KeywordTypetData>(dict);
             if (valid.Length > 0)
                 return valid;
             var keywordTypeService = InitKeywordTypeService();
             return keywordTypeService.Delete(int.Parse(input.KeywordTypeId));
         }
 
         /// <summary>
         /// 添加关键字
         /// </summary>
         /// <param name="input"></param>
         /// <returns></returns>
         public string InsertKeyword(KeywordtData input)
         {
             input.KeywordTypeId = FilterParamters(input.KeywordTypeId);
             input.Name = FilterParamters(input.Name);
 
             var dict = new Dictionary<string, object>
             {
                 {"KeywordTypeId", input.KeywordTypeId},
                 {"Name", input.Name}
             };
             var valid = ValidateParameters<KeywordtData>(dict);
             if (valid.Length > 0)
                 return valid;
 
             var keyword = new tb_Keyword
             {
                 KeywordTypeId = int.Parse(input.KeywordTypeId),
                 Name = input.Name,
                 AddTime = DateTime.Now,
                 Pinyin = Pinyin.GetPinyin(input.Name),
                 Active = true
             };
 
             var keywordService = InitKeywordService();
             return keywordService.Insert(keyword);
         }
 
         /// <summary>
         /// 修改关键字
         /// </summary>
         /// <param name="input"></param>
         /// <returns></returns>
         public string EditKeyword(KeywordtData input)
         {
             input.KeywordId = FilterParamters(input.KeywordId);
             input.KeywordTypeId = FilterParamters(input.KeywordTypeId);
             input.Name = FilterParamters(input.Name);
 
             var dict = new Dictionary<string, object>
             {
                 {"KeywordTypeId", input.KeywordTypeId},
                 {"Name", input.Name},
                 {"KeywordId", input.KeywordId}
             };
             var valid = ValidateParameters<KeywordtData>(dict);
             if (valid.Length > 0)
                 return valid;
 
             var keyword = new tb_Keyword
             {
                 KeywordId = int.Parse(input.KeywordId),
                 KeywordTypeId = int.Parse(input.KeywordTypeId),
                 Name = input.Name,
                 Pinyin = Pinyin.GetPinyin(input.Name),
             };
 
             var keywordService = InitKeywordService();
             return keywordService.Update(keyword);
         }
 
         /// <summary>
         /// 删除关键字
         /// </summary>
         /// <param name="input"></param>
         /// <returns></returns>
         public string DelKeyword(KeywordtData input)
         {
             input.KeywordId = FilterParamters(input.KeywordId);
             var dict = new Dictionary<string, object>
             {
                 {"KeywordId", input.KeywordId}
             };
             var valid = ValidateParameters<KeywordtData>(dict);
             if (valid.Length > 0)
                 return valid;
             var keywordService = InitKeywordService();
             return keywordService.Delete(int.Parse(input.KeywordId));
         }
 
         /// <summary>
         /// 获取关键字列表
         /// </summary>
         /// <param name="input"></param>
         /// <returns></returns>
         public string GetKeywords(KeywordTypetData input)
         {
             int playClass, playModel, playType;
             int.TryParse(input.PlayClass, out playClass);
             int.TryParse(input.PlayModel, out playModel);
             int.TryParse(input.PlayType, out playType);
             var keywordType = new tb_KeywordType
             {
                 PlayClass = playClass,
                 PlayModel = playModel,
                 PlayType = playType
             };
             var keywordService = InitKeywordService();
             return keywordService.GetSelectedKeywords(keywordType);
         }
 
         /// <summary>
         /// 显示错误信息的JSON提示
         /// </summary>
         /// <returns></returns>
         public string ShowErrorJson()
         {
             return PromptToJson(Const.PromptFailure);
         }
     }
 }

Linqer下载:

Linqer.rar

结构可以在这里下载,代码都是不能运行的,主要就是展示下结构



相关评论