private async Task <IEnumerable <T> > Find <T, TIncType>(Expression <Func <T, bool> > filter, Expression <Func <T, object> > field, Expression <Func <T, TIncType> > targetField, string order, int pageIndex, int size, bool isDescending, CancellationToken cancellationToken = default) where T : BaseEntity where TIncType : BaseEntity { var res = await Find <T>(filter, order, pageIndex, size, isDescending, cancellationToken); if (res == null || !res.Any()) { return(res); } var incFieldValues = res.Select(field.Compile()).ToList(); var tran = new QueryBuilder <T>(); tran.Evaluate(ObjectExt.GetExpression <TIncType>(c => incFieldValues.Contains((c as BaseDbEntity).Id))); var where = tran.Sql; where = where.Replace("WHERE", "WHERE Id"); var resInc = await _dapper.GetListAsync <TIncType>(_connection, where, tran.Parameters, _transaction, Timeout, cancellationToken); foreach (var item in res) { var tValue = item.GetPropertyValue <T, int>(field.PropertyName()); var obj = resInc.FirstOrDefault(c => c.GetPropertyValue <TIncType, int>("Id") == tValue); item.SetPropertyValue(targetField, obj); } return(res); }
/// <summary> /// Override this method to perform cmd-line validation. It is recommended to call the base method. /// </summary> /// <returns></returns> protected virtual string[] Validate() { var errors = new List <string>(); foreach (CmdLineProperty prop in Properties) { if (!prop.Error.IsEmpty()) { errors.Add(string.Format("{0} has an error: {1}", prop.Name, prop.Error)); } if (prop.Required && !prop.PropertySet) { errors.Add(string.Format("{0} is required.", prop.Name)); } foreach (ICustomValidator validator in prop.Validators) { if (!validator.IsValid(prop.Value)) { errors.Add(validator.FormatErrorMessage(prop.Name)); } } } ValidationResult[] results = ObjectExt.Validate(this); foreach (ValidationResult result in results) { if (result.ErrorMessage.HasValue()) { errors.Add(result.ErrorMessage); } } return(errors.ToArray()); }
public void Update(String id, TaskModel data) { Boolean exist = Find(id); if (exist) { TaskModel task = Get(id); ObjectExt.TransferTo(data, task); } }
private string[] ValidateCmdLineObject(T obj) { var errors = new List <string>(); var results = ObjectExt.Validate(obj); foreach (var result in results) { if (result.ErrorMessage.HasValue()) { errors.Add(result.ErrorMessage); } } return(errors.ToArray()); }
/// <inheritdoc /> public async Task Add <T>(T entity, CancellationToken cancellationToken = default) where T : BaseEntity { var id = await _dapper.InsertAsync(_connection, entity, _transaction, Timeout, cancellationToken); ObjectExt.SetPropertyValue(entity, "Id", id); }