使用Autofac注册RavenDb?

9
有人能指导我如何使用Autofac注册RavenDB吗? builder.Register<DocumentStore>(.. 然后是什么?

1
这里有一个相关的问题:https://dev59.com/_GTWa4cB1Zd3GeqPGLqH。它涉及到Simple Injector,但对于Autofac来说基本相同。 - Steven
1个回答

15
以下是关于如何连接文档存储并设置其以便您只需注入文档会话的示例控制台程序:
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个不同用户同时下订单的情况。他们将各自获得自己的会话。
在OnRelease事件中放置SaveChanges是可选的,但可以避免您在每个服务中都放置它。
在现实世界中,这可能是一个Web应用程序或服务总线应用程序,在这种情况下,您的会话应分别针对单个Web请求或消息的生命周期进行作用域设置。
如果您正在使用ASP.Net WebApi,则应获取NuGet上的Autofac.WebApi包,并使用其.InstancePerApiRequest()方法,该方法会自动创建适当的生命周期范围。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接