کانال بله, جهت پشتیبانی و اطلاع رسانی کانال بله, جهت پشتیبانی و اطلاع رسانی
عضویت

آموزش MVC Core-آموزش استفاده از Azure NoSQL در MVC Core

آموزش MVC Core-آموزش استفاده از Azure NoSQL در MVC Core

مشکل

نحوه استفاده از پایگاه داده Azure NoSQL در ASP.NET Core

راه حل

ایجاد یک class library و اضافه کردن بسته NuGet Microsoft.Azure.DocumentDB.Core.
یک کلاس برای تنظیم کردن تنظیمات اضافه کنید.

public class AzureNoSqlSettings  
    {  
        public AzureNoSqlSettings(string endpoint, string authKey,  
                                  string databaseId, string collectionId)  
        {  
            if (string.IsNullOrEmpty(endpoint))  
                throw new ArgumentNullException("Endpoint");  
   
            if (string.IsNullOrEmpty(authKey))  
                throw new ArgumentNullException("AuthKey");  
   
            if (string.IsNullOrEmpty(databaseId))  
                throw new ArgumentNullException("DatabaseId");  
   
            if (string.IsNullOrEmpty(collectionId))  
                throw new ArgumentNullException("CollectionId");  
   
            this.Endpoint = endpoint;  
            this.AuthKey = authKey;  
            this.DatabaseId = databaseId;  
            this.CollectionId = collectionId;  
        }  
   
        public string Endpoint { get; }  
        public string AuthKey { get; }  
        public string DatabaseId { get; }  
        public string CollectionId { get; }  
    }

یک کلاس repository ایجاد کرده که با یک نوع generic کار می کند.یک constructor و private methods برای مقدار دهی اولیه Azure client اضافه کنید:

public AzureNoSqlRepository(AzureNoSqlSettings settings)  
        {  
            this.settings = settings ?? throw new ArgumentNullException("Settings");  
            Init();  
        }  
        private AzureNoSqlSettings settings;  
        private DocumentClient client;  
   
        private void Init()  
        {  
            client = new DocumentClient(  
new Uri(this.settings.Endpoint), this.settings.AuthKey);  
            CreateDatabaseIfNotExistsAsync().Wait();  
            CreateCollectionIfNotExistsAsync().Wait();  
        }  
   
        private async Task CreateDatabaseIfNotExistsAsync()  
        {  
            await client.ReadDatabaseAsync(  
UriFactory.CreateDatabaseUri(this.settings.DatabaseId));  
        }  
   
        private async Task CreateCollectionIfNotExistsAsync()  
        {  
            await client.ReadDocumentCollectionAsync(  
                    UriFactory.CreateDocumentCollectionUri(  
this.settings.DatabaseId, this.settings.CollectionId));  
        }  

یک متد برای گرفتن(get) یک یا چند آیتم اضافه کنید.

private Uri GetCollectionUri()  
        {  
            return UriFactory.CreateDocumentCollectionUri(  
                   this.settings.DatabaseId, this.settings.CollectionId);  
        }  
   
        private Uri GetDocumentUri(string documentId)  
        {  
            return UriFactory.CreateDocumentUri(  
                   this.settings.DatabaseId, this.settings.CollectionId, documentId);  
        }  

اکنون متدهای عمومی(public methods) برای repository اضافه کنید:

public async Task< List< T > > GetList()
        {
            var query = this.client
                            .CreateDocumentQuery< T >(GetCollectionUri())
                            .AsDocumentQuery();

            var results = new List< T >();
            while (query.HasMoreResults)
            {
                results.AddRange(await query.ExecuteNextAsync< T >());
            }

            return results;
        }
        public async Task< T > GetItem(string id)
        {
            Document document = await this.client.ReadDocumentAsync(
GetDocumentUri(id));
            return (T)(dynamic)document;
        }

        public async Task< Document > Insert(T item)
        {
            return await this.client.CreateDocumentAsync(GetCollectionUri(), item);
        }

        public async Task< Document > Update(string id, T item)
        {
            return await this.client.ReplaceDocumentAsync(GetDocumentUri(id), item);
        }

        public async Task< Document > InsertOrUpdate(T item)
        {
            return await this.client.UpsertDocumentAsync(GetCollectionUri(), item);
        }

        public async Task Delete(string id)
        {
            await this.client.DeleteDocumentAsync(GetDocumentUri(id));
        }

Repository تزریق (Inject) کرده و استفاده کنید:

public class MovieService : IMovieService  
    {  
        private readonly IAzureNoSqlRepository< Movie > repository;  
   
        public MovieService(IAzureNoSqlRepository< Movie > repository)  
        {  
            this.repository = repository;  
        }  

در ASP.NET Core Web Application قسمت configure services:

public void ConfigureServices(  
    IServiceCollection services)  
{  
    services.AddScoped< IAzureNoSqlRepository< Movie > >(factory = >
    {
        return new AzureNoSqlRepository< Movie >(
            new AzureNoSqlSettings(
                endpoint: Configuration["NoSql_Endpoint"],
                authKey: Configuration["NoSql_AuthKey"],
                databaseId: Configuration["NoSql_Database"],
                collectionId: Configuration["NoSql_Collection"]));
    });
    services.AddScoped< IMovieService, MovieService >();

    services.AddMvc();
}

مباحثه

کد نمونه نیاز به تنظیم حساب کاربری Azure، پایگاه داده و مجموعه NoSQL دارد. دستورالعمل برای این را می توان در اینجا یافت.

https://docs.microsoft.com/en-gb/azure/cosmos-db/create-documentdb-dotnet#create-a-database-account

شما می توانید Source Code این مقاله را از لینک زیر دانلود کنید:

https://github.com/TahirNaushad/Fiver.Azure.NoSql

شما دانشجویان گرامی می توانید فایل مربوط به این آموزش را از قسمت پایانی این مقاله دانلود نمایید .

1397/03/06 2571 1569
رمز عبور : tahlildadeh.com یا www.tahlildadeh.com
نظرات شما

نظرات خود را ثبت کنید...