Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface IRepository<T> where T : BasePoco {
- T Create(T t);
- IEnumerable<T> Select(int? id = default(int?));
- T Update(T t);
- bool Delete(int? id = default(int?));
- }
- public abstract class BaseRepository<T> where T : new(){
- protected static MsSqlConnector _msSqlConnector;
- protected static List<PropertyInfo> _propertyInfo;
- protected static Type _type;
- protected static List<ClassPropertyCache> _classPropertyCache;
- protected BaseRepository(MsSqlConnector msSqlConnector)
- {
- _msSqlConnector = msSqlConnector;
- }
- protected virtual void _mapReaderToObject(IDataReader dataReader, T poco)
- {
- for (int i = 0; i < _propertyInfo.Count; i++) {
- var cachedProperties = _classPropertyCache.First(m => m.Name == _propertyInfo[i].Name);
- cachedProperties.SetProperty(poco, Convert.ChangeType(dataReader[cachedProperties.Name], Nullable.GetUnderlyingType(_propertyInfo[i].PropertyType) ?? _propertyInfo[i].PropertyType));
- }
- }
- protected virtual void _mapReaderToObject(IDataReader dataReader, IList<T> pocoList)
- {
- while (dataReader.Read())
- {
- var poco = new T();
- _mapReaderToObject(dataReader, poco);
- pocoList.Add(poco);
- }
- }
- protected IList<SqlParameter> _mapPocoToSqlParameters(T poco, bool includeId = default(bool))
- {
- var parameters = new List<SqlParameter>();
- for (int i = 0; i < _propertyInfo.Count; i++)
- {
- var cachedProperties = _classPropertyCache.First(m => m.Name == _propertyInfo[i].Name);
- parameters.Add(new SqlParameter($"@{_propertyInfo[i].Name}", (object)cachedProperties.GetProperty(poco) ?? DBNull.Value));
- }
- if (!includeId)
- parameters.RemoveAll(m => m.ParameterName == "Id");
- return parameters;
- }
- protected static Action<object, object> _buildSetAccessor(string name, MethodInfo methodInfo)
- {
- var obj = Expression.Parameter(typeof(object), name);
- var value = Expression.Parameter(typeof(object));
- return Expression.Lambda<Action<object, object>>( Expression.Call( Expression.Convert(obj, methodInfo.DeclaringType), methodInfo, Expression.Convert(value, methodInfo.GetParameters()[0].ParameterType)), obj, value).Compile();
- }
- protected static Func<object, object> _buildGetAccessor(string name, MethodInfo method)
- {
- var obj = Expression.Parameter(typeof(object), name);
- return Expression.Lambda<Func<object, object>>(Expression.Convert(Expression.Call(Expression.Convert(obj, method.DeclaringType), method), typeof(object)), obj).Compile();
- }
- }
- public class Category : BasePoco {
- public string Name { get; set; }
- public string Description { get; set; }
- public string Slug { get; set; }
- public DateTime? InsertDate { get; set; }
- public DateTime? EditDate { get; set; }
- public int? OrderIndex { get; set; }
- public int? Parent { get; set; }
- public string Lang { get; set; }
- public int? Level { get; set; }
- }
- public sealed class CategoryRepository : BaseRepository<Category>, IRepository<Category> {
- static CategoryRepository()
- {
- _propertyInfo = typeof(Category).GetProperties().ToList();
- _type = typeof(Category);
- _classPropertyCache = new List<Models.ClassPropertyCache>();
- Parallel.ForEach(_propertyInfo, (propertyInfo) => {
- _classPropertyCache.Add(new Models.ClassPropertyCache() {
- Name = propertyInfo.Name,
- GetProperty = _buildGetAccessor(propertyInfo.Name, propertyInfo.GetGetMethod()),
- SetProperty = _buildSetAccessor(propertyInfo.Name, propertyInfo.GetSetMethod())
- });
- });
- }
- public CategoryRepository(MsSqlConnector msSqlConnector) : base (msSqlConnector) { }
- public Category Create(Category category) {
- var results = new Category();
- _msSqlConnector.ExecuteReader(
- "usp_CategoryCreate",
- CommandType.StoredProcedure,
- CommandBehavior.CloseConnection,
- _mapPocoToSqlParameters(category),
- reader => _mapReaderToObject(reader, results));
- return results;
- }
- public bool Delete(int? id = default(int?)) {
- return _msSqlConnector.ExecuteNonQuery(
- "usp_CategoryDelete",
- CommandType.StoredProcedure,
- new[] {
- new SqlParameter("@Id", (object)id ?? DBNull.Value)
- }).ToString() == "1";
- }
- public IEnumerable<Category> Select(int? id = default(int?)) {
- var results = new List<Category>();
- _msSqlConnector.ExecuteReader(
- "usp_CategorySelect",
- CommandType.StoredProcedure,
- CommandBehavior.CloseConnection,
- new[]
- {
- new SqlParameter("@Id", (object)id ?? DBNull.Value)
- }, reader => _mapReaderToObject(reader, results));
- return results;
- }
- public Category Update(Category category) {
- var results = new Category();
- _msSqlConnector.ExecuteReader(
- "usp_CategoryUpdate",
- CommandType.StoredProcedure,
- CommandBehavior.CloseConnection,
- _mapPocoToSqlParameters(category, true),
- reader => _mapReaderToObject(reader, results));
- return results;
- }
- }
- public class ClassPropertyCache {
- public string Name { get; set; }
- public Action<object, object> SetProperty { get; set; }
- public Func<object, object> GetProperty { get; set; }
- }
- public class DbContext {
- private MsSqlConnector _msSqlConnector;
- public DbContext(string connectionString)
- {
- _msSqlConnector = new MsSqlConnector(connectionString);
- CategoryRepository = new CategoryRepository(_msSqlConnector);
- }
- public static CategoryRepository CategoryRepository { get; private set; }
- }
Add Comment
Please, Sign In to add comment