
یادگیری سی شارپ از مفاهیم پایه تا پروژه محور: شیگرایی، کار با SQL و LINQ، ORMها (Entity Framework)، ساخت پروژه مدیریت رستوران با گزارشات حرفهای و امکانات کامل!
مشاهده بیشتر
یادگیری MVC Core از مبانی تا پیشرفته: شیگرایی، Routing، Entity Framework، امنیت، تست یونیت، Razor، Ajax، و پروژههای کاربردی! یک دوره کامل برای تسلط بر توسعه وب با ASP.NET Core. به صورت حضوری و آنلاین!
مشاهده بیشترمشخصات مقاله
آموزش استفاده از Azure NoSQL در ASP.NET MVC Core 2.0
Azure NoSQL در ASP.NET Core 2.0
مشکل
نحوه استفاده از پایگاه داده Azure NoSQL در ASP.NET Core
راه حل
ایجاد یک class library و اضافه کردن بسته NuGet Microsoft.Azure.DocumentDB.Core.
یک کلاس برای تنظیم کردن تنظیمات اضافه کنید.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 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; } } <button></button> |
یک کلاس repository ایجاد کرده که با یک نوع generic کار می کند.یک constructor و private methods برای مقدار دهی اولیه Azure client اضافه کنید:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | { 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)); } <button></button> |
یک متد برای گرفتن(get) یک یا چند آیتم اضافه کنید.
1 2 3 4 5 6 7 8 9 10 11 12 | 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); } <button></button> |
اکنون متدهای عمومی(public methods) برای repository اضافه کنید:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | 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)); } </t><button></button> |
Repository تزریق(Inject) کرده و استفاده کنید:
1 2 3 4 5 6 7 8 9 | public class MovieService : IMovieService { private readonly IAzureNoSqlRepository< Movie > repository; public MovieService(IAzureNoSqlRepository< Movie > repository) { this.repository = repository; } <button></button> |
در ASP.NET Core Web Application قسمت configure services:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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(); } <button></button> |
مباحثه
کد نمونه نیاز به تنظیم حساب کاربری Azure، پایگاه داده و مجموعه NoSQL دارد. دستورالعمل برای این را می توان در اینجا یافت.
https://docs.microsoft.com/en-gb/azure/cosmos-db/create-documentdb-dotnet#create-a-database-account
شما می توانید Source Code این مقاله را از لینک زیر دانلود کنید: