2014-03-19 51 views
1

我将我们的WCF Web服务迁移到ServiceStack。根据我们有3个实体。让他们为A,B和CServiceStack和实体框架Lazy Loading

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

在WCF中,我们将有3种方法来获取与他的孩子:

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做的数据存取完全自动化的过程的伎俩,而不滥的应用程序?

回答

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(); 
    } 
} 
+0

非常有趣的你的方法,我将离开开放的,现在看到更多的例子。 – Guardian

+1

当然!看看其他人是否回答,他们会如何接近它,这很有趣! :) – CallumVass