简体   繁体   English

ServiceStack和实体框架的延迟加载

[英]ServiceStack and entity framework Lazy Loading

I'm migrating our WCF web services to ServiceStack. 我正在将WCF Web服务迁移到ServiceStack。 Supose we have 3 entities. 假设我们有3个实体。 Let them be A, B and C. 让它们成为A,B和C。

  • A is father of B. A是B的父亲。
  • A is father of C. A是C的父亲。

In WCF we would have 3 methods to get A with his children: 在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();
}

I'm having enormous difficult to translate this process to ServiceStack manner. 我很难将此过程转换为ServiceStack方式。 Can you guys provide some examples? 你们能提供一些例子吗?

The best I came up with is: 我想到的最好的是:

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

We know we cannot use WCF with lazyloading, but can ServiceStack and ormlite do the trick of fully automated process of data access without overcharging the application? 我们知道我们不能将WCF与延迟加载一起使用,但是ServiceStack和ormlite能否在不过度收费应用程序的情况下实现完全自动化的数据访问过程的窍门?

If you're using EF, I would probably do something like this: 如果您使用的是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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM