程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 自己使用的一個.NET輕量開發結構

自己使用的一個.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>
 1 using System;
 2 using System.Web;
 3 using System.Xml;
 4 using QuyouCore.Core.Domain;
 5 using QuyouCore.Properties;
 6 
 7 namespace QuyouCore.Core.DataAccess.Impl
 8 {
 9     public class Connection
10     {
11         public Connection()
12         {
13             ConnString = GetCurrentConnectionString();
14         }
15         public ClubDataContext GetClubContext()
16         {
17             var vcd = new ClubDataContext(ConnString);
18             //vcd.Log = new DebuggerWriter();
19             return vcd;
20         }
21         public string GetCurrentConnectionString()
22         {
23             var connString = string.Empty;
24             try
25             {
26                 var doc = new XmlDocument();
27                 doc.Load(HttpContext.Current.Request.PhysicalApplicationPath + "bin/ConnectionStringToUse.xml");
28                 var xnl = doc.GetElementsByTagName("environment");
29                 var xe = (XmlElement)xnl[0];
30                 switch (xe.InnerText.ToLower())
31                 {
32                     case "local":
33                         connString = Settings.Default.ClubConnectionString;
34                         break;
35                     case "production":
36                         connString = Settings.Default.ClubServerConnectionString;
37                         break;
38                     case "test":
39                         connString = Settings.Default.ClubTestConnectionString;
40                         break;
41                 }
42             }
43             catch (Exception e)
44             {
45                 connString = Settings.Default.ClubTestConnectionString;
46             }
47             return connString;
48         }
49 
50         public string ConnString
51         {
52             get;
53             set;
54         }
55     }
56 }

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";
    }
}
  1 using System.Collections;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 
  5 namespace QuyouCore.Core.Domain
  6 {
  7     /// <summary>
  8     /// 數據列表
  9     /// </summary>
 10     /// <typeparam name="T">類型</typeparam>
 11     public class DataPageList<T> : IEnumerable
 12     {
 13         #region 成員變量
 14 
 15         private System.Linq.IQueryable<T> query;
 16         private int allRecordCount;
 17         private int thisPageRecordCount;
 18         private int pageSize;
 19         private int pageCount;
 20         private int indexPage;
 21         private bool canFirstPage;
 22         private bool canPrevPage;
 23         private bool canNextPage;
 24         private bool canLastpage;
 25         private List<T> listValue;
 26 
 27         #endregion
 28 
 29 
 30         #region 構造函數
 31 
 32         //私有構造函數,只能通過內部構造
 33         private DataPageList(System.Linq.IQueryable<T> Query, int IndexPage, int PageSize)
 34         {
 35             query = Query;
 36             indexPage = IndexPage;
 37             pageSize = PageSize;
 38             PageAttrList = new List<PageAttr>();
 39             Update();
 40         }
 41 
 42         private DataPageList(System.Linq.IQueryable<T> Query)
 43             : this(Query, 1, 0)
 44         {
 45         }
 46 
 47         #endregion
 48 
 49 
 50         #region 屬性
 51 
 52         public int AllRecordCount
 53         {
 54             get { return allRecordCount; }
 55         }
 56 
 57         public int ThisPageRecordCount
 58         {
 59             get { return thisPageRecordCount; }
 60         }
 61 
 62         public int PageSize
 63         {
 64             get { return pageSize; }
 65             set { pageSize = value; }
 66         }
 67 
 68         public int PageCount
 69         {
 70             get { return pageCount; }
 71         }
 72 
 73         public int IndexPage
 74         {
 75             get { return indexPage; }
 76             set { indexPage = value; }
 77         }
 78 
 79         public bool CanFirstPage
 80         {
 81             get { return canFirstPage; }
 82         }
 83 
 84         public bool CanPrevPage
 85         {
 86             get { return canPrevPage; }
 87         }
 88 
 89         public bool CanNextPage
 90         {
 91             get { return canNextPage; }
 92         }
 93 
 94         public bool CanLastPage
 95         {
 96             get { return canLastpage; }
 97         }
 98 
 99         public List<T> Value
100         {
101             get { return listValue; }
102         }
103 
104         public class PageAttr
105         {
106             public int Page { get; set; }
107         }
108 
109         public List<PageAttr> PageAttrList
110         {
111             get; set;
112         }
113         #endregion
114 
115         #region 索引器
116 
117         public T this[int index]
118         {
119             get { return listValue[index]; }
120             set { listValue[index] = value; }
121         }
122 
123         #endregion
124 
125         #region 公開方法
126 
127         /**/
128 
129         /// <summary>
130         /// 創建自身實例
131         /// </summary>
132         /// <typeparam name="N">類型,如果此處為匿名類型,請用var 定義實體,且類後的T為object(其實什麼也不重要,只是用於可以調用當前類的靜態方法)</typeparam>
133         /// <param name="Query">Linq查詢語句</param>
134         /// <param name="type">元素類型</param>
135         /// <param name="IndexPage">開始頁</param>
136         /// <param name="PageSize">每頁大小</param>
137         /// <returns></returns>
138         public static DataPageList<N> Create<N>(System.Linq.IQueryable Query, int IndexPage, int PageSize)
139         {
140             System.Linq.IQueryable<N> query = (System.Linq.IQueryable<N>) Query;
141             return new DataPageList<N>(query, IndexPage, PageSize);
142         }
143 
144         //public static DataPageList<N> Create<N>(System.Linq.IQueryable Query, int IndexPage, int PageSize)
145         //{
146         //    return Create<N>(Query, IndexPage, PageSize);
147         //}
148 
149         /**/
150 
151         /// <summary>
152         /// 更新數據
153         /// </summary>
154         public virtual void Update()
155         {
156             allRecordCount = query.Count();
157             PageAttrList.Clear();
158             if (pageSize > 0)
159             {
160                 query = query.Skip((indexPage - 1) * pageSize).Take(pageSize);
161                 thisPageRecordCount = query.Count();
162                 //計算分頁結果
163                 pageCount = (allRecordCount%pageSize == 0) ? (allRecordCount/pageSize) : allRecordCount/pageSize + 1;
164                 if (indexPage > 1) canFirstPage = true;
165                 canPrevPage = true;
166                 if (indexPage < pageCount) canNextPage = true;
167                 canLastpage = true;
168             }
169             else
170             {
171                 thisPageRecordCount = allRecordCount;
172                 pageCount = indexPage = 1;
173                 canFirstPage = canPrevPage = canNextPage = canLastpage = false;
174             }
175             for (var cur = 1; cur <= pageCount; cur++)
176             {
177                 PageAttrList.Add(new PageAttr {Page = cur});
178             }
179             listValue = query.ToList();
180         }
181 
182         /**/
183 
184         /// <summary>
185         /// 實現枚舉接口,不過返回的是object
186         /// </summary>
187         /// <returns></returns>
188         public IEnumerator GetEnumerator()
189         {
190             return listValue.GetEnumerator();
191         }
192 
193         #endregion
194     }
195 }

 

