2012-11-07 35 views
9

Có ai có thể hướng dẫn tôi cách tôi có thể đăng ký RavenDB bằng Autofac không?Đăng ký RavenDb bằng Autofac?

builder.Register<DocumentStore>( .. sau đó là gì?

+1

Đây là câu hỏi có liên quan: http://stackoverflow.com/questions/10940988/how-to-configure-simple-injector-ioc-to-use-ravendb. Nó nói về Simple Injector, nhưng nó sẽ khá giống với Autofac. – Steven

Trả lời

15

Dưới đây là một chương trình mẫu console minh họa không chỉ làm thế nào để dây lên các cửa hàng văn bản, mà còn làm thế nào để thiết lập nó vì vậy bạn chỉ có thể tiêm phiên tài liệu của bạn:

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; } 
    } 
} 

Lưu ý rằng DocumentStore là đơn Ví dụ, nhưng DocumentSession là instance cho mỗi phạm vi đời. Đối với mẫu này, tôi tạo thủ công phạm vi cuộc đời và thực hiện song song, mô phỏng cách 5 người dùng khác nhau có thể đặt hàng cùng một lúc. Mỗi người sẽ có phiên riêng của họ.

Đặt SaveChang trong sự kiện OnRelease là tùy chọn, nhưng sẽ giúp bạn không phải đặt nó trong mọi dịch vụ.

Trong thế giới thực, đây có thể là ứng dụng web hoặc ứng dụng dịch vụ xe buýt, trong trường hợp đó phiên của bạn phải được dò theo yêu cầu web đơn hoặc thời lượng của thư tương ứng.

Nếu bạn đang sử dụng ASP.Net WebApi, bạn nên lấy gói Autofac.WebApi khỏi NuGet và sử dụng phương thức .InstancePerApiRequest() của họ, tự động tạo phạm vi thời gian phù hợp.

+0

cảm ơn bạn! hoạt động như một sự quyến rũ! – trailmax

Các vấn đề liên quan