实体框架 - 查询适配器

时间:2011-07-27 13:57:36

标签: c# entity-framework business-objects

我正在使用实体框架,我已经用Business Objects(即Account)建立了一个用于整个系统的库。

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();

这样我就没有实体背景......

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

根据您的评论,您可以选择以下选项:

  • 直接在linq-to-entities查询中进行投影,而不是使用mapper
  • 查询您的实体,调用.ToList并使用您的mapper投影对结果运行linq-to-object查询

答案 1 :(得分:0)

您需要一个上下文来连接到您的数据库。但是,通常将其封装在存储库中并包含在中央位置以供使用,而不是为每个操作创建一个上下文。