使用Autofac注册RavenDb?

编程入门 行业动态 更新时间:2024-10-26 03:23:10
本文介绍了使用Autofac注册RavenDb?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有人可以指导我如何使用Autofac注册RavenDB吗?

Can anyone guide me on how I could register RavenDB using Autofac?

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

推荐答案

这里是一个示例控制台程序,它不仅说明了如何建立文档存储库,而且还说明了如何设置它,以便您可以注入文档会话:

Here is a sample console program that illustrates not only how to wire up the document store, but also how to set it up so you can just inject your document session:

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 = "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个不同的用户可能如何同时下订单.他们每个人都有自己的会话.

Note that DocumentStore is single instance, but DocumentSession is instance per lifetime scope. For this sample, I am manually creating the lifetime scopes and doing it in parallel, simulating how 5 different users might be placing orders at the same time. They will each get their own session.

在OnRelease事件中放置SaveChanges是可选的,但是可以避免您将其放入每个服务中.

Putting SaveChanges in the OnRelease event is optional, but will save you from having to put it in every service.

在现实世界中,这可能是Web应用程序或服务总线应用程序,在这种情况下,您的会话应分别限制为单个Web请求或消息的生存期.

In the real world, this might be a web application, or a service bus application, in which case your session should be scoped to either the single web request or the lifetime of the message, respectively.

如果使用的是ASP.Net WebApi,则应从NuGet处获取Autofac.WebApi程序包,并使用其.InstancePerApiRequest()方法,该方法将自动创建适当的生存期范围.

If you are using ASP.Net WebApi, you should go get the Autofac.WebApi package off NuGet and use their .InstancePerApiRequest() method, which automatically creates the appropriate lifetime scope.

更多推荐

使用Autofac注册RavenDb?

本文发布于:2023-11-04 12:27:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557937.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Autofac   RavenDb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!