141 lines
4.9 KiB
C#
141 lines
4.9 KiB
C#
using taloyhtio.idp.parser.common.domain;
|
|
using Microsoft.Azure.CosmosDB.Table;
|
|
using Microsoft.Azure.Storage;
|
|
using Serilog;
|
|
using System;
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace taloyhtio.idp.parser.common.repository
|
|
{
|
|
public class BaseRepository<T> : IBaseRepository<T> where T : Entity
|
|
{
|
|
private static CloudStorageAccount CreateStorageAccountFromConnectionString(string storageConnectionString)
|
|
{
|
|
CloudStorageAccount storageAccount;
|
|
try
|
|
{
|
|
storageAccount = CloudStorageAccount.Parse(storageConnectionString);
|
|
}
|
|
catch (FormatException)
|
|
{
|
|
Console.WriteLine("Invalid storage account information provided. Please confirm the AccountName and AccountKey are valid in the app.config file - then restart the application.");
|
|
throw;
|
|
}
|
|
catch (ArgumentException)
|
|
{
|
|
Console.WriteLine("Invalid storage account information provided. Please confirm the AccountName and AccountKey are valid in the app.config file - then restart the sample.");
|
|
Console.ReadLine();
|
|
throw;
|
|
}
|
|
|
|
return storageAccount;
|
|
}
|
|
|
|
protected readonly CloudTable _table;
|
|
protected readonly ILogger _log;
|
|
private readonly EventsHandlerFactory _eventsFactory;
|
|
|
|
public BaseRepository(ILogger log, string storageConnectionString, EventsHandlerFactory eventsFactory)
|
|
{
|
|
_log = log;
|
|
_eventsFactory = eventsFactory;
|
|
|
|
CloudStorageAccount storageAccount = CreateStorageAccountFromConnectionString(storageConnectionString);
|
|
|
|
// Create a table client for interacting with the table service
|
|
CloudTableClient tableClient = storageAccount.CreateCloudTableClient(
|
|
new TableConnectionPolicy()
|
|
{
|
|
UseDirectMode = false
|
|
}); // new TableClientConfiguration());
|
|
|
|
var tableName = typeof(T).Name;
|
|
System.Attribute[] attrs = Attribute.GetCustomAttributes(typeof(T));
|
|
if (attrs != null && attrs.Any())
|
|
{
|
|
var tableAttr = (attrs.FirstOrDefault() as TableAttribute);
|
|
if (tableAttr != null)
|
|
tableName = tableAttr.Name;
|
|
}
|
|
// Create a table client for interacting with the table service
|
|
var table = tableClient.GetTableReference(tableName);
|
|
if (table.CreateIfNotExists())
|
|
{
|
|
//Console.WriteLine("Created Table named: {0}", tableName);
|
|
}
|
|
else
|
|
{
|
|
//Console.WriteLine("Table {0} already exists", tableName);
|
|
}
|
|
|
|
this._table = table;
|
|
}
|
|
|
|
|
|
public async Task DeleteEntityAsync(T deleteEntity)
|
|
{
|
|
try
|
|
{
|
|
if (deleteEntity == null)
|
|
{
|
|
throw new ArgumentNullException("deleteEntity");
|
|
}
|
|
|
|
if (deleteEntity.DomainEvents != null)
|
|
{
|
|
foreach (var evt in deleteEntity.DomainEvents)
|
|
{
|
|
_eventsFactory.ResolveAndRun(evt);
|
|
}
|
|
}
|
|
|
|
TableOperation deleteOperation = TableOperation.Delete(deleteEntity);
|
|
TableResult result = await _table.ExecuteAsync(deleteOperation);
|
|
}
|
|
catch (StorageException e)
|
|
{
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
public async Task<T> SaveChanges(T entity, bool skipEntitySaving = false)
|
|
{
|
|
try
|
|
{
|
|
if (entity.DomainEvents != null)
|
|
{
|
|
foreach (var evt in entity.DomainEvents)
|
|
{
|
|
_eventsFactory.ResolveAndRun(evt);
|
|
}
|
|
}
|
|
|
|
if (skipEntitySaving)
|
|
{
|
|
return entity;
|
|
}
|
|
|
|
_log.Information($"Start SaveChanges");
|
|
TableOperation insertOrMergeOperation = TableOperation.InsertOrMerge(entity);
|
|
_log.Information($"Execute the operation.");
|
|
TableResult result = await _table.ExecuteAsync(insertOrMergeOperation);
|
|
_log.Information($"End SaveChanges");
|
|
|
|
return result.Result as T;
|
|
}
|
|
catch (StorageException e)
|
|
{
|
|
_log.Error($"Error SaveChanges StorageException {e.Message} {e.StackTrace} {e.InnerException}", e.Message);
|
|
throw e;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_log.Error($"Error SaveChanges Exception {e.Message} {e.StackTrace} {e.InnerException}", e.Message);
|
|
throw e;
|
|
}
|
|
}
|
|
}
|
|
}
|