I am trying to configure a common interface for retrieving objects from a repository. The problem is that I need to request data from the WCF service, and Generics do not work with operations contracts, from what I see.
So, I have this that works in a console application without using a utility call:
public virtual List<T> GetAll<T>() where T : MyBaseType { return this.datacontext.Set<T>().Include(l => l.RelationshipEntity).ToList(); }
The only way I could see is something like:
public virtual List<MyBaseType> GetAll(Type entityType) { return this.datacontext.Set(entityType).Include(l => l.RelationshipEntity).ToList(); }
Set<T>() and Set(Type type) both return a DbSet , but Set(Type type) has no extension for using ToList() , and I wonβt get all my results.
The Local property shows only the context in the current execution area, and not what is contained in the repository.
So, I want to have a WCF contract as follows:
[ServiceContract] public interface IRulesService { [OperationContract] MyBaseType Add(MyBaseType entity); [OperationContract] List<MyBaseType> GetAll(Type type); }
Then implementation:
public virtual List<MyBaseType> GetAll(Type entityType) { var dbset = this.datacontext.Set(entityType); string sql = String.Format("select * from {0}s", type.Name); Type listType = typeof(List<>).MakeGenericType(entityType); List<MyBaseType> list = new List<MyBaseType>(); IEnumerator result = dbset.SqlQuery(sql).GetEnumerator(); while (result.MoveNext()){ list.Add(result.Current as MyBaseType); } return list; } //public virtual List<T> GetAll<T>() where T : MyBaseType //{ // return this.datacontext.Set<T>().Include(l => l.RelationshipEntity).ToList(); //} public virtual MyBaseType Add(MyBaseType entity) { DbSet set = this.datacontext.Set(typeof(entity)); set.Add(entity); this.datacontext.SaveChanges(); return entity; } //public virtual T Add<T>(T t) where T : MyBaseType //{ // this.datacontext.Set<T>().Add(t); // this.datacontext.SaveChanges(); // return t; //} public virtual List<MyBaseType> UpdateAll(List<MyBaseType> entities) { }
What ideas work best?
Gabe
source share