私はこの時点で、複数の取得メソッドで満たされたリポジトリを持っています。EF send
E.q. Get1() => cxt.Entites.Include(e => e.obj1); Get2() => cxt.Entities.Include(e => e.obj1).Include(e => e.obj2)
など。
パラメータを介してincluesを送信できるGETメソッドが1つあるというパターンはありますか?
私はこの時点で、複数の取得メソッドで満たされたリポジトリを持っています。EF send
E.q. Get1() => cxt.Entites.Include(e => e.obj1); Get2() => cxt.Entities.Include(e => e.obj1).Include(e => e.obj2)
など。
パラメータを介してincluesを送信できるGETメソッドが1つあるというパターンはありますか?
public virtual IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "")
{
IQueryable<TEntity> query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
if (orderBy != null)
{
return orderBy(query).ToList();
}
else
{
return query.ToList();
}
}
あなたはラムダ
public virtual IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
Expression<Func<TEntity, object>>[] includes)
{
IQueryable<TEntity> query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
if (includes != null)
{
query = includes.Aggregate(query,
(current, include) => current.Include(include));
}
if (orderBy != null)
{
return orderBy(query).ToList();
}
else
{
return query.ToList();
}
}
と同じ含め
_sampleRepostiory.Get(h=>h.Id>1,null,"Employees.Departments");
を使用することができますmsdn でリポジトリパターンは、この
var query = context.Customers
.Get(x=>x.Id>1,null,
c => c.Address,
c => c.Orders.Select(o => o.OrderItems));
のようにそれを消費を参照してください。 210
私は私のプロジェクトでは、以下のなかった:
public Entity[] GetAll(bool includeObj1, bool includeAllOthers) {
IQueryable<Entity> entity = ctx.Entities;
if (includeObj1)
entity = entity.Include(e => e.obj1);
if (includeAllOthers) {
entity = entity
.Include(e => e.obj2)
.Include(e => e.obj3)
.Include(e => e.obj4)
.Include(e => e.obj5);
}
return entity.ToArray();
}
はincludeObj1
とincludeObj2
実装からリポジトリの消費者を分離し、任意のデータアクセスロジックをカプセル化するような引数を提供します。
"これらのプロパティを含める"命令をリポジトリに直接渡すことは、リポジトリの仕組みを知っていて、抽象をぼかすソートORMであることを前提としています。
ナー、それは良くありません。私は含めるべき多くのオブジェクトを持っています。だから私は7つの引数を持つメソッドを望んでいない。 – Nerf
@Nerf類似の内容の7つ以上のメソッドを持つよりも優れています。 7つの状態オプションに応じて異なる結果を返すメソッドが必要な場合は、7つの引数が必要です。 –
このメソッドを使用する人は、含まれているすべてのプロパティを知り、列挙する必要があります。そして、誰かがプロパティ名を変更することを決めた場合、プロジェクトを通してCtrl + Shift + Fキーを押し、すべてのテキストの用途を変更します。誰かがeveyrwhereに含めるべきプロパティを追加すると、すべての用途を見つけて、すべての文字列に "NewProperty"を追加する必要があります。 SRP違反のように聞こえる。私は[tdykstra](https://github.com/tdykstra)のような人が悪いコードを書くことができるとは信じられないので、理解できないものがあるはずです。それは私に悲しい気がします:( –
私が書いたコード私は、文字列を使用する必要がないところでlamda式を使用しているオーバーロードでメソッドを作成しました。このhttps://stackoverflow.com/questions/5376421/ef-including-other-entities- generic-repository-pattern – Eldho
引数のヌル例外がスローされる;/ – Nerf