Created
October 15, 2013 16:00
-
-
Save Boggin/ceafa55421ecb2585711 to your computer and use it in GitHub Desktop.
Repository pattern infrastructure (with FrameLog auditing).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Demo.Repository.Infrastructure | |
{ | |
public class ApplicationContext : DbContext | |
{ | |
public readonly FrameLogModule<ChangeSet, Person> Logger; | |
public ApplicationContext() : base("name=Demo") | |
{ | |
Configuration.ProxyCreationEnabled = false; | |
Database.Initialize(true); | |
this.Logger = new FrameLogModule<ChangeSet, Person>(new ChangeSetFactory(), this.FrameLogContext); | |
} | |
public IDbSet<Country> Countries { get; set; } | |
public IDbSet<Issue> Issues { get; set; } | |
#region logging | |
public DbSet<ChangeSet> ChangeSets { get; set; } | |
public IFrameLogContext<ChangeSet, Person> FrameLogContext | |
{ | |
get { return new FrameLogContextAdapter(this); } | |
} | |
public HistoryExplorer<ChangeSet, Person> HistoryExplorer | |
{ | |
get { return new HistoryExplorer<ChangeSet, Person>(this.FrameLogContext); } | |
} | |
public DbSet<ObjectChange> ObjectChanges { get; set; } | |
public DbSet<PropertyChange> PropertyChanges { get; set; } | |
public int Save(Person author) | |
{ | |
return this.Logger.SaveChanges(author, SaveOptions.AcceptAllChangesAfterSave); | |
} | |
#endregion | |
protected override void OnModelCreating(DbModelBuilder modelBuilder) | |
{ | |
base.OnModelCreating(modelBuilder); | |
modelBuilder.Entity<Issue>() | |
.HasMany(x => x.AdditionalCountries) | |
.WithMany(x => x.Issues) | |
.Map(x => | |
{ | |
x.ToTable("CountriesIssues"); | |
x.MapLeftKey("Issue_Id"); | |
x.MapRightKey("Country_Code"); | |
}); | |
modelBuilder.Entity<Issue>() | |
.Property(p => p.LogDate) | |
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Demo.Repository.Infrastructure | |
{ | |
public class ContextFactory : IContextFactory | |
{ | |
private ApplicationContext context; | |
public ApplicationContext Get() | |
{ | |
return this.context ?? (this.context = new ApplicationContext()); | |
} | |
public void Dispose() | |
{ | |
if (this.context != null) | |
{ | |
this.context.Dispose(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Demo.Repository.Infrastructure | |
{ | |
public class FrameLogContextAdapter : DbContextAdapter<ChangeSet, Person> | |
{ | |
private readonly ApplicationContext context; | |
public FrameLogContextAdapter(ApplicationContext context) : base(context) | |
{ | |
this.context = context; | |
} | |
public override IQueryable<IChangeSet<Person>> ChangeSets | |
{ | |
get { return this.context.ChangeSets; } | |
} | |
public override IQueryable<IObjectChange<Person>> ObjectChanges | |
{ | |
get { return this.context.ObjectChanges; } | |
} | |
public override IQueryable<IPropertyChange<Person>> PropertyChanges | |
{ | |
get { return this.context.PropertyChanges; } | |
} | |
public override Type UnderlyingContextType | |
{ | |
get { return typeof(ApplicationContext); } | |
} | |
public override void AddChangeSet(ChangeSet changeSet) | |
{ | |
this.context.ChangeSets.Add(changeSet); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Demo.Repository.Infrastructure | |
{ | |
public class GenericRepository<T> : RepositoryBase where T : class | |
{ | |
private readonly ApplicationContext context; | |
public GenericRepository(IUnitOfWork unitOfWork) : base(unitOfWork) | |
{ | |
this.context = this.UnitOfWork.ApplicationContext; | |
this.DataSet = this.context.Set<T>(); | |
} | |
protected IDbSet<T> DataSet { get; private set; } | |
public void Add(T entity) | |
{ | |
this.DataSet.Attach(entity); | |
this.context.Entry(entity).State = EntityState.Added; | |
} | |
public void Update(T entity) | |
{ | |
this.DataSet.Attach(entity); | |
this.context.Entry(entity).State = EntityState.Modified; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Demo.Repository.Infrastructure | |
{ | |
public class RepositoryBase | |
{ | |
public RepositoryBase(IUnitOfWork unitOfWork) | |
{ | |
if (unitOfWork == null) | |
{ | |
throw new ArgumentNullException("unitOfWork"); | |
} | |
this.UnitOfWork = unitOfWork; | |
} | |
protected IUnitOfWork UnitOfWork { get; private set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Demo.Repository.Infrastructure | |
{ | |
public class UnitOfWork : IUnitOfWork | |
{ | |
private ApplicationContext applicationContext; | |
public UnitOfWork(IContextFactory contextFactory) | |
{ | |
this.ContextFactory = contextFactory; | |
} | |
public ApplicationContext ApplicationContext | |
{ | |
get { return this.applicationContext ?? (this.applicationContext = this.ContextFactory.Get()); } | |
} | |
private IContextFactory ContextFactory { get; set; } | |
public int Commit() | |
{ | |
return this.ApplicationContext.SaveChanges(); | |
} | |
/// <summary> | |
/// Audit the change. | |
/// </summary> | |
/// <param name="user">The author of the change.</param> | |
/// <returns>Result of SaveChanges().</returns> | |
public int Commit(Person user) | |
{ | |
return this.ApplicationContext.Save(user); | |
} | |
public void Rollback() | |
{ | |
this.ApplicationContext.Dispose(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment