Skip to content

Instantly share code, notes, and snippets.

@jahan-paisley
Created October 1, 2011 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jahan-paisley/424637484504b89789d6 to your computer and use it in GitHub Desktop.
Save jahan-paisley/424637484504b89789d6 to your computer and use it in GitHub Desktop.
Interception Problem
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
}
}
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
}
}
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
}
}
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
}
}
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
}
}
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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment