私はここを含むさまざまなソースからリポジトリと作業単位のパターンを学んでいます:
上記の作業単位クラスの作成へのリンクを参照すると、次のようになります。
private GenericRepository<Department> departmentRepository;
private GenericRepository<Course> courseRepository;
これは問題ありませんが、GenericのUnit of Workクラスを拡張し、GenericRepositoriesのコレクションをセットアップして、通過したモデルに基づいて動的に更新できるようにしたいと思いました。
最終的には、コントローラーで次のことを実行したいと思います。
public class LedgerUserController : Controller
{
private GenericUnitOfWork unitOfWork = new GenericUnitOfWork();
LedgerUser ledgeruser = new LedgerUser();
public ActionResult Index()
{
//var ledgerusers = db.LedgerUsers.Include(l => l.Image).Include(l => l.UserType);
var view = unitOfWork.Repository(ledgeruser).Get(l => l.LastName == "smith");
return View(view.ToList());
}
}
これまでの私のクラスとインターフェースは次のとおりです。
IRepository.cs
/// <summary>
/// Generic Repository for CRUD Operations and methods
/// to locate entities within your store. This is not specific to which Data Access
/// tools your are using (Direct SQL, EF, NHibernate, etc).
/// </summary>
public interface IRepository<T> where T : class
{
//--Search Operations
IQueryable<T> GetAll();
IEnumerable<T> GetAllList();
IEnumerable<T> Get(Expression<Func<T,bool>> filter);
T GetIt(Expression<Func<T, bool>> filter);
T GetById(object id);
//--CRUD Operations
void Create(T entity);
void Update(T entity);
void Delete(T entity);
}
GenericRepository.cs
//////Aferエンティティを検索するためのリポジトリクラス///CRUD操作//////public class GenericRepository:IRepository where TEntity:class {
internal AccountsContext context;
internal DbSet<TEntity> dbSet;
internal IQueryable<TEntity> query;
/// <summary>
/// Default Constructor.
/// </summary>
/// <param name="context"></param>
public GenericRepository(AccountsContext context)
{
this.context = context;
this.dbSet = context.Set<TEntity>();
}
#region Methods
#region Search Functionality
/// <summary>
/// Obtain the whole Entity to query if needed.
/// </summary>
/// <returns>IQueryable object.</returns>
public virtual IQueryable<TEntity> GetAll()
{
IQueryable<TEntity> query = dbSet;
return query;
}
/// <summary>
/// Obtain the whole Entity to Enumerate throught if needed.
/// </summary>
/// <returns>IEnumerble object.</returns>
public virtual IEnumerable<TEntity> GetAllList()
{
IQueryable<TEntity> query = dbSet;
return query.ToList();
}
/// <summary>
/// Locate an Entity by its indexed id.
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public virtual TEntity GetById(object id)
{
return dbSet.Find(id);
}
/// <summary>
/// Gets a collection based on LINQ lambda expressions
/// </summary>
/// <param name="filter">Lambda Expression</param>
/// <returns>Query</returns>
public virtual IEnumerable<TEntity> Get(Expression<Func<TEntity, bool>> filter)
{
query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
return this.query.ToList();
}
/// <summary>
/// Gets one record based on a one-to-one relationship.
/// </summary>
/// <param name="filter">Lambda Expression.</param>
/// <returns>One record.</returns>
public virtual TEntity GetIt(Expression<Func<TEntity, bool>> filter)
{
IQueryable<TEntity> query = dbSet;
return query.SingleOrDefault(filter);
}
#endregion
#region CRUD Functionality
/// <summary>
/// Used to create a new entity into the database.
/// </summary>
/// <param name="entity">Entity to create.</param>
public virtual void Create(TEntity entity)
{
dbSet.Add(entity);
}
/// <summary>
/// Used to update an entity that already exists in the
/// database.
/// </summary>
/// <param name="entity">Entity to update.</param>
public virtual void Update(TEntity entity)
{
dbSet.Attach(entity);
context.Entry(entity).State = EntityState.Modified;
}
/// <summary>
/// Used to delete an entity from the database.
/// </summary>
/// <param name="entity">Entity to delete.</param>
public virtual void Delete(TEntity entity)
{
if (context.Entry(entity).State == EntityState.Detached)
{
dbSet.Attach(entity);
}
dbSet.Remove(entity);
}
#endregion
#endregion
}
#endregion
GenericUnitOfWork.cs:
/// <summary>
/// Unit of work class that handles multiple Repositories
/// and shares the context.
/// </summary>
public class GenericUnitOfWork : IUnitOfWork
{
private AccountsContext context = new AccountsContext();
Dictionary<string, GenericRepository<IRepository<IRepositoryEntity>>> repostories = null;
/// <summary>
/// Generic Repository method which checks the repository is available if not,
/// it sets it up.
/// </summary>
/// <param name="entity">Entity</param>
/// <returns>Repository to use.</returns>
public GenericRepository<IRepository<IRepositoryEntity>> Repository (IRepositoryEntity entity)
{
string index = entity.GetType().ToString();
if (!repostories.ContainsKey(index))
{
//Reflections to create the repoositiory if it is not needed.
Type type1 = typeof(GenericRepository<>);
Type[] typeArgs = {typeof(IRepositoryEntity)};
Type constructed = type1.MakeGenericType(typeArgs);
object o = Activator.CreateInstance(constructed);
if(o is GenericRepository<IRepository<IRepositoryEntity>>)
{
var rep = (GenericRepository<IRepository<IRepositoryEntity>>)o;
rep.context = this.context;
repostories.Add(index, rep);
}
}
return this.repostories[index];
}
/// <summary>
/// Save method.
/// </summary>
public void Save()
{
context.SaveChanges();
}
private bool disposed = false;
/// <summary>
/// Dispose the conxtext when finished.
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
context.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}
ラムダ式では元帳ユーザーのLastNameを解決できないため、リポジトリディクショナリとメソッドリポジトリの実装が正しくない(ジェネリック型が間違っている)ことがわかりました。
var view = unitOfWork.Repository(ledgeruser).Get(l => l.LastName == "smith");
私は問題を過剰に設計していますか、それとも、モデルオブジェクトが与えるもの(上記のLedgerUser)に基づいて設定および作成されたジェネリックリポジトリのコレクションを使用してジェネリック作業単位を作成するための優れたクリーンな方法がありますか?