3.Entity

這裡面打算放的是一些額外的數據字段,例如一些方法的參數集合、公共的JSON返回格式等

Prompt.cs就是返回JSON格式的

 1 using System.Collections.Generic;
 2 using LitJson;
 3 namespace QuyouCore.Core.Entity
 4 {
 5     public class Prompt
 6     {
 7         public int Number
 8         {
 9             get; set;
10         }
11 
12         public string Msg
13         {
14             get;
15             set;
16         }
17 
18         public static string ToJson(int number, string msg)
19         {
20             var prompt = new Prompt {Number = number, Msg = msg};
21             return JsonMapper.ToJson(prompt);
22         }
23 
24         public static string ToJson<T>(int number, string msg, List<T> data)
25         {
26             var prompt = new PromptTemplate<T> { Number = number, Msg = msg, DataList = data };
27             return JsonMapper.ToJson(prompt);
28         }
29     }
30 }

 

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

 1 using System.Collections.Generic;
 2 using Lasy.Validator;
 3 using QuyouCore.Core.Entity;
 4 using QuyouCore.Core.Enum;
 5 using StructureMap;
 6 
 7 namespace QuyouCore.Core.Service.Impl
 8 {
 9     public abstract class BaseService
10     {
11         protected abstract IContainer ConfigureDependencies();//單例配置化
12         
13         protected string PromptToJson(int prompt)
14         {
15             return Prompt.ToJson(prompt, EnumCommon.GetPrompt()[prompt]);
16         }
17 
18         protected string PromptToJson<T>(int prompt, List<T> dataList)
19         {
20             return Prompt.ToJson(prompt, EnumCommon.GetPrompt()[prompt], dataList);
21         }
22 
23         /// <summary>
24         /// 用Attribute做字段驗證
25         /// </summary>
26         /// <typeparam name="T"></typeparam>
27         /// <param name="dict"></param>
28         /// <returns></returns>
29         protected string ValidateParameters<T>(Dictionary<string, object> dict)
30         {
31             var validator = new Validator(typeof(T));
32             if (!validator.Validate(dict).BoolResult)
33             {
34                 return validator.Validate(dict).ErrorStringWithoutFormat;
35             }
36             return string.Empty;
37         }
38     }
39 }
 1 using System.Collections.Generic;
 2 using QuyouCore.Core.DataAccess;
 3 using QuyouCore.Core.DataAccess.Impl;
 4 using QuyouCore.Core.Domain;
 5 using StructureMap;
 6 using QuyouCore.Core.Util;
 7 namespace QuyouCore.Core.Service.Impl
 8 {
 9     public class KeywordService : BaseService, IKeywordService
10     {
11         protected override IContainer ConfigureDependencies()
12         {
13             return new Container(x =>
14             {
15                 x.For<IKeywordTypeRepository>().Use<KeywordTypeRepository>();
16                 x.For<IKeywordRepository>().Use<KeywordRepository>();
17             });
18         }
19         private IKeywordTypeRepository InitKeywordTypeRepository()
20         {
21             var container = ConfigureDependencies();
22             return container.GetInstance<IKeywordTypeRepository>();
23         }
24         private IKeywordRepository InitKeywordRepository()
25         {
26             var container = ConfigureDependencies();
27             return container.GetInstance<IKeywordRepository>();
28         }
29 
30         public string Insert(tb_Keyword data)
31         {
32             var keywordRepository = InitKeywordRepository();
33             var result = keywordRepository.Insert(data);
34             return PromptToJson(result > 0 ? Const.PromptSuccess : Const.PromptFailure);
35         }
36 
37         public string Update(tb_Keyword data)
38         {
39             var keywordRepository = InitKeywordRepository();
40             keywordRepository.Update(data);
41             return PromptToJson(Const.PromptSuccess);
42         }
43 
44         public string Delete(int id)
45         {
46             var keywordRepository = InitKeywordRepository();
47             keywordRepository.Delete(id);
48             return PromptToJson(Const.PromptSuccess);
49         }
50 
51         public string GetSelectedKeywords(tb_KeywordType data)
52         {
53             var typeRepository = InitKeywordTypeRepository();
54             var keywordRepository = InitKeywordRepository();
55 
56             data.Active = true;
57             data.ParentTypeId = 0;
58             //根據線路類型 版塊 類別查詢出關鍵字類別
59             var allTypes = new List<tb_KeywordType>(); //一級 二級的關鍵字類別 現在只分到二級 TODO
60             var types = typeRepository.GetSelectedTypesByParentId(data);
61             foreach (var tbKeywordType in types)
62             {
63                 allTypes.Add(tbKeywordType);
64                 data.ParentTypeId = tbKeywordType.KeywordTypeId;
65                 var children = typeRepository.GetSelectedTypesByParentId(data);
66                 if (children.Count <= 0) continue;
67                 allTypes.AddRange(children);
68             }
69 
70             //查詢關鍵字內容
71             //var allKeywords = new List<tb_Keyword>();
72             var allKeywords = new List<tb_KeywordType>();
73             var condition = new tb_Keyword {Active = true};
74             foreach (var tbKeywordType in allTypes)
75             {
76                 //allKeywords.Add(new tb_Keyword
77                 //{
78                 //    KeywordTypeName = tbKeywordType.Name,
79                 //    IsKeywordType = true,
80                 //    Pinyin = string.Empty,
81                 //    Name = string.Empty
82                 //});
83                 condition.KeywordTypeId = tbKeywordType.KeywordTypeId;
84                 var keywords = keywordRepository.GetKeywordsByTypeId(condition);
85                 if (keywords.Count <= 0)
86                 {
87                     allKeywords.Add(tbKeywordType);
88                     continue;
89                 }
90                 tbKeywordType.Keywords = keywords;
91                 allKeywords.Add(tbKeywordType);
92             }
93             return PromptToJson(Const.PromptSuccess, allKeywords);
94         }
95     }
96 }

 

