Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Below is the copy of my own working code. I do not pretending that my code is perfect, but it is handy and has loose coupleness. Also, it will be quite big amount of code, but do not be afraid =)...</p> <p><strong>IEntity:</strong></p> <pre><code>public interface IEntity { int Id { get; set; } } </code></pre> <p><strong>AbstractRepository and interface:</strong></p> <pre><code>public interface IRepository&lt;T&gt; { IEnumerable&lt;T&gt; List(); IEnumerable&lt;T&gt; List(Func&lt;T, bool&gt; pred); T Get(int id); T Get(Func&lt;T, bool&gt; pred); void Add(T entity); T Update(T entity); void Delete(T entity); void Delete(Func&lt;T, bool&gt; pred); } public abstract class AbstractRepository&lt;TEntity, TContext&gt; : IRepository&lt;TEntity&gt; where TContext : DbContext, new() where TEntity : class, IEntity { protected TContext context; public AbstractRepository(UnitOfWork&lt;TContext&gt; unit) { context = unit.Context; } public virtual IEnumerable&lt;TEntity&gt; List() { return context.Set&lt;TEntity&gt;(); } public virtual IEnumerable&lt;TEntity&gt; List(Func&lt;TEntity, bool&gt; pred) { return context.Set&lt;TEntity&gt;().Where(pred); } public virtual TEntity Get(int id) { return context.Set&lt;TEntity&gt;().Find(id); } public virtual TEntity Get(Func&lt;TEntity, bool&gt; pred) { return context.Set&lt;TEntity&gt;().FirstOrDefault(pred); } public virtual void Add(TEntity entity) { if (entity.Id &lt;= 0) context.Entry(entity).State = System.Data.EntityState.Added; } public virtual TEntity Update(TEntity entity) { if (entity.Id &gt; 0) { context.Entry(entity).State = System.Data.EntityState.Modified; return entity; } return null; } public virtual void Delete(TEntity entity) { context.Entry(entity).State = System.Data.EntityState.Deleted; } public virtual void Delete(Func&lt;TEntity, bool&gt; pred) { foreach (var entity in List(pred)) { Delete(entity); } } } </code></pre> <p><strong>Concrete repository:</strong></p> <pre><code>public class GroupRepository : AbstractRepository&lt;Group, SurveyContext&gt; { public GroupRepository(UnitOfWork&lt;SurveyContext&gt; unit) : base(unit) { } } </code></pre> <p><strong>DbContext successor:</strong></p> <pre><code>public class SurveyContext: DbContext { public SurveyContext() : base("name=ApplicationConnection") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { //Optional modelBuilder.Configurations.Add(new Confuration1()); modelBuilder.Configurations.Add(new Confuration2()); modelBuilder.Configurations.Add(new Confuration3()); modelBuilder.Configurations.Add(new GroupConfiguration()); base.OnModelCreating(modelBuilder); } public DbSet&lt;Group&gt; Groups { get; set; } public DbSet&lt;Foo&gt; Foos { get; set; } public DbSet&lt;Bar&gt; Bars { get; set; } } </code></pre> <p><strong>Unit of work:</strong></p> <pre><code>public class UnitOfWork&lt;TContext&gt; : IDisposable where TContext: DbContext, new() { public TContext Context { get; private set; } public UnitOfWork(TContext context) { Context = context; } public UnitOfWork() : this(new TContext()) { } public void Commit() { Context.SaveChanges(); } public void Dispose() { } } </code></pre> <p><strong>Usage:</strong></p> <pre><code>using (var unit = new UnitOfWork&lt;SurveyContext&gt;()) { //ViewDataSynchronize.SynchronizeSectionsForGroup(context, model.SectionIds, model.Group); var repo = new GroupRepository(unit); repo.Add(model.Group); unit.Commit(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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