使用Autofac注册RavenDb? [英] Register RavenDb using Autofac?

查看:81
本文介绍了使用Autofac注册RavenDb?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以指导我如何使用Autofac注册RavenDB吗?

Can anyone guide me on how I could register RavenDB using Autofac?

builder.Register<DocumentStore>( ..之后呢?

推荐答案

这里是一个示例控制台程序,它不仅说明了如何建立文档存储库,而且还说明了如何设置它,以便您可以注入文档会话:

Here is a sample console program that illustrates not only how to wire up the document store, but also how to set it up so you can just inject your document session:

using System.Threading.Tasks;
using Autofac;
using Raven.Client;
using Raven.Client.Document;

namespace ConsoleApplication1
{
  internal class Program
  {
    private static void Main()
    {
      var builder = new ContainerBuilder();

      // Register the document store as single instance,
      // initializing it on first use.
      builder.Register(x =>
        {
          var store = new DocumentStore { Url = "http://localhost:8080" };
          store.Initialize();
          return store;
        })
           .As<IDocumentStore>()
           .SingleInstance();

      // Register the session, opening a new session per lifetime scope.
      builder.Register(x => x.Resolve<IDocumentStore>().OpenSession())
           .As<IDocumentSession>()
           .InstancePerLifetimeScope()
           .OnRelease(x =>
             {
               // When the scope is released, save changes
               //  before disposing the session.
               x.SaveChanges();
               x.Dispose();
             });

      // Register other services as you see fit
      builder.RegisterType<OrderService>().As<IOrderService>();

      var container = builder.Build();


      // Simulate some activity.  5 users are placing orders simultaneously.
      Parallel.For(0, 5, i =>
        {
          // Each user gets their own scope.  In the real world this would be
          // a new inbound call, such as a web request, and you would let an
          // autofac plugin create the scope rather than creating it manually.
          using (var scope = container.BeginLifetimeScope())
          {
            // Let's do it.  Again, in the real world you would just inject
            // your service to something already wired up, like an MVC
            // controller.  Here, we will resolve the service manually.
            var orderService = scope.Resolve<IOrderService>();
            orderService.PlaceOrder();
          }
        });
    }
  }

  // Define the order service
  public interface IOrderService
  {
    void PlaceOrder();
  }

  public class OrderService : IOrderService
  {
    private readonly IDocumentSession _session;

    // Note how the session is being constructor injected
    public OrderService(IDocumentSession session)
    {
      _session = session;
    }

    public void PlaceOrder()
    {
      _session.Store(new Order { Description = "Stuff", Total = 100.00m });

      // we don't have to call .SaveChanges() here because we are doing it
      // globally for the lifetime scope of the session.
    }
  }

  // Just a sample of something to save into raven.
  public class Order
  {
    public string Id { get; set; }
    public string Description { get; set; }
    public decimal Total { get; set; }
  }
}

请注意,DocumentStore是单个实例,但是DocumentSession是每个生存期范围内的实例.对于此示例,我将手动创建生存期范围并进行并行处理,以模拟5个不同的用户可能如何同时下订单.他们每个人都有自己的会话.

Note that DocumentStore is single instance, but DocumentSession is instance per lifetime scope. For this sample, I am manually creating the lifetime scopes and doing it in parallel, simulating how 5 different users might be placing orders at the same time. They will each get their own session.

在OnRelease事件中放置SaveChanges是可选的,但是可以避免您将其放入每个服务中.

Putting SaveChanges in the OnRelease event is optional, but will save you from having to put it in every service.

在现实世界中,这可能是Web应用程序或服务总线应用程序,在这种情况下,您的会话应分别限制为单个Web请求或消息的生存期.

In the real world, this might be a web application, or a service bus application, in which case your session should be scoped to either the single web request or the lifetime of the message, respectively.

如果使用的是ASP.Net WebApi,则应从NuGet处获取Autofac.WebApi程序包,并使用其.InstancePerApiRequest()方法,该方法将自动创建适当的生存期范围.

If you are using ASP.Net WebApi, you should go get the Autofac.WebApi package off NuGet and use their .InstancePerApiRequest() method, which automatically creates the appropriate lifetime scope.

这篇关于使用Autofac注册RavenDb?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