【问题标题】:ServiceStack and entity framework Lazy LoadingServiceStack 和实体框架延迟加载
【发布时间】:2014-11-12 02:09:10
【问题描述】:

我正在将我们的 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 能否在不给应用程序带来过多负担的情况下实现数据访问的完全自动化过程?

【问题讨论】:

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


    【解决方案1】:

    如果您使用的是 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();
        }
    }
    

    【讨论】:

    • 你的方法非常有趣,我现在将打开以查看更多示例。
    • 当然!看看其他人是否回答,他们会如何回答,很有趣! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多