Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your base Repo:</p> <pre><code>public class BaseRepository&lt;TObject&gt; : IRepository&lt;TObject&gt; where TObject : class { public BaseRepository(IUnitOfWork unitOfWork) { if (unitOfWork == null) throw new ArgumentException("unitOfWork"); UnitOfWork = unitOfWork; } protected DbSet&lt;TObject&gt; DbSet { get { return Context.Set&lt;TObject&gt;(); } } public void Dispose() { if (sharedContext &amp;&amp; (Context != null)) Context.Dispose(); } public virtual IQueryable&lt;TObject&gt; All() { return DbSet.AsQueryable(); } public virtual IQueryable&lt;TObject&gt; Filter(Expression&lt;Func&lt;TObject, bool&gt;&gt; predicate) { return DbSet.Where(predicate).AsQueryable&lt;TObject&gt;(); } public virtual IQueryable&lt;TObject&gt; Filter&lt;Key&gt;(Expression&lt;Func&lt;TObject, Key&gt;&gt; sortingSelector, Expression&lt;Func&lt;TObject, bool&gt;&gt; filter, out int total, SortingOrders sortby = SortingOrders.Asc, int index = 0, int size = 50) { int skipCount = index * size; var _resultSet = filter != null ? DbSet.Where(filter).AsQueryable() : DbSet.AsQueryable(); total = _resultSet.Count(); _resultSet = sortby == SortingOrders.Asc ? _resultSet.OrderBy(sortingSelector).AsQueryable() : _resultSet.OrderByDescending(sortingSelector).AsQueryable(); _resultSet = skipCount == 0 ? _resultSet.Take(size) : _resultSet.Skip(skipCount).Take(size); return _resultSet; } public bool Contains(Expression&lt;Func&lt;TObject, bool&gt;&gt; predicate) { return DbSet.Count(predicate) &gt; 0; } public virtual TObject Find(params object[] keys) { return DbSet.Find(keys); } public virtual TObject Find(Expression&lt;Func&lt;TObject, bool&gt;&gt; predicate) { return DbSet.FirstOrDefault(predicate); } public virtual TObject Create(TObject TObject, bool SaveChanges = true) { var newEntry = DbSet.Add(TObject); if (!sharedContext &amp;&amp; SaveChanges) Context.SaveChanges(); return newEntry; } public virtual int Count { get { return DbSet.Count(); } } public virtual int Delete(TObject TObject) { DbSet.Remove(TObject); if (!sharedContext) return Context.SaveChanges(); return 0; } public virtual int Update(TObject TObject, bool SaveChanges = true) { var entry = Context.Entry(TObject); DbSet.Attach(TObject); entry.State = EntityState.Modified; if (!sharedContext &amp;&amp; SaveChanges) return Context.SaveChanges(); return 0; } public virtual int Delete(Expression&lt;Func&lt;TObject, bool&gt;&gt; predicate) { var objects = Filter(predicate); foreach (var obj in objects) DbSet.Remove(obj); if (!sharedContext) return Context.SaveChanges(); return 0; } /// &lt;summary&gt; /// Sets the state of an entity. /// &lt;/summary&gt; /// &lt;param name="entity"&gt;object to set state.&lt;/param&gt; /// &lt;param name="entityState"&gt;&lt;see cref="EntityState"/&gt;&lt;/param&gt; protected virtual void SetEntityState(object entity, EntityState entityState) { Context.Entry(entity).State = entityState; } /// &lt;summary&gt; /// /// &lt;/summary&gt; /// &lt;param name="entity"&gt;&lt;/param&gt; protected virtual void Attach(object entity) { if (Context.Entry(entity).State == EntityState.Detached) Context.Entry(entity).State = EntityState.Modified; } protected virtual void Detach(object entity) { Context.Entry(entity).State = EntityState.Detached; } public void SubmitChanges() { UnitOfWork.SaveChanges(); } #region Properties private bool sharedContext { get; set; } /// &lt;summary&gt; /// Unit of work controlling this repository. /// &lt;/summary&gt; protected IUnitOfWork UnitOfWork { get; set; } /// &lt;summary&gt; /// Provides access to the ef context we are working with /// &lt;/summary&gt; internal IMyContext Context { get { return (IMyContext)UnitOfWork; } } #endregion } </code></pre> <p>Notice Context is an interface implementing UnitOfWork.</p> <p>Your context interface:</p> <pre><code>public interface IMyContext : IDbContext { DbSet&lt;Sometype&gt; SomeProperty { get; set; } ... } </code></pre> <p>Your IDbContext interface:</p> <pre><code> public interface IDbContext { DbChangeTracker ChangeTracker { get; } DbContextConfiguration Configuration { get; } Database Database { get; } void Dispose(); void Dispose(bool disposing); DbEntityEntry Entry(object entity); DbEntityEntry&lt;TEntity&gt; Entry&lt;TEntity&gt;(TEntity entity) where TEntity : class; bool Equals(object obj); int GetHashCode(); Type GetType(); IEnumerable&lt;DbEntityValidationResult&gt; GetValidationErrors(); void OnModelCreating(DbModelBuilder modelBuilder); int SaveChanges(); DbSet&lt;TEntity&gt; Set&lt;TEntity&gt;() where TEntity : class; DbSet Set(Type entityType); bool ShouldValidateEntity(DbEntityEntry entityEntry); string ToString(); DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary&lt;object, object&gt; items); } </code></pre> <p>Then the actual context implementation:</p> <pre><code> public class MyContext : DbContext, IUnitOfWork, IMyContext { //public MyContext() //{ // Database.SetInitializer&lt;ReconContext&gt;(null); //} public ReconContext() : base("Name=ReconContext") { ((IObjectContextAdapter)this).ObjectContext.ContextOptions.ProxyCreationEnabled = false; } public DbSet&lt;SomeType&gt; SomeProperty { get; set; } .... public new void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new SomePropertyMap()); ..... base.OnModelCreating(modelBuilder); } int IUnitOfWork.SaveChanges() { return base.SaveChanges(); } void IDisposable.Dispose() { base.Dispose(); } public new void Dispose(bool disposing) { base.Dispose(disposing); } public new bool ShouldValidateEntity(DbEntityEntry entityEntry) { return base.ShouldValidateEntity(entityEntry); } public new DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary&lt;object, object&gt; items) { return base.ValidateEntity(entityEntry, items); } } </code></pre> <p>Then in your ninject config you simply do:</p> <pre><code>kernel.Bind&lt;IUnitOfWork&lt;MyContext&gt;&gt;().To&lt;MyContext&gt;().InRequestScope(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload