【问题标题】:servicestack vs template and ravendbservicestack vs 模板和 ravendb
【发布时间】:2014-11-13 18:54:38
【问题描述】:

我正在使用servicestack vs 模板和这个问题 I'm suggested 使用 Service.Interface 项目作为一个地方 我应该在哪里接触数据库。

之前我使用AppHost,它是Configure 方法 配置DocumentStore

因为我在 Service.Interface 项目上没有 Configure 方法 我应该如何配置DocumentStore以便进行通信 使用 ravendb?

【问题讨论】:

    标签: c# .net angularjs servicestack ravendb


    【解决方案1】:

    解决方案很简单。在您的服务类 (ServiceInteface) 中,您应该注入 ravendb 会话

    public class MyServices : Service{
       private readonly IDocumentSession session;
       public MyServices(IDocumentSession session){
          this.session = session;
       }
    
       // now use session to query data from database inside your webservice method call
       public object Any(SomeObjectReq request){
          var data = session.Query<ObjectToQuery>().ToList();
       }
    

    由于此项目 (ServiceInteface) 在 AppHost 所在的 Web 项目中被引用,因此您应该将您的数据库基础设施 (IDocumentStore) 放在 AppHost 配置方法中。

    public override void Configure(Container container)
    {
          var store = new DocumentStore(){
              Url = "http://",
              DefaultDatabase = "xxxxxx"
          }.Initialize();
    
          // register ravendb
          container.Register(store);
          container.Register(c =>c.Resolve<IDocumentStore>).OpenSession()).ReusedWithin(ReuseScope.Request);
     }       
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-27
      • 1970-01-01
      • 2010-10-24
      • 1970-01-01
      • 2012-11-04
      • 1970-01-01
      相关资源
      最近更新 更多