secret
Created

Interception Problem

  • Download Gist
BindModule.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
namespace WebPortal.Configuration
{
 
public class BindModule : NinjectModule
{
#region Public Methods
 
public override void Load()
{
this.Bind<IControllerActivator>().To<CustomControllerActivator>();
this.Bind<IDatabaseFactory>().To<NHDatabaseFactory>().InRequestScope();
this.Bind<IUnitOfWork>().To<UnitOfWork>().InRequestScope();
 
this.Bind(typeof(ServiceBase<,>)).ToSelf().Intercept().With<LoggingInterceptor>();
 
this.Bind<ILocationRepository>().To<LocationRepository>().InRequestScope();
this.Bind<IDistributorRepository>().To<DistributorRepository>().InRequestScope();
 
this.Bind<IDistributorService>().To<DistributorService>().InRequestScope();
this.Bind<ILocationService>().To<LocationService>().InRequestScope();
this.Bind<ITaminContext>().To<NHibernateContext>().InRequestScope();
 
}
 
#endregion
}
}
DistributorRepository.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
namespace WebPortal.Data
{
public class DistributorRepository : RepositoryBase<Distributor>, IDistributorRepository
{
#region Constructors and Destructors
 
public DistributorRepository(IDatabaseFactory databaseFactory)
: base(databaseFactory)
{
}
 
#endregion
 
#region Implemented Interfaces
 
#region IDistributorRepository
 
public string GenerateFollowupCode()
{
//...
}
 
public Distributor GetByUserId(Guid userId)
{
//....
}
 
#endregion
 
#endregion
 
#region Methods
 
protected override void BeforeSave(Distributor arg)
{
//....
}
 
#endregion
}
}
IRepository.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
namespace WebPortal.Data.Common
{
public interface IRepository<T>
where T : class
{
#region Public Methods
 
void Add(T entity);
 
void Delete(T entity);
 
void Delete(Guid id);
 
void Delete(Func<T, Boolean> predicate);
 
T Get(Func<T, Boolean> where);
 
IEnumerable<T> GetAll();
 
T GetById(Guid Id);
 
IEnumerable<T> GetMany(Func<T, bool> where);
 
void Save(T arg);
 
#endregion
}
}
IServiceBase.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
namespace WebPortal.Service.Common
{
public interface IServiceBase<T>
{
#region Public Methods
 
void Add(T entity);
 
void Delete(Guid id);
 
void Delete(T entity);
 
void Delete(Func<T, bool> predicate);
 
T Get(Func<T, Boolean> where);
 
IEnumerable<T> GetAll();
 
T GetById(Guid Id);
 
IEnumerable<T> GetMany(Func<T, bool> where);
 
void Save(T arg);
 
#endregion
}
}
RepositoryBase.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
namespace WebPortal.Data.Infrastructure
{
public abstract class RepositoryBase<T> : IRepository<T>
where T : class
{
#region Constants and Fields
 
private readonly IQueryable<T> dbset;
 
private readonly ITaminContext taminContext;
 
#endregion
 
#region Constructors and Destructors
 
protected RepositoryBase(IDatabaseFactory databaseFactory)
{
this.DatabaseFactory = databaseFactory;
this.dbset = this.DataContext.Query<T>();
this.taminContext = databaseFactory.TaminContext;
}
 
#endregion
 
#region Properties
 
protected ITaminContext DataContext
{
get
{
return this.taminContext ?? this.DatabaseFactory.TaminContext;
}
}
 
private IDatabaseFactory DatabaseFactory { get; set; }
 
#endregion
 
#region Implemented Interfaces
 
#region IRepository<T>
 
public virtual void Add(T entity)
{
this.BeforeSave(entity);
this.DataContext.Save(entity);
}
 
protected virtual void BeforeSave(T arg)
{
}
 
public virtual void Delete(T entity)
{
this.DataContext.Delete(entity);
}
 
public void Delete(Guid id)
{
this.DataContext.Delete<T>(id);
}
 
public virtual void Delete(Func<T, Boolean> where)
{
IEnumerable<T> objects = this.dbset.Where(where).AsEnumerable();
foreach (T obj in objects)
{
this.Delete(obj);
}
}
 
public virtual T Get(Func<T, Boolean> where)
{
return this.dbset.FirstOrDefault(where);
}
 
public virtual IEnumerable<T> GetAll()
{
return this.dbset.ToList();
}
 
public virtual T GetById(Guid id)
{
return this.DataContext.Load<T>(id);
}
 
public virtual IEnumerable<T> GetMany(Func<T, bool> where)
{
return this.dbset.Where(where);
}
 
public virtual void Save(T arg)
{
this.BeforeSave(arg);
this.DataContext.Update(arg);
}
 
#endregion
 
#endregion
}
}
ServiceBase.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
namespace WebPortal.Service
{
[LogMyCalls]
public abstract class ServiceBase<TEntity, ITEntityRepository> : IServiceBase<TEntity> where TEntity : class, new()
where ITEntityRepository : IRepository<TEntity>
{
#region Constructors and Destructors
 
protected ServiceBase(ITEntityRepository repository, IUnitOfWork unitOfWork)
{
this.UnitOfWork = unitOfWork;
this.Repository = repository;
}
 
#endregion
 
#region Properties
 
protected virtual ITEntityRepository Repository { get; set; }
 
protected virtual IUnitOfWork UnitOfWork { get; set; }
 
#endregion
 
#region Implemented Interfaces
 
#region IServiceBase<TEntity>
 
public virtual void Add(TEntity entity)
{
this.Repository.Add(entity);
this.UnitOfWork.Commit();
}
 
public virtual void Delete(Guid id)
{
Repository.Delete(id);
this.UnitOfWork.Commit();
}
 
public virtual void Delete(TEntity entity)
{
this.Repository.Delete(entity);
this.UnitOfWork.Commit();
}
 
public virtual void Delete(Func<TEntity, bool> predicate)
{
this.Repository.Delete(predicate);
this.UnitOfWork.Commit();
}
 
public virtual TEntity Get(Func<TEntity, bool> @where)
{
return this.Repository.Get(@where);
}
 
public virtual IEnumerable<TEntity> GetAll()
{
return this.Repository.GetAll();
}
 
public virtual TEntity GetById(Guid Id)
{
return this.Repository.GetById(Id);
}
 
public virtual IEnumerable<TEntity> GetMany(Func<TEntity, bool> @where)
{
return this.Repository.GetMany(@where);
}
 
public virtual void Save(TEntity arg)
{
this.Repository.Save(arg);
this.UnitOfWork.Commit();
}
 
#endregion
 
#endregion
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.