/// <summary> /// Saves or update entity with the specified id /// </summary> /// <typeparam name="T">Type of entity</typeparam> /// <typeparam name="TId">Id type</typeparam> /// <param name="id">Id of the existing entity or null for a new one</param> /// <param name="action">Update action</param> /// <returns></returns> protected async Task <T> SaveOrUpdateEntityAsync <T, TId>(TId?id, Func <T, Task> action) where T : class, IEntity <TId> where TId : struct { var item = id.HasValue ? await GetEntityAsync <T, TId>(id.Value) : (T)Activator.CreateInstance(typeof(T)); await action.Invoke(item); await DynamicRepo.SaveOrUpdateAsync(item); return(item); }
/// <summary> /// Returns entity of the specified type with the specified <paramref name="id"/> /// </summary> /// <typeparam name="T">Type of entity</typeparam> /// <typeparam name="TId">Id type</typeparam> /// <param name="id">Id of the entity</param> /// <param name="throwException">Throw exception if entity not found</param> /// <returns></returns> protected async Task <T> GetEntityAsync <T, TId>(TId id, bool throwException = true) where T : class, IEntity <TId> { var item = await DynamicRepo.GetAsync(typeof(T), id.ToString()); if (item != null) { return((T)item); } if (throwException) { throw new UserFriendlyException($"{typeof(T).Name} with the specified id `{id}` not found"); } return(null); }
public DynamicAPIController(IConfiguration configuration) { _connectionString = Helper.ConnectionString(configuration); //DynamicRepo = new DynamicRepo(configuration); DynamicRepo = new DynamicRepo(_connectionString); }