接着上文章的内容,在日志和json都简单封装的情况下
我们开始对Models层进行封装
Models主要存放数据库的上下文Orm和不同的实体类(PocoModel,ExtendModel)
Models层
上下文的操作,因为小型控制台项目,不会涉及大的业务分层,所以选取SqlSugar做为Orm 首选,它具有小巧,轻便,可以使用Lambda表达式进行CURD操作,和EntityFramework有异曲同工之妙,以前项目大多用EF来做,后来接触SqlSugar之后 ,感受到了它的方便,大多都转了过来
SqlSugar 官网
接下来开始使用SqlSugar,使用其实是一件特点简单的事情 ,大家可以简单看一个官网的使用文档1,2,3。10分钟看完,然后就可以开始做项目了
基于自己的使用习惯,我喜欢将与数据库相当的核心内容放在一起,基本的类如下
config.cs 数据库参数配置
DbFactory.cs 数据库实例化工厂
HttpRuntimeCache.cs 为Sqlsugarclient提供缓存的接口实现
config.cs
public class Config
{
///
/// 数据库连接字符串(私有字段)
///
private static readonly string _connectionString = ConfigurationManager.ConnectionStrings["SQLDbCon"].ConnectionString;
///
/// 数据库连接字符串(公有属性)
///
public static string ConnectionString
{
get { return _connectionString; }
}
}
DbFactory.cs
public class DbFactory
{
///
/// SqlSugarClient属性
///
///
public static SqlSugarClient GetSqlSugarClient()
{
var db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = Config.ConnectionString,
DbType = DbType.SqlServer,
IsAutoCloseConnection = true,
ConfigureExternalServices = new ConfigureExternalServices()
{
DataInfoCacheService = new HttpRuntimeCache()
},
IsShardSameThread = true
});
return db;
}
}
HttpRuntimeCache.cs
public class HttpRuntimeCache : ICacheService
{
public void Add(string key, V value)
{
HttpRuntimeCacheHelper.GetInstance().Add(key, value);
}
public void Add(string key, V value, int cacheDurationInSeconds)
{
HttpRuntimeCacheHelper.GetInstance().Add(key, value, cacheDurationInSeconds);
}
public bool ContainsKey(string key)
{
return HttpRuntimeCacheHelper.GetInstance().ContainsKey(key);
}
public V Get(string key)
{
return HttpRuntimeCacheHelper.GetInstance().Get(key);
}
public IEnumerable GetAllKey()
{
return HttpRuntimeCacheHelper.GetInstance().GetAllKey();
}
public V GetOrCreate(string cacheKey, Func create, int cacheDurationInSeconds = int.MaxValue)
{
var cacheManager = HttpRuntimeCacheHelper.GetInstance();
if (cacheManager.ContainsKey(cacheKey))
{
return cacheManager[cacheKey];
}
else
{
var result = create();
cacheManager.Add(cacheKey, result, cacheDurationInSeconds);
return result;
}
}
public void Remove(string key)
{
HttpRuntimeCacheHelper.GetInstance().Remove(key);
}
}
internal class HttpRuntimeCacheHelper
{
#region 全局变量
private static HttpRuntimeCacheHelper _instance = null;
private static readonly object _instanceLock = new object();
#endregion
#region 构造函数
private HttpRuntimeCacheHelper() { }
#endregion
#region 属性
///
///根据key获取value
///
///
public V this[string key]
{
get { return (V)HttpRuntime.Cache[CreateKey(key)]; }
}
#endregion
#region 公共函数
///
/// key是否存在
///
/// key
/// /// 存在true 不存在false . /// ///
public bool ContainsKey(string key)
{
return HttpRuntime.Cache[CreateKey(key)] != null;
}
///
/// 获取缓存值
///
/// key
///
public V Get(string key)
{
return (V)HttpRuntime.Cache.Get(CreateKey(key));
}
///
/// 获取实例 (单例模式)
///
///
public static HttpRuntimeCacheHelper GetInstance()
{
if (_instance == null)
lock (_instanceLock)
if (_instance == null)
_instance = new HttpRuntimeCacheHelper();
return _instance;
}
///
/// 插入缓存(默认20分钟)
///
/// key
/// value
public void Add(string key, V value)
{
Add(key, value, 60 * 20);
}
///
/// 插入缓存
///
/// key
/// value
/// 过期时间单位秒
public void Add(string key, V value, int cacheDurationInSeconds)
{
Add(key, value, cacheDurationInSeconds, CacheItemPriority.Default);
}
///
/// 插入缓存.
///
/// key
/// value
/// 过期时间单位秒
/// 缓存项属性
public void Add(string key, V value, int cacheDurationInSeconds, CacheItemPriority priority)
{
string keyString = CreateKey(key);
HttpRuntime.Cache.Insert(keyString, value, null,
DateTime.Now.AddSeconds(cacheDurationInSeconds), Cache.NoSlidingExpiration, priority, null);
}
///
/// 插入缓存.
///
/// key
/// value
/// 过期时间单位秒
/// 缓存项属性
public void Add(string key, V value, int
cacheDurationInSeconds, CacheDependency dependency, CacheItemPriority priority)
{
string keyString = CreateKey(key);
HttpRuntime.Cache.Insert(keyString, value,
dependency, DateTime.Now.AddSeconds(cacheDurationInSeconds), Cache.NoSlidingExpiration, priority, null);
}
///
/// 删除缓存
///
/// key
public void Remove(string key)
{
HttpRuntime.Cache.Remove(CreateKey(key));
}
///
/// 清除所有缓存
///
public void RemoveAll()
{
System.Web.Caching.Cache cache = HttpRuntime.Cache;
IDictionaryEnumerator CacheEnum = cache.GetEnumerator();
ArrayList al = new ArrayList();
while (CacheEnum.MoveNext())
{
al.Add(CacheEnum.Key);
}
foreach (string key in al)
{
cache.Remove(key);
}
}
///
/// 清除所有包含关键字的缓存
///
/// 关键字
public void RemoveAll(Func removeExpression)
{
System.Web.Caching.Cache _cache = HttpRuntime.Cache;
var allKeyList = GetAllKey();
var delKeyList = allKeyList.Where(removeExpression).ToList();
foreach (var key in delKeyList)
{
HttpRuntime.Cache.Remove(key); ;
}
}
///
/// 获取所有缓存key
///
///
public IEnumerable GetAllKey()
{
IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();
while (CacheEnum.MoveNext())
{
yield return CacheEnum.Key.ToString();
}
}
#endregion
#region 私有函数
///
///创建KEY
///
/// Key
///
private string CreateKey(string key)
{
return key;
}
#endregion
}
Models层完成