Guest User

Untitled

a guest
Apr 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. public interface IRepository<T> where T : BasePoco {
  2. T Create(T t);
  3. IEnumerable<T> Select(int? id = default(int?));
  4. T Update(T t);
  5. bool Delete(int? id = default(int?));
  6. }
  7.  
  8. public abstract class BaseRepository<T> where T : new(){
  9. protected static MsSqlConnector _msSqlConnector;
  10. protected static List<PropertyInfo> _propertyInfo;
  11. protected static Type _type;
  12. protected static List<ClassPropertyCache> _classPropertyCache;
  13.  
  14. protected BaseRepository(MsSqlConnector msSqlConnector)
  15. {
  16. _msSqlConnector = msSqlConnector;
  17. }
  18.  
  19. protected virtual void _mapReaderToObject(IDataReader dataReader, T poco)
  20. {
  21. for (int i = 0; i < _propertyInfo.Count; i++) {
  22. var cachedProperties = _classPropertyCache.First(m => m.Name == _propertyInfo[i].Name);
  23. cachedProperties.SetProperty(poco, Convert.ChangeType(dataReader[cachedProperties.Name], Nullable.GetUnderlyingType(_propertyInfo[i].PropertyType) ?? _propertyInfo[i].PropertyType));
  24. }
  25. }
  26.  
  27. protected virtual void _mapReaderToObject(IDataReader dataReader, IList<T> pocoList)
  28. {
  29. while (dataReader.Read())
  30. {
  31. var poco = new T();
  32. _mapReaderToObject(dataReader, poco);
  33.  
  34. pocoList.Add(poco);
  35. }
  36. }
  37.  
  38. protected IList<SqlParameter> _mapPocoToSqlParameters(T poco, bool includeId = default(bool))
  39. {
  40. var parameters = new List<SqlParameter>();
  41.  
  42. for (int i = 0; i < _propertyInfo.Count; i++)
  43. {
  44. var cachedProperties = _classPropertyCache.First(m => m.Name == _propertyInfo[i].Name);
  45. parameters.Add(new SqlParameter($"@{_propertyInfo[i].Name}", (object)cachedProperties.GetProperty(poco) ?? DBNull.Value));
  46. }
  47.  
  48. if (!includeId)
  49. parameters.RemoveAll(m => m.ParameterName == "Id");
  50.  
  51. return parameters;
  52. }
  53.  
  54. protected static Action<object, object> _buildSetAccessor(string name, MethodInfo methodInfo)
  55. {
  56. var obj = Expression.Parameter(typeof(object), name);
  57. var value = Expression.Parameter(typeof(object));
  58.  
  59. return Expression.Lambda<Action<object, object>>( Expression.Call( Expression.Convert(obj, methodInfo.DeclaringType), methodInfo, Expression.Convert(value, methodInfo.GetParameters()[0].ParameterType)), obj, value).Compile();
  60. }
  61.  
  62. protected static Func<object, object> _buildGetAccessor(string name, MethodInfo method)
  63. {
  64. var obj = Expression.Parameter(typeof(object), name);
  65. return Expression.Lambda<Func<object, object>>(Expression.Convert(Expression.Call(Expression.Convert(obj, method.DeclaringType), method), typeof(object)), obj).Compile();
  66. }
  67. }
  68.  
  69. public class Category : BasePoco {
  70. public string Name { get; set; }
  71. public string Description { get; set; }
  72. public string Slug { get; set; }
  73. public DateTime? InsertDate { get; set; }
  74. public DateTime? EditDate { get; set; }
  75. public int? OrderIndex { get; set; }
  76. public int? Parent { get; set; }
  77. public string Lang { get; set; }
  78. public int? Level { get; set; }
  79. }
  80.  
  81. public sealed class CategoryRepository : BaseRepository<Category>, IRepository<Category> {
  82.  
  83. static CategoryRepository()
  84. {
  85. _propertyInfo = typeof(Category).GetProperties().ToList();
  86. _type = typeof(Category);
  87. _classPropertyCache = new List<Models.ClassPropertyCache>();
  88.  
  89. Parallel.ForEach(_propertyInfo, (propertyInfo) => {
  90. _classPropertyCache.Add(new Models.ClassPropertyCache() {
  91. Name = propertyInfo.Name,
  92. GetProperty = _buildGetAccessor(propertyInfo.Name, propertyInfo.GetGetMethod()),
  93. SetProperty = _buildSetAccessor(propertyInfo.Name, propertyInfo.GetSetMethod())
  94. });
  95. });
  96. }
  97.  
  98. public CategoryRepository(MsSqlConnector msSqlConnector) : base (msSqlConnector) { }
  99.  
  100. public Category Create(Category category) {
  101. var results = new Category();
  102.  
  103. _msSqlConnector.ExecuteReader(
  104. "usp_CategoryCreate",
  105. CommandType.StoredProcedure,
  106. CommandBehavior.CloseConnection,
  107. _mapPocoToSqlParameters(category),
  108. reader => _mapReaderToObject(reader, results));
  109.  
  110. return results;
  111. }
  112.  
  113. public bool Delete(int? id = default(int?)) {
  114. return _msSqlConnector.ExecuteNonQuery(
  115. "usp_CategoryDelete",
  116. CommandType.StoredProcedure,
  117. new[] {
  118. new SqlParameter("@Id", (object)id ?? DBNull.Value)
  119. }).ToString() == "1";
  120. }
  121.  
  122. public IEnumerable<Category> Select(int? id = default(int?)) {
  123. var results = new List<Category>();
  124.  
  125. _msSqlConnector.ExecuteReader(
  126. "usp_CategorySelect",
  127. CommandType.StoredProcedure,
  128. CommandBehavior.CloseConnection,
  129. new[]
  130. {
  131. new SqlParameter("@Id", (object)id ?? DBNull.Value)
  132. }, reader => _mapReaderToObject(reader, results));
  133.  
  134. return results;
  135. }
  136.  
  137. public Category Update(Category category) {
  138.  
  139. var results = new Category();
  140.  
  141. _msSqlConnector.ExecuteReader(
  142. "usp_CategoryUpdate",
  143. CommandType.StoredProcedure,
  144. CommandBehavior.CloseConnection,
  145. _mapPocoToSqlParameters(category, true),
  146. reader => _mapReaderToObject(reader, results));
  147.  
  148. return results;
  149. }
  150. }
  151.  
  152. public class ClassPropertyCache {
  153. public string Name { get; set; }
  154. public Action<object, object> SetProperty { get; set; }
  155. public Func<object, object> GetProperty { get; set; }
  156. }
  157.  
  158. public class DbContext {
  159. private MsSqlConnector _msSqlConnector;
  160.  
  161. public DbContext(string connectionString)
  162. {
  163. _msSqlConnector = new MsSqlConnector(connectionString);
  164. CategoryRepository = new CategoryRepository(_msSqlConnector);
  165. }
  166.  
  167. public static CategoryRepository CategoryRepository { get; private set; }
  168. }
Add Comment
Please, Sign In to add comment