![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public static class SingletonProvider<T> where T : new()
{
private static T m_instance;
private static readonly object sync = new object();
#region 无够构造函数的泛型单列
/// <summary>
/// 无够构造函数的泛型单列
/// </summary>
public static T Instance
{
get
{
if (m_instance == null)
{
lock (sync)
{
if (m_instance == null)
{
try
{
m_instance = new T();
}
catch
{
}
}
}
}
return m_instance;
}
}
#endregion
#region 带构造函数的泛型单列模式
/// <summary>
/// 带构造函数的泛型单列模式
/// </summary>
public static T InstanceForParameter(params object[] objarr)
{
if (m_instance == null)
{
lock (sync)
{
if (m_instance == null)
{
var t = typeof(T);
var tp = new Type[objarr.Length];
for (int i = 0; i < objarr.Length; i++)
{
tp[i] = objarr[i].GetType();
}
try
{
System.Reflection.ConstructorInfo ci = t.GetConstructor(tp);
m_instance = (T)ci.Invoke(objarr);
}
catch
{
}
}
}
}
return m_instance;
}
#endregion
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
public static class SingletonProvider<T> where T : new()
{
private static T m_instance;
private static readonly object sync = new object();
#region 无够构造函数的泛型单列
/// <summary>
/// 无够构造函数的泛型单列
/// </summary>
public static T Instance
{
get
{
if (m_instance == null)
{
lock (sync)
{
if (m_instance == null)
{
try
{
m_instance = new T();
}
catch
{
}
}
}
}
return m_instance;
}
}
#endregion
#region 带构造函数的泛型单列模式
/// <summary>
/// 带构造函数的泛型单列模式
/// </summary>
public static T InstanceForParameter(params object[] objarr)
{
if (m_instance == null)
{
lock (sync)
{
if (m_instance == null)
{
var t = typeof(T);
var tp = new Type[objarr.Length];
for (int i = 0; i < objarr.Length; i++)
{
tp[i] = objarr[i].GetType();
}
try
{
System.Reflection.ConstructorInfo ci = t.GetConstructor(tp);
m_instance = (T)ci.Invoke(objarr);
}
catch
{
}
}
}
}
return m_instance;
}
#endregion
}
1.先放个单例,减少开销
2.LinqReflectionHelp,这类辅助LINQ
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using System.Text;
using System.Linq.Expressions;
namespace MvcApplication1.Models
{
public static class LinqReflectionHelp
{
public static void ExecMethodMForMainObject<T>(T MainObject, string methodName, params object[] paramesForMethod)
{
Type t = MainObject.GetType();
MethodInfo[] methods = t.GetMethods();
MethodInfo method = findAMethodInT(methodName, methods);
if (method != null)
{
try
{
method.Invoke(MainObject, paramesForMethod);
}
catch
{
throw new Exception();
}
}
}
private static MethodInfo findAMethodInT(string MethodName, MethodInfo[] methods)
{
foreach (MethodInfo item in methods)
{
if (item.Name.Equals(MethodName))
{
return item;
}
}
return null;
}
public static System.Data.Linq.Table<TableName> SelTableListLinqDB<LinqDBContextType, TableName>(LinqDBContextType reflectionEntity) where TableName : class
{
object PropertiesValue = null;
Type t = typeof(LinqDBContextType);
foreach (var item in t.GetProperties())
{
if (item.PropertyType == typeof(System.Data.Linq.Table<TableName>))
{
PropertiesValue = item.GetValue(reflectionEntity, null);
return (System.Data.Linq.Table<TableName>)PropertiesValue;
}
}
return default(System.Data.Linq.Table<TableName>);
}
public static T2 FindModel<PropertiesList, T2>(PropertiesList reflectionEntity, int id) where T2 : class
{
object PropertiesValue = null;
Type t = typeof(PropertiesList);
foreach (var item in t.GetProperties())
{
if (item.PropertyType == typeof(T2))
{
PropertiesValue = item.GetValue(reflectionEntity, null);
return (T2)PropertiesValue;
}
}
return default(T2); ;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using System.Text;
using System.Linq.Expressions;
namespace MvcApplication1.Models
{
public static class LinqReflectionHelp
{
public static void ExecMethodMForMainObject<T>(T MainObject, string methodName, params object[] paramesForMethod)
{
Type t = MainObject.GetType();
MethodInfo[] methods = t.GetMethods();
MethodInfo method = findAMethodInT(methodName, methods);
if (method != null)
{
try
{
method.Invoke(MainObject, paramesForMethod);
}
catch
{
throw new Exception();
}
}
}
private static MethodInfo findAMethodInT(string MethodName, MethodInfo[] methods)
{
foreach (MethodInfo item in methods)
{
if (item.Name.Equals(MethodName))
{
return item;
}
}
return null;
}
public static System.Data.Linq.Table<TableName> SelTableListLinqDB<LinqDBContextType, TableName>(LinqDBContextType reflectionEntity) where TableName : class
{
object PropertiesValue = null;
Type t = typeof(LinqDBContextType);
foreach (var item in t.GetProperties())
{
if (item.PropertyType == typeof(System.Data.Linq.Table<TableName>))
{
PropertiesValue = item.GetValue(reflectionEntity, null);
return (System.Data.Linq.Table<TableName>)PropertiesValue;
}
}
return default(System.Data.Linq.Table<TableName>);
}
public static T2 FindModel<PropertiesList, T2>(PropertiesList reflectionEntity, int id) where T2 : class
{
object PropertiesValue = null;
Type t = typeof(PropertiesList);
foreach (var item in t.GetProperties())
{
if (item.PropertyType == typeof(T2))
{
PropertiesValue = item.GetValue(reflectionEntity, null);
return (T2)PropertiesValue;
}
}
return default(T2); ;
}
}
}
3.LINQ数据库处理
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public static class LinqDBHelper<LinqContextDB>
where LinqContextDB : new()
{
public static LinqContextDB LinqDBContextEntity
{
get
{
return SingletonProvider<LinqContextDB>.Instance;
}
}
public static LinqContextDB LinqDBContextEntityForParameter(params object[] objarr)
{
return SingletonProvider<LinqContextDB>.InstanceForParameter(objarr);
}
public static System.Data.Linq.Table<TableName> getTableList<TableName>()
where TableName : class
{
return LinqReflectionHelp.SelTableListLinqDB<LinqContextDB, TableName>(LinqDBContextEntity);
}
public static void Save()
{
LinqReflectionHelp.ExecMethodMForMainObject(LinqDBContextEntity, "SubmitChanges", null);
}
public static void Add<TableName>(TableName T2Model) where TableName : class
{
LinqReflectionHelp.ExecMethodMForMainObject(LinqDBContextEntity, "InsertOnSubmit", T2Model);
}
public static void Delete<TableName>(TableName T2Model) where TableName : class
{
LinqReflectionHelp.ExecMethodMForMainObject(LinqDBContextEntity, "DeleteOnSubmit", T2Model);
}
public static IQueryable<LinqTableObjct> FindSomeTableListFromLinqTableObjct<LinqTableObjct>
(IQueryable<LinqTableObjct> linqTableObjct, Expression<Func<LinqTableObjct, bool>> expression)
where LinqTableObjct : class,new()
{
try
{
IQueryable<LinqTableObjct> TableEntity = linqTableObjct.Where<LinqTableObjct>(expression);
return TableEntity;
}
catch
{
}
return default(System.Data.Linq.Table<LinqTableObjct>);
}
public static TableName FindModel<TableName>(System.Data.Linq.Table<TableName> tableList, Expression<Func<TableName, bool>> expression)
where TableName : class,new()
{
try
{
TableName TableEntity = tableList.Single<TableName>(expression);
return TableEntity;
}
catch
{
}
return default(TableName);
}
}
where LinqContextDB : new()
{
public static LinqContextDB LinqDBContextEntity
{
get
{
return SingletonProvider<LinqContextDB>.Instance;
}
}
public static LinqContextDB LinqDBContextEntityForParameter(params object[] objarr)
{
return SingletonProvider<LinqContextDB>.InstanceForParameter(objarr);
}
public static System.Data.Linq.Table<TableName> getTableList<TableName>()
where TableName : class
{
return LinqReflectionHelp.SelTableListLinqDB<LinqContextDB, TableName>(LinqDBContextEntity);
}
public static void Save()
{
LinqReflectionHelp.ExecMethodMForMainObject(LinqDBContextEntity, "SubmitChanges", null);
}
public static void Add<TableName>(TableName T2Model) where TableName : class
{
LinqReflectionHelp.ExecMethodMForMainObject(LinqDBContextEntity, "InsertOnSubmit", T2Model);
}
public static void Delete<TableName>(TableName T2Model) where TableName : class
{
LinqReflectionHelp.ExecMethodMForMainObject(LinqDBContextEntity, "DeleteOnSubmit", T2Model);
}
public static IQueryable<LinqTableObjct> FindSomeTableListFromLinqTableObjct<LinqTableObjct>
(IQueryable<LinqTableObjct> linqTableObjct, Expression<Func<LinqTableObjct, bool>> expression)
where LinqTableObjct : class,new()
{
try
{
IQueryable<LinqTableObjct> TableEntity = linqTableObjct.Where<LinqTableObjct>(expression);
return TableEntity;
}
catch
{
}
return default(System.Data.Linq.Table<LinqTableObjct>);
}
public static TableName FindModel<TableName>(System.Data.Linq.Table<TableName> tableList, Expression<Func<TableName, bool>> expression)
where TableName : class,new()
{
try
{
TableName TableEntity = tableList.Single<TableName>(expression);
return TableEntity;
}
catch
{
}
return default(TableName);
}
}
调用方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public static class DepartmentRepository
{
public static System.Data.Linq.Table<Department> AllDepartments
{
get
{
return LinqDBHelper<SchoolDBDataContext>.getProperties<System.Data.Linq.Table<Department>>();
}
}
public static SchoolDBDataContext schoolContext
{
get
{
return LinqDBHelper<SchoolDBDataContext>.LinqDBContextEntity;
}
}
public static IQueryable<Department> FindSomeDepartment(OrderDirection OrderBy)
{
Expression<Func<Department, bool>> expression = PredicateExtensions.True<Department>();
expression.And(d => (d.Budget > 1 && d.Administrator > 1));
IQueryable<Department> DepartLinqList = LinqDBHelper<SchoolDBDataContext>.FindSomeTableListFromLinqTableObjct<Department>(AllDepartments, expression);
if (OrderBy == OrderDirection.ASC)
{
return DepartLinqList.OrderBy(d => d.Budget);
}
return DepartLinqList.OrderByDescending(d => d.Budget);
}
public static Department GetDepartment(int departmentID)
{
Expression<Func<Department, bool>> expr = d => d.DepartmentID == departmentID;
return LinqDBHelper<SchoolDBDataContext>.FindModel<Department>(AllDepartments, expr);
}
public static void Add(Department department)
{
// LinqDBHelper<SchoolDBDataContext>.Add(department);
object[] ParameterList = { department };
//两种调用方法随意
LinqReflectionHelp.ExecMethodMForMainObject(LinqDBHelper<SchoolDBDataContext>.LinqDBContextEntity.Department, "InsertOnSubmit", ParameterList);
}
public static void Delete(Department department)
{
LinqDBHelper<SchoolDBDataContext>.Delete(department);
}
}
public enum OrderDirection
{
ASC, DESC
}
{
public static System.Data.Linq.Table<Department> AllDepartments
{
get
{
return LinqDBHelper<SchoolDBDataContext>.getProperties<System.Data.Linq.Table<Department>>();
}
}
public static SchoolDBDataContext schoolContext
{
get
{
return LinqDBHelper<SchoolDBDataContext>.LinqDBContextEntity;
}
}
public static IQueryable<Department> FindSomeDepartment(OrderDirection OrderBy)
{
Expression<Func<Department, bool>> expression = PredicateExtensions.True<Department>();
expression.And(d => (d.Budget > 1 && d.Administrator > 1));
IQueryable<Department> DepartLinqList = LinqDBHelper<SchoolDBDataContext>.FindSomeTableListFromLinqTableObjct<Department>(AllDepartments, expression);
if (OrderBy == OrderDirection.ASC)
{
return DepartLinqList.OrderBy(d => d.Budget);
}
return DepartLinqList.OrderByDescending(d => d.Budget);
}
public static Department GetDepartment(int departmentID)
{
Expression<Func<Department, bool>> expr = d => d.DepartmentID == departmentID;
return LinqDBHelper<SchoolDBDataContext>.FindModel<Department>(AllDepartments, expr);
}
public static void Add(Department department)
{
// LinqDBHelper<SchoolDBDataContext>.Add(department);
object[] ParameterList = { department };
//两种调用方法随意
LinqReflectionHelp.ExecMethodMForMainObject(LinqDBHelper<SchoolDBDataContext>.LinqDBContextEntity.Department, "InsertOnSubmit", ParameterList);
}
public static void Delete(Department department)
{
LinqDBHelper<SchoolDBDataContext>.Delete(department);
}
}
public enum OrderDirection
{
ASC, DESC
}
还少放了一个东西 补上
网上好多下的 ,
PredicateExtensions,处理EXPRESSION表达式的一个老外写的,这里就不浪费空间了。。。