2012-11-07 97 views
9

任何人都可以指导我,我可以怎样使用Autofac RavenDB注册?使用Autofac注册RavenDb?

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

+1

这里有一个相关的问题:http://stackoverflow.com/questions/10940988/how-to-configure-simple-injector-ioc-to-use-ravendb。它谈到简单的喷油器,但它对于Autofac来说几乎是一样的。 – Steven

回答

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个不同的用户可能会在同一时间下订单。他们将分别得到他们自己的会议。

的SaveChanges把在OnRelease事件是可选的,但将节省您不必把它在每一个服务。

在现实世界中,这可能是一个Web应用程序或服务总线的应用程序,在这种情况下,您的会话应该作用域到单web请求或消息的分别寿命。

如果您使用的是ASP.Net WebApi,则应该关闭NuGet的Autofac.WebApi包并使用它们的.InstancePerApiRequest()方法,该方法会自动创建适当的生存期范围。

+0

谢谢!奇迹般有效! – trailmax