public T Add <T>(T entity) where T : class { try { return(_repository.Add(entity)); } finally { var id = _metaData.GetPrimaryKeyValue <T>(entity); var key = CacheKey <T>("GetById", new[] { new { Name = "id", Value = ConvertId(id) } }, false); _delayedCache.Add(() => { SetWriteThroughCache(key, entity); NextGeneration <T>(); }); } }
public static Expression <Func <T, bool> > BuildPrimaryKeyExpression <T>(this IMetaDataProvider repository, T entity) where T : class { var primaryKeyValue = repository.GetPrimaryKeyValue(entity).First(); var primaryKey = repository.GetPrimaryKey <T>().First(); var parameter = Expression.Parameter(typeof(T)); var body = Expression.Convert(Expression.PropertyOrField(parameter, primaryKey), typeof(object)); var idSelector = Expression.Lambda <Func <T, object> >(body, parameter); var predicate = idSelector.BuildOrExpression(new[] { primaryKeyValue }); return(predicate); }
public static Expression <Func <T, bool> > BuildPrimaryKeyExpression <T>(this IMetaDataProvider repository, T entity) where T : class { var primaryKeyValue = repository.GetPrimaryKeyValue(entity); var primaryKey = repository.GetPrimaryKey <T>(); var values = primaryKey.Zip(primaryKeyValue, Tuple.Create).ToArray(); Expression <Func <T, bool> > predicate = null; foreach (var value in values) { var parameter = Expression.Parameter(typeof(T)); var left = Expression.Convert(Expression.PropertyOrField(parameter, value.Item1), value.Item2.GetType()); var body = Expression.Equal(left, Expression.Constant(value.Item2)); predicate = predicate == null?Expression.Lambda <Func <T, bool> >(body, parameter) : predicate.And(Expression.Lambda <Func <T, bool> >(body, parameter)); } return((Expression <Func <T, bool> >)Evaluator.PartialEval(predicate)); }