2012-11-07 138 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