Skip to content

Instantly share code, notes, and snippets.

@mgroves
Last active August 29, 2015 13:59
Show Gist options
  • Select an option

  • Save mgroves/10928249 to your computer and use it in GitHub Desktop.

Select an option

Save mgroves/10928249 to your computer and use it in GitHub Desktop.
// 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