私は、システム全体で使用されるビジネスオブジェクト(つまりアカウント)を使用してライブラリを作成したエンティティフレームワークを使用しています。彼らは要求されたかEntity Framework - アダプタの照会
public interface EntityAdapter<T> {
T Materialize(long id);
long Dematerialize(T business);
void Dispose(T business);
}
public abstract class EFEntityAdapter<T> : EntityAdapter<T> {
private static MyModel.MyEntities __ctx = null;
protected MyModel.MyEntities _context
{
get
{
if (__ctx == null)
{
__ctx = new MyModel.MyEntities();
}
return __ctx;
}
}
public abstract T Materialize(long id);
public abstract long Dematerialize(T business);
public abstract void Dispose(T business);
}
public class AccountEntityAdapter : EFEntityAdapter<CommonLib.BusinessModels.Account>
{
public override CommonLib.BusinessModels.Account Materialize(long id)
{
Account entity = (from account in _context.Accounts
where account.AccountId == id
select account).FirstOrDefault();
if (entity == null)
return null;
CommonLib.BusinessModels.Account business = new CommonLib.BusinessModels.Account();
business.AccountId = entity.AccountId;
business.AccountText = entity.AccountText;
return business;
}
public override long Dematerialize(CommonLib.BusinessModels.Account business)
{
long id = business.AccountId;
Account entity = (from account in _context.Accounts
where account.AccountId == id
select account).FirstOrDefault();
if (entity == null)
{
if (id > 0)
{
throw new Exception("Account with id: " + id + " does not exists");
}
else
{
entity = new Account();
_context.Accounts.AddObject(entity);
}
}
entity.AccountId = business.AccountId;
entity.AccountText = business.AccountText;
_context.SaveChanges();
business.AccountId = entity.AccountId;
return entity.AccountId;
}
public override void Dispose(CommonLib.BusinessModels.Account business)
{
long id = business.AccountId;
Account entity = (from account in _context.Accounts
where account.AccountId == id
select account).FirstOrDefault();
if (entity == null)
{
throw new Exception("Account with id: " + id + " was not found, but an attempt to delete it was done");
}
_context.DeleteObject(entity);
_context.SaveChanges();
}
}
を保存するために必要なしかし、今、私は私がすることができるよようにLINQとアダプタを使用したいと考えているとき、エンティティフレームワークは、これらの前後に変換され
public class Account
{
public long AccountId { get; set; }
public string AccountText { get; set; }
}
...私は、エンティティコンテキストの自由だと
AccountEntityAdapter a = new AccountEntityAdapter();
List<Commonlib.BusinessModels.Account> list = (from account in a
where account.AccountId > 6
select account).ToList();
このようなような何かを
どうしたらいいですか?
あなたは車輪を再発明しています。ビジネスオブジェクトとして、エンティティを放棄して使用します。そのような広範な複雑さのポイントは何ですか? –
私が知る限り、エンティティオブジェクトをアプリケーションドメインから削除することはできません。情報を別の場所に転送してから、いくつかの変更を加えて返す場合は、他のオブジェクトを使用する必要がありますか? –
これは当てはまりますが、クエリはDTOではなく元のオブジェクトで実行する必要があります。 –