ServiceStack和实体框架延迟加载

时间:2014-03-19 12:37:20

标签: c# entity-framework servicestack ormlite-servicestack

我正在将我们的WCF Web服务迁移到ServiceStack。我们有3个实体。让他们成为A,B和C.

  • A是B的父亲。
  • A是C的父亲。

在WCF中,我们将有3种方法与他的孩子一起获得A:

public List<A> GetAWithBIncluded()
{
    return Repository.A.Include("B").ToList();
}

public List<A> GetAWithCIncluded()
{
    return Repository.A.Include("C").ToList();
}

public List<A> GetAWithBAndCIncluded()
{
    return Repository.A.Include("B").Include("C").ToList();
}

我很难将此过程转换为ServiceStack方式。你们能提供一些例子吗?

我想出的最好的是:

public class AResponse
{
    public List<A> As {get;set;}
    ....//One list of each type.
}

我们知道我们不能在延迟加载时使用WCF,但是ServiceStack和ormlite可以完全自动化数据访问过程,而不会对应用程序过度收费吗?

1 个答案:

答案 0 :(得分:2)

如果您正在使用EF,我可能会这样做:

[Route("/a")]
public class GetAs : IReturn<List<A>>
{
    public bool IncludeB { get; set; }
    public bool IncludeC { get; set; }
}

public class AService : Service
{
    private readonly AContext _aContext;

    public AService(AContext aContext)
    {
        _aContext = aContext;
    }

    public object Get(GetAs req)
    {
        var res = _aContext.As;

        if (req.IncludeB)
            res = res.Include("B");

        if (req.IncludeC)
            res = res.Include("C");

        return res.ToList();
    }
}
相关问题