Last active
August 29, 2015 13:59
-
-
Save mgroves/10928249 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // each entity uses this as a base | |
| // different entities have different types of keys (guids, string, ints typically) | |
| public class EntityBase<T> : IEntity<T> | |
| { | |
| public T Id { get; set; } | |
| } | |
| public class SomeEntity : EntityBase<int> | |
| { | |
| public string Foo { get;set;} | |
| public string Bar { get;set;} | |
| } | |
| public class RepositoryBase<TEntity,TId> : IRepository<TEntity, TId> where TEntity : EntityBase<TId> | |
| { | |
| // ... snip ... | |
| public DbSet Entities { | |
| get { return dbset; } | |
| } | |
| public RepositoryBase(MyContext context, DbSet<TEntity> dbset) { | |
| this.dbset = dbset; | |
| this.context = context; | |
| } | |
| public TEntity GetById(TId id) | |
| { | |
| var entity = (TEntity)Entities.Find(id); // <--- is this okay? | |
| return entity; | |
| } | |
| } | |
| public interface ISomeRepository : IRepository<SomeEntity, int> | |
| { | |
| } | |
| public class SomeRepository : RepositoryBase<SomeEntity, int>, ISomeRepository | |
| { | |
| public SomeRepository(MyContext context) : base(context, context.SomeEntities) { } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment