コード例 #1
0
        public async Task <bool> TryToAddProductAsync(Product <IDescription> product)
        {
            try
            {
                var name = product.Description.GetType().Name + 's';

                // Get DbSet<"TableName"> property
                var property = ReflectionHelper.GetProperty(this, name);
                // Get property value
                var propertyValue = property.GetValue(this);
                // Get property type
                var type = property.PropertyType;

                var productType = product.Description.GetType();

                product.ProductInfo.AddDate   = DateTime.Now;
                product.ProductInfo.TableName = name;

                await Products.AddAsync(product.ProductInfo);
                await SaveChangesAsync();

                var id = Products.Last().Id;
                product.Description.Id = id;

                // Find and invoke method "Add" in property type
                type.GetMethod("Add", new Type[1] {
                    productType
                })
                .Invoke(propertyValue, new object[1] {
                    product.Description
                });
                await SaveChangesAsync();
            }
            catch
            {
                return(false);
            }
            return(true);
        }
コード例 #2
0
ファイル: ManagementService.cs プロジェクト: g0r4n/pet-store
 public async Task AddProductAsync(Product product) => await Products.AddAsync(product);