Я борюсь с платформой сущностей, где я создал библиотеку с бизнес-объектами (то есть с учетной записью), которые используются во всей системе.
public class Account
{
public long AccountId { get; set; }
public string AccountText { get; set; }
}
Затем структура сущностей преобразует их туда и обратнокогда они запрашиваются или требуются для сохранения
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, чтобы я мог сделать что-то вроде
AccountEntityAdapter a = new AccountEntityAdapter();
List<Commonlib.BusinessModels.Account> list = (from account in a
where account.AccountId > 6
select account).ToList();
Так, что яЯ свободен от контекста сущности ...
Как мне этого добиться?