Czy ktoś może mi doradzić, w jaki sposób mogę zarejestrować RavenDB przy użyciu Autofac?Zarejestruj się RavenDb przy użyciu Autofac?
builder.Register<DocumentStore>(
.. co dalej?
Czy ktoś może mi doradzić, w jaki sposób mogę zarejestrować RavenDB przy użyciu Autofac?Zarejestruj się RavenDb przy użyciu Autofac?
builder.Register<DocumentStore>(
.. co dalej?
Oto program konsoli przykładowy, który ilustruje nie tylko jak drut sklep dokumentu, ale również w jaki sposób skonfigurować go tak po prostu wstrzyknąć sesji dokumentu:
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; }
}
}
Zauważ, że DocumentStore jest pojedynczy instancja, ale DocumentSession jest instancją dla każdego zakresu życia. W przypadku tej próbki ręcznie kreślę zakresy życia i robię to równolegle, symulując sposób, w jaki 5 różnych użytkowników może składać zamówienia w tym samym czasie. Każda z nich otrzyma własną sesję.
Umieszczanie SaveChanges w zdarzeniu OnRelease jest opcjonalne, ale pozwoli zaoszczędzić na konieczności umieszczenia go w każdej usłudze.
W prawdziwym świecie może to być aplikacja internetowa lub aplikacja magistrali usług. W takim przypadku sesja powinna obejmować zakres odpowiednio jednego żądania internetowego lub okresu istnienia wiadomości.
Jeśli używasz ASP.Net WebApi, powinieneś odłączyć pakiet Autofac.WebApi od NuGet i użyć ich metody .InstancePerApiRequest(), która automatycznie tworzy odpowiedni zakres czasu życia.
dziękuję! działa jak marzenie! – trailmax
Oto powiązane pytanie: http://stackoverflow.com/questions/10940988/how-to-configure-simple-injector-ioc-to-use-ravendb. Mówi o Simple Injector, ale to samo dotyczy Autofac. – Steven