6.Util

主要放些小工具類,比如Log寫工具、cookie操作、圖片壓縮類等

 

Tests文件類庫

健壯的代碼是需要有單元測試來做保障的,所以單元測試必不可少。用到的工具是NUnit,測試的點可以發揮大家的想象力。怎麼測都行。

 

 1 using System;
 2 using NUnit.Framework;
 3 using QuyouCore.Core.DataAccess;
 4 using QuyouCore.Core.DataAccess.Impl;
 5 using QuyouCore.Core.Domain;
 6 using StructureMap;
 7 
 8 namespace QuyouTests.core.DataAccess
 9 {
10     [TestFixture]
11     public class KeywordRepositoryTest
12     {
13         private IKeywordRepository _keywordModel;
14 
15         [TestFixtureSetUp]
16         public void Init()
17         {
18             var container = new Container(x => x.For<IKeywordRepository>().Use<KeywordRepository>());
19             _keywordModel = container.GetInstance<IKeywordRepository>();
20         }
21 
22         [Test]
23         public void TestInsert()
24         {
25             var data = new tb_Keyword
26             {
27                 KeywordTypeId = 1,
28                 Name = "塔裡木",
29                 AddTime = DateTime.Now,
30                 Pinyin = "talimu",
31                 Active = true
32             };
33             var result = _keywordModel.Insert(data);
34             Assert.Greater(result, 0);
35         }
36 
37         [Test]
38         public void TestUpdate()
39         {
40             var data = new tb_Keyword
41             {
42                 KeywordId = 1,
43                 KeywordTypeId = 2,
44                 Name = "莫干山",
45                 Pinyin = "moganshan"
46             };
47             _keywordModel.Update(data);
48         }
49 
50         [Test]
51         public void TestDelete()
52         {
53             _keywordModel.Delete(1);
54         }
55     }
56 }

 

