1

私は RavenDB を使用する初心者です。私が理解しているのは、最初に DocumentStore を作成し、次にデータをデータベースに保存するためにセッションを開く必要があるということです。ドキュメントから、毎回インスタンスを作成するべきではなく、DocumentStore の作成にはシングルトンのみを使用する必要があることを理解しています。しかし、ドキュメントやチュートリアルのほとんどは、毎回インスタンスを作成するデモのみであることに気付きました。

参考までに、私は ASP.NET MVC フレームワークを使用しています。

ここで質問です。

  1. CreationDocumentStore.cs (Singleton クラス) はどのフォルダーに配置する必要がありますか? アプリケーションフォルダのルートに?
  2. このシングルトン クラスを作成したら、どのように使用しますか?

以下は、シングルトンを使用する前の私の AdminController の元のコードです。そして、シングルトンクラスを使用するためにそれを変更する方法がわかりません-CreatingDocumentStore.cs

コードを表示して、Singleton の使用方法をデモしていただければ幸いです。前もって感謝します!

コントローラーフォルダーのAdminController.cs

public class AdminController : Controller
{

    public ActionResult Index()
    {

        using (var store = new DocumentStore
        {
            Url = "http://localhost:8080/",
            DefaultDatabase = "foodfurydb"
        })
        {
            store.Initialize();

            using (var session = store.OpenSession())
            {
                session.Store(new Restaurant
                {
                    RestaurantName = "Boxer Republic",
                    ResCuisine = "Western",
                    ResAddress = "Test Address",
                    ResCity = "TestCity",
                    ResState = "TestState",
                    ResPostcode = 82910,
                    ResPhone = "02-28937481"
                });

                session.SaveChanges();

            }
        }

        return View();
    }

    public ActionResult AddRestaurant()
    {
        return View();
    }
}

ルート フォルダーにDocumentStore.cs を作成する

 public class CreatingDocumentStore
{
    public CreatingDocumentStore()
    {
        #region document_store_creation
        using (IDocumentStore store = new DocumentStore()
        {
            Url = "http://localhost:8080"
        }.Initialize())
        {

        }
        #endregion
    }

    #region document_store_holder
    public class DocumentStoreHolder
    {
        private static Lazy<IDocumentStore> store = new Lazy<IDocumentStore>(CreateStore);

        public static IDocumentStore Store
        {
            get { return store.Value; }
        }

        private static IDocumentStore CreateStore()
        {
            IDocumentStore store = new DocumentStore()
            {
                Url = "http://localhost:8080",
                DefaultDatabase = "foodfurydb"
            }.Initialize();

            return store;
        }
    }
    #endregion
}
4

1 に答える 1

1

AyendeがRavenDB ドキュメント ストアのスタートアップを管理する前に彼のブログに投稿したように:

RavenDB のドキュメント ストアは、データベースへの主要なアクセス ポイントです。アクセスするサーバーごとにドキュメント ストアのインスタンスを 1 つだけ持つことを強くお勧めします。これは通常、シングルトンを実装する必要があることを意味し、それに関連する二重チェックのロックのナンセンスがすべて含まれます。

彼は私たちに例を示します:

public static class Global
{
    private static readonly Lazy<IDocumentStore> theDocStore = new Lazy<IDocumentStore>(()=>
        {
            var docStore = new DocumentStore
                {
                    ConnectionStringName = "RavenDB"
                };
            docStore.Initialize();

            //OPTIONAL:
            //IndexCreation.CreateIndexes(typeof(Global).Assembly, docStore);

            return docStore;
        });

    public static IDocumentStore DocumentStore
    {
        get { return theDocStore.Value; }
    }
}

どこに配置するかは、アーキテクチャによって異なります。通常database、接続などを に配置しInfrastructureます。単一のプロジェクトがある場合は、プロジェクトのルートに配置するか、何かを含むフォルダーを作成できますdatabase

これらの投稿は、stackoverflow から確認できます。

于 2016-11-01T11:28:45.860 に答える