如何使RavenDB DocumentStore可用于调用API [英] How to make RavenDB DocumentStore available to calling APIs
问题描述
我有一个使用RavenDB作为数据存储的MVC4应用程序.该应用程序具有MVC/Web,域,数据和安全层.
I have an MVC4 application using RavenDB as a datastore. The application has MVC/Web, Domain, Data, and Security layers.
我正在编写需要初始化数据库和访问DocumentStore的自定义成员资格和角色提供程序.我正在从安全性"层编写这些类,并且想使用单例DocumentStore(在应用程序中设置),但是我不知道如何访问它.
I am writing custom membership and role providers that need to initialize the database and access the DocumentStore. I'm writing these class from the Security layer, and would like to use a singleton DocumentStore (set in the application), but I can't figure out how to access it.
另外,我看到的为RavenDB编写自定义提供程序的示例在Provider.Initialize()方法中创建了新的DocumentStore实例,但这似乎违反了每台服务器只有一个DocumentStore的规则.
Other, examples I see of writing custom providers for RavenDB create new DocumentStore instances within the Provider.Initialize() methods, but that seems to break the rule of having a single DocumentStore per server.
当前,我在Application_Start()中创建RavenDB DocumentStore的单个实例.我在MVC/Web层中有一个用于处理DocumentStore.Session的基本控制器.
Currently, I create a single instance of the RavenDB DocumentStore in Application_Start(). I have a base controller in the MVC/Web layer that handles the DocumentStore.Session(s).
有没有办法做到这一点?我是否应该将安全逻辑移到MVC/Web层以简化操作?
Is there a way of accomplishing this? Should I move my security logic into the MVC/Web layer to simplify things?
推荐答案
我使用单例模式提出了自己的解决方案.
I came up with my own solution using a singleton pattern.
我所做的是在应用程序的数据层中创建一个单例,该单例公开一个公共IDocumentStore属性.它利用静态构造函数,该构造函数在首次请求静态属性时运行(在Application_Start中执行),然后实例化IDocumentStore对象.然后,对于基础控制器和应用程序其他层(例如安全层f.ex.)中对DocStore.Instance的每次引用,都会返回初始实例.
What I did was create a singleton, that exposes a public IDocumentStore property, in the data layer of my application. It utilizes a static constructor which runs upon first request for static property (executed in Application_Start), and in turn instantiates an IDocumentStore object. The initial instance is then then returned for each reference to DocStore.Instance in a base controller and in other layers of my application (like the security layer f.ex.)
public sealed class DocStore
{
protected static readonly IDocumentStore instance;
static DocStore()
{
// instantiate documentStore
instance = new DocumentStore { ConnectionStringName = Constants.ConnectionStrings.XXXXX };
instance.Initialize();
// instantiate tenants
try
{
instance.DatabaseCommands.EnsureDatabaseExists(Constants.Tenants.XXXXX);
}
catch (Exception ex)
{
//TODO: catch exception
throw ex;
}
// initialize indexed
try
{
InitializeIndexes(instance);
}
catch (Exception ex)
{
//TODO: catch exception
throw ex;
}
}
private DocStore()
{
}
public static IDocumentStore Instance
{
get { return instance; }
}
private static void InitializeIndexes(IDocumentStore store)
{
// builds all indexes defined in XXXXX.Data.dll
var dataCatalog = new CompositionContainer(new AssemblyCatalog(typeof(DocStore).Assembly));
IndexCreation.CreateIndexes(dataCatalog,
store.DatabaseCommands.ForDatabase(Constants.Tenants.XXXXX),
store.Conventions);
}
}
这篇关于如何使RavenDB DocumentStore可用于调用API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!