Ejemplo n.º 1
0
        /// <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);
        }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
0
 public DynamicAPIController(IConfiguration configuration)
 {
     _connectionString = Helper.ConnectionString(configuration);
     //DynamicRepo = new DynamicRepo(configuration);
     DynamicRepo = new DynamicRepo(_connectionString);
 }