Quyou文件夾

這裡就是與頁面操作的代碼,腳本、CSS等都在裡面

Presenter文件夾裡面的類是用於操作界面邏輯,以及過濾傳過來的數據

  1 using System;
  2 using NPinyin;
  3 using Quyou.admin.Interface;
  4 using QuyouCore.Core.Domain;
  5 using QuyouCore.Core.Entity;
  6 using QuyouCore.Core.Service;
  7 using QuyouCore.Core.Service.Impl;
  8 using QuyouCore.Core.Util;
  9 using StructureMap;
 10 using System.Collections.Generic;
 11 
 12 namespace Quyou.admin.Presenter
 13 {
 14     public class KeywordAjaxPresenter : BasePresenter, IKeywordAjaxPresenter
 15     {
 16         protected override IContainer ConfigureDependencies()
 17         {
 18             return new Container(x =>
 19             {
 20                 x.For<IKeywordTypeService>().Use<KeywordTypeService>();
 21                 x.For<IKeywordService>().Use<KeywordService>();
 22             });
 23         }
 24 
 25         private IKeywordTypeService InitKeywordTypeService()
 26         {
 27             var container = ConfigureDependencies();
 28             return container.GetInstance<IKeywordTypeService>();
 29         }
 30 
 31         private IKeywordService InitKeywordService()
 32         {
 33             var container = ConfigureDependencies();
 34             return container.GetInstance<IKeywordService>();
 35         }
 36 
 37         private string ValidInsertOrUpdate(KeywordTypetData input)
 38         {
 39             input.PlayClass = FilterParamters(input.PlayClass);
 40             input.PlayModel = FilterParamters(input.PlayModel);
 41             input.PlayType = FilterParamters(input.PlayType);
 42             input.Name = FilterParamters(input.Name);
 43             input.Sort = FilterParamters(input.Sort);
 44             input.ParentTypeId = FilterParamters(input.ParentTypeId);
 45             var dict = new Dictionary<string, object>
 46             {
 47                 {"PlayClass", input.PlayClass},
 48                 {"PlayModel", input.PlayModel},
 49                 {"PlayType", input.PlayType},
 50                 {"Name", input.Name},
 51                 {"Sort", input.Sort},
 52                 {"ParentTypeId", input.ParentTypeId}
 53             };
 54             if (!string.IsNullOrEmpty(input.KeywordTypeId))
 55             {
 56                 input.KeywordTypeId = FilterParamters(input.KeywordTypeId);
 57                 dict.Add("KeywordTypeId", input.KeywordTypeId);
 58             }
 59             return ValidateParameters<KeywordTypetData>(dict);
 60         }
 61         public string InsertKeywordType(KeywordTypetData input)
 62         {
 63             var valid = ValidInsertOrUpdate(input);
 64             if (valid.Length > 0)
 65                 return valid;
 66 
 67             var keywordType = new tb_KeywordType
 68             {
 69                 PlayClass = int.Parse(input.PlayClass),
 70                 PlayModel = int.Parse(input.PlayModel),
 71                 PlayType = int.Parse(input.PlayType),
 72                 Name = input.Name,
 73                 AddTime = DateTime.Now,
 74                 Sort = int.Parse(input.Sort),
 75                 ParentTypeId = int.Parse(input.ParentTypeId),
 76                 Active = true
 77             };
 78             
 79             var keywordTypeService = InitKeywordTypeService();
 80             return keywordTypeService.Insert(keywordType);
 81         }
 82 
 83         /// <summary>
 84         /// 修改關鍵字類別
 85         /// </summary>
 86         /// <param name="input"></param>
 87         /// <returns></returns>
 88         public string EditKeywordType(KeywordTypetData input)
 89         {
 90             var valid = ValidInsertOrUpdate(input);
 91             if (valid.Length > 0)
 92                 return valid;
 93 
 94             var keywordType = new tb_KeywordType
 95             {
 96                 KeywordTypeId = int.Parse(input.KeywordTypeId),
 97                 PlayClass = int.Parse(input.PlayClass),
 98                 PlayModel = int.Parse(input.PlayModel),
 99                 PlayType = int.Parse(input.PlayType),
100                 Name = input.Name,
101                 Sort = int.Parse(input.Sort),
102                 ParentTypeId = int.Parse(input.ParentTypeId)
103             };
104 
105             var keywordTypeService = InitKeywordTypeService();
106             return keywordTypeService.Update(keywordType);
107         }
108 
109         /// <summary>
110         /// 刪除關鍵字類別
111         /// </summary>
112         /// <param name="input"></param>
113         /// <returns></returns>
114         public string DelKeywordType(KeywordTypetData input)
115         {
116             input.KeywordTypeId = FilterParamters(input.KeywordTypeId);
117             var dict = new Dictionary<string, object>
118             {
119                 {"KeywordTypeId", input.KeywordTypeId}
120             };
121             var valid = ValidateParameters<KeywordTypetData>(dict);
122             if (valid.Length > 0)
123                 return valid;
124             var keywordTypeService = InitKeywordTypeService();
125             return keywordTypeService.Delete(int.Parse(input.KeywordTypeId));
126         }
127 
128         /// <summary>
129         /// 添加關鍵字
130         /// </summary>
131         /// <param name="input"></param>
132         /// <returns></returns>
133         public string InsertKeyword(KeywordtData input)
134         {
135             input.KeywordTypeId = FilterParamters(input.KeywordTypeId);
136             input.Name = FilterParamters(input.Name);
137 
138             var dict = new Dictionary<string, object>
139             {
140                 {"KeywordTypeId", input.KeywordTypeId},
141                 {"Name", input.Name}
142             };
143             var valid = ValidateParameters<KeywordtData>(dict);
144             if (valid.Length > 0)
145                 return valid;
146 
147             var keyword = new tb_Keyword
148             {
149                 KeywordTypeId = int.Parse(input.KeywordTypeId),
150                 Name = input.Name,
151                 AddTime = DateTime.Now,
152                 Pinyin = Pinyin.GetPinyin(input.Name),
153                 Active = true
154             };
155 
156             var keywordService = InitKeywordService();
157             return keywordService.Insert(keyword);
158         }
159 
160         /// <summary>
161         /// 修改關鍵字
162         /// </summary>
163         /// <param name="input"></param>
164         /// <returns></returns>
165         public string EditKeyword(KeywordtData input)
166         {
167             input.KeywordId = FilterParamters(input.KeywordId);
168             input.KeywordTypeId = FilterParamters(input.KeywordTypeId);
169             input.Name = FilterParamters(input.Name);
170 
171             var dict = new Dictionary<string, object>
172             {
173                 {"KeywordTypeId", input.KeywordTypeId},
174                 {"Name", input.Name},
175                 {"KeywordId", input.KeywordId}
176             };
177             var valid = ValidateParameters<KeywordtData>(dict);
178             if (valid.Length > 0)
179                 return valid;
180 
181             var keyword = new tb_Keyword
182             {
183                 KeywordId = int.Parse(input.KeywordId),
184                 KeywordTypeId = int.Parse(input.KeywordTypeId),
185                 Name = input.Name,
186                 Pinyin = Pinyin.GetPinyin(input.Name),
187             };
188 
189             var keywordService = InitKeywordService();
190             return keywordService.Update(keyword);
191         }
192 
193         /// <summary>
194         /// 刪除關鍵字
195         /// </summary>
196         /// <param name="input"></param>
197         /// <returns></returns>
198         public string DelKeyword(KeywordtData input)
199         {
200             input.KeywordId = FilterParamters(input.KeywordId);
201             var dict = new Dictionary<string, object>
202             {
203                 {"KeywordId", input.KeywordId}
204             };
205             var valid = ValidateParameters<KeywordtData>(dict);
206             if (valid.Length > 0)
207                 return valid;
208             var keywordService = InitKeywordService();
209             return keywordService.Delete(int.Parse(input.KeywordId));
210         }
211 
212         /// <summary>
213         /// 獲取關鍵字列表
214         /// </summary>
215         /// <param name="input"></param>
216         /// <returns></returns>
217         public string GetKeywords(KeywordTypetData input)
218         {
219             int playClass, playModel, playType;
220             int.TryParse(input.PlayClass, out playClass);
221             int.TryParse(input.PlayModel, out playModel);
222             int.TryParse(input.PlayType, out playType);
223             var keywordType = new tb_KeywordType
224             {
225                 PlayClass = playClass,
226                 PlayModel = playModel,
227                 PlayType = playType
228             };
229             var keywordService = InitKeywordService();
230             return keywordService.GetSelectedKeywords(keywordType);
231         }
232 
233         /// <summary>
234         /// 顯示錯誤信息的JSON提示
235         /// </summary>
236         /// <returns></returns>
237         public string ShowErrorJson()
238         {
239             return PromptToJson(Const.PromptFailure);
240         }
241     }
242 }

 

 

 

Linqer下載:

Linqer.rar

結構可以在這裡下載,代碼都是不能運行的,主要就是展示下結構

演示結構.rar

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved