0
在我的數據庫我有很多查詢,這將有一個UI的改變,我有MVC的網站,並希望有那些查找加載一次,並刷新如有變化發生查找表的最佳實踐
我的數據接入層的樣子:
private ClassessEntities Context;
protected Repository()
{
Context = new ArabicEWorldEntities();
}
public IEnumerable<T> Get(Expression<Func<T, bool>> predicate)
{
return Context.CreateObjectSet<T>().Where(predicate).ToList();
}
public T First(Expression<Func<T, bool>> predicate)
{
return Context.CreateObjectSet<T>().Where(predicate).FirstOrDefault();
}
public IEnumerable<T> GetAll()
{
return Context.CreateObjectSet<T>().ToList();
}
public IEnumerable<T> GetAllOrderBy(Func<T, object> keySelector)
{
return Context.CreateObjectSet<T>().OrderBy(keySelector).ToList();
}
public IEnumerable<T> GetAllOrderByDescending(Func<T, object> keySelector)
{
return Context.CreateObjectSet<T>().OrderByDescending(keySelector).ToList();
}
public void Commit()
{
Context.SaveChanges();
}
public void Add(T entity)
{
Context.CreateObjectSet<T>().AddObject(entity);
}
public void Update(T entity)
{
Context.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
Context.SaveChanges();
}
public void Delete(T entity)
{
Context.DeleteObject(entity);
Context.SaveChanges();
}
public void Dispose()
{
if (Context != null)
{
Context.Dispose();
}
GC.SuppressFinalize(this);
}
,並從查找獲取列表我使用的方法類似
private static ClassesEntities Context;
public static IEnumerable<T> GetLookup<T>() where T : System.Data.Objects.DataClasses.EntityObject
{
try
{
var key = typeof(T).Name;
// 1. we need the container for the conceptual model
var container = Context.MetadataWorkspace.GetEntityContainer(
Context.DefaultContainerName, System.Data.Metadata.Edm.DataSpace.CSpace);
// 2. we need the name given to the element set in that conceptual model
var name = container.BaseEntitySets.Where((s) => s.ElementType.Name.Equals(key)).FirstOrDefault().Name;
// 3. finally, we can create a basic query for this set
var query = Context.CreateQuery<T>("[" + name + "]");
return query.ToList();
}
catch (System.Data.EntityException ex)
{
throw new ArgumentException("Invalid Entity Type supplied for Lookup", ex);
}
}
任何想法來處理我上面所討論的情況,曾經和我加載它們F所列變化重新載入我使用Web服務