Entity Framework DbSet dynamic query - c#

I am creating a DbSet from a Type that is passed in and I need to query the database for a dynamic field and value. Using generics I would use the where function with a lambda expression. Can this be achieved from a standard dbSet created as follows ?
DbSet table = dataContext.Set(EntityType);
PropertyInfo propertyInfo = EntityType.GetProperty(PropertyName);
// Expression: "entity"
ParameterExpression parameter = Expression.Parameter(EntityType, "entity");
// Expression: "entity.PropertyName"
MemberExpression propertyValue = Expression.MakeMemberAccess(parameter, propertyInfo);
// Expression: "value"
object convertedValue = Convert.ChangeType(value, propertyInfo.PropertyType);
ConstantExpression rhs = Expression.Constant(convertedValue);
// Expression: "entity.PropertyName == value"
BinaryExpression equal = Expression.Equal(propertyValue, rhs);
// Expression: "entity => entity.PropertyName == value"
LambdaExpression lambda = Expression.Lambda(equal, parameter);
Now need to query the table to get data.

There is a nuget package called 'System.Linq.Dynamic'.
http://dynamiclinq.codeplex.com/
This package allows you to form statements against DbSets using strings, like this:
myContext.MyDbSet.Where("PropertyName == #0", "aValue");
It is possible to do this, with Expressions as you suggest in your question, but this library takes out all the heavy lifting.
I've used this with great success in one of my previous projects.

You can try something like the following:
public class UniqueRecordValidationAttribute : ValidationAttribute
{
public IValidationAttribute DynamicInstance { get; private set; }
public UniqueRecordValidationAttribute(Type type,
params object[] arguments )
{
DynamicInstance =
Activator.CreateInstance(type, arguments) as IValidationAttribute;
}
public override bool IsValid(object value)
{
return DynamicInstance.IsValid(value);
}
}
public class UniqueRecordValidator<C, E, P> : IValidationAttribute
where C : DataContext, new() where E : class
{
Func<E, P, bool> Check { get; set; }
public UniqueRecordValidator(Func<E, P, bool> check)
{
Check = check;
}
public bool IsValid(object value)
{
DataContext dataContext = new C();
Table<E> table = dataContext.GetTable<E>();
return table.Count(i => Check(i as E, (P)value)) == 0;
}
}
public interface IValidationAttribute
{
bool IsValid(object value);
}
and
[UniqueRecordValidation(
typeof(UniqueRecordValidator<AssetTrackingEntities, ATUser_Account, string>),
new Func<ATUser_Account, string, bool>((i, p) => i.User_Login == p))]
public string User_Name { get; set; }
This would be a completely strongly typed solution, but I am not shure the Func<E, P, bool> inside the Count is supported by EF as I could not test that here at the moment. But for LINQ to objects this code does work.
If this does not work you can at least improve it with generics and dynamic LINQ to the following:
public class UniqueRecordValidator<C, E> : IValidationAttribute
where C : DataContext, new() where E : class
{
string PropertyName { get; set; }
public UniqueRecordValidator(string propertyName)
{
PropertyName = propertyName;
}
public bool IsValid(object value)
{
DataContext dataContext = new C();
Table<E> table = dataContext.GetTable<E>();
return table.Count(PropertyName + " = #0", value) == 0;
}
}
[UniqueRecordValidation(
typeof(UniqueRecordValidator<AssetTrackingEntities, ATUser_Account>)
"User_Login")]
public string User_Login { get; set; }

Related

Dynamic Linq Expression Query Nested List Object

Is it possible to dynamically build an IQueryable/Linq Expression with filtering criteria based on a NESTED/Child List Object's Property.
I have not included all code here - particularly the code around Pagination but I hope there is sufficient detail. Things to note is my use of EFCore5 and Automapper ProjectTo extension method.
For Example:
public class PersonModel
{
public int Id { get; set; }
public PersonName Name { get; set; }
public List<Pet> Pets { get; set; }
}
[Owned]
public class PersonName
{
public string Surname { get; set; }
public string GivenNames { get; set; }
}
public class Pet
{
public string Name { get; set; }
public string TypeOfAnimal { get; set; }
}
Here is my WebApi Controller.
[HttpGet(Name = nameof(GetAllPersons))]
public async Task<ActionResult<IEnumerable<PersonDTO>>> GetAllPersons(
[FromQuery] QueryStringParameters parameters)
{
IQueryable<Person> persons = _context.Persons;
parameters.FilterClauses.ForEach(filter =>
persons = persons.Where(filter.name, filter.op, filter.val));
// Note the use of 'Where' Extension Method.
var dTOs = persons
.ProjectTo<PersonDTO>(_mapper.ConfigurationProvider);;
var pagedPersons = PaginatedList<PersonDTO>
.CreateAsync(dTOs, parameters);
return Ok(await pagedPersons);
}
To query for all people with a Name.GivenNames property equal to "John" I would issue a GET call such as;
http://127.0.0.1/api/v1.0/?Filter=Name.GivenNames,==,John
This works perfectly fine.
However I would like to query for all people with a Pet with a Name property equal to "Scruffy" I would issue a GET call such as;
http://127.0.0.1/api/v1.0/?Filter=Pets.Name,==,Scruffy
Somewhat expectedly it throws the following exception on the line of code in BuildPredicate Function. This is because "Pets" is Type is a "List"... not a "Pet"
var left = propertyName.Split...
Instance property 'Pet:Name' is not defined for type
System.Collections.Generic.List`1[Person]' (Parameter 'propertyName')
Here are the Extension Methods.
public static class ExpressionExtensions
{
public static IQueryable<T> Where<T>(this IQueryable<T> source, string propertyName, string comparison, string value)
{
return source.Where(BuildPredicate<T>(propertyName, comparison, value));
}
}
public static Expression<Func<T, bool>> BuildPredicate<T>(string propertyName, string comparison, string value)
{
var parameter = Expression.Parameter(typeof(T), "x");
var left = propertyName.Split('.').Aggregate((Expression)parameter, Expression.Property);
var body = MakeComparison(left, comparison, value);
return Expression.Lambda<Func<T, bool>>(body, parameter);
}
private static Expression MakeComparison(Expression left, string comparison, string value)
{
switch (comparison)
{
case "==":
return MakeBinary(ExpressionType.Equal, left, value);
case "!=":
return MakeBinary(ExpressionType.NotEqual, left, value);
case ">":
return MakeBinary(ExpressionType.GreaterThan, left, value);
case ">=":
return MakeBinary(ExpressionType.GreaterThanOrEqual, left, value);
case "<":
return MakeBinary(ExpressionType.LessThan, left, value);
case "<=":
return MakeBinary(ExpressionType.LessThanOrEqual, left, value);
case "Contains":
case "StartsWith":
case "EndsWith":
return Expression.Call(MakeString(left), comparison, Type.EmptyTypes, Expression.Constant(value, typeof(string)));
default:
throw new NotSupportedException($"Invalid comparison operator '{comparison}'.");
}
}
private static Expression MakeString(Expression source)
{
return source.Type == typeof(string) ? source : Expression.Call(source, "ToString", Type.EmptyTypes);
}
private static Expression MakeBinary(ExpressionType type, Expression left, string value)
{
object typedValue = value;
if (left.Type != typeof(string))
{
if (string.IsNullOrEmpty(value))
{
typedValue = null;
if (Nullable.GetUnderlyingType(left.Type) == null)
left = Expression.Convert(left, typeof(Nullable<>).MakeGenericType(left.Type));
}
else
{
var valueType = Nullable.GetUnderlyingType(left.Type) ?? left.Type;
typedValue = valueType.IsEnum ? Enum.Parse(valueType, value) :
valueType == typeof(Guid) ? Guid.Parse(value) :
valueType == typeof(DateTimeOffset) ? DateTimeOffset.ParseExact(value, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal) :
Convert.ChangeType(value, valueType);
}
}
var right = Expression.Constant(typedValue, left.Type);
return Expression.MakeBinary(type, left, right);
}
Is there anyway to adapt this code to detect that if one of the nested properties is a LIST that it builds an 'Inner Predicate' to do a query on the child collection? ie: Enumerable.Any() ?
Working with raw expression tree's, it sometimes helps to start with an example, let the C# compiler have a go at it, and work backwards. eg;
Expression<Func<Person,bool>> expr = p => p.Pets.Any(t => t.Foo == "blah");
Though the compiler does take a shortcut in IL to specify type members that can't be decompiled.
The trick here is to make your method recursive. Instead of assuming that you can get each property;
var left = propertyName.Split('.').Aggregate((Expression)parameter, Expression.Property);
If you find a collection property in the list, you need to call BuildPredicate<Pet> with the remaining property string. Then use the return value as the argument to call .Pets.Any(...).
Perhaps something like;
public static Expression<Func<T, bool>> BuildPredicate<T>(string propertyName, string comparison, string value)
=> (Expression<Func<T, bool>>)BuildPredicate(typeof(T), propertyName.Split('.'), comparison, value);
public static LambdaExpression BuildPredicate(Type t, Span<string> propertyNames, string comparison, string value)
{
var parameter= Expression.Parameter(t, "x");
var p = (Expression)parameter;
for(var i=0; i<propertyNames.Length; i++)
{
var method = p.Type.GetMethods().FirstOrDefault(m => m.Name == "GetEnumerator" && m.ReturnType.IsGenericType);
if (method != null)
{
BuildPredicate(method.ReturnType.GetGenericArguments()[0], propertyNames.Slice(i), comparison, value);
// TODO ...
}
else
p = Expression.Property(p, propertyNames[i]);
}
// TODO ...
return Expression.Lambda(body, parameter);
}

Can a string-based Include alternative be created in Entity Framework Core?

On an API I need dynamic include, but EF Core does not support string-based include.
Because of this, I created a mapper which maps strings to lambda expressions added to a list as:
List<List<Expression>> expressions = new List<List<Expression>>();
Consider the following specific types:
public class EFContext
{
public DbSet<P1> P1s { get; set; }
public DbSet<P1> P2s { get; set; }
public DbSet<P1> P3s { get; set; }
}
public class P1
{
public P2 P2 { get; set; }
public P3 P3 { get; set; }
}
public class P2
{
public P3 P3 { get; set; }
}
public class P3 { }
Include and ThenInclude are normally used as follows:
EFContext efcontext = new EFContext();
IQueryable<P1> result = efcontext.P1s
.Include(p1 => p1.P2)
.ThenInclude(p2 => p2.P3)
.Include(p1 => p1.P3);
They can also be used the following way:
Expression<Func<P1, P2>> p1p2 = p1 => p1.P2;
Expression<Func<P1, P3>> p1p3 = p1 => p1.P3;
Expression<Func<P2, P3>> p2p3 = p2 => p2.P3;
List<List<Expression>> expressions = new List<List<Expression>>
{
new List<Expression> { p1p2, p1p3 },
new List<Expression> { p2p3 }
};
EFContext efcontext = new EFContext();
IIncludableQueryable<P1, P2> q1 = EntityFrameworkQueryableExtensions
.Include(efcontext.P1s, p1p2);
IIncludableQueryable<P1, P3> q2 = EntityFrameworkQueryableExtensions
.ThenInclude(q1, p2p3);
IIncludableQueryable<P1, P3> q3 = EntityFrameworkQueryableExtensions
.Include(q2, p1p3);
result = q3.AsQueryable();
The problem is that my method receives a list of Expressions and I only have the base type in T:
public static class IncludeExtensions<T>
{
public static IQueryable<T> IncludeAll(this IQueryable<T> collection, List<List<Expression>> expressions)
{
MethodInfo include = typeof(EntityFrameworkQueryableExtensions)
.GetTypeInfo()
.GetDeclaredMethods(nameof(EntityFrameworkQueryableExtensions.Include))
.Single(mi => mi.GetParameters()
.Any(pi => pi.Name == "navigationPropertyPath"));
MethodInfo includeAfterCollection = typeof(EntityFrameworkQueryableExtensions)
.GetTypeInfo()
.GetDeclaredMethods(nameof(EntityFrameworkQueryableExtensions.ThenInclude))
.Single(mi =>
!mi.GetParameters()[0].ParameterType.GenericTypeArguments[1].IsGenericParameter);
MethodInfo includeAfterReference = typeof(EntityFrameworkQueryableExtensions)
.GetTypeInfo()
.GetDeclaredMethods(nameof(EntityFrameworkQueryableExtensions.ThenInclude))
.Single(mi => mi.GetParameters()[0].ParameterType.GenericTypeArguments[1].IsGenericParameter);
foreach (List<Expression> path in expressions)
{
bool start = true;
foreach (Expression expression in path)
{
if (start)
{
MethodInfo method = include.MakeGenericMethod(typeof(T), ((LambdaExpression)expression).ReturnType);
IIncludableQueryable<T,?> result = method.Invoke(null, new Object[] { collection, expression });
start = false;
}
else
{
MethodInfo method = includeAfterReference.MakeGenericMethod(typeof(T), typeof(?), ((LambdaExpression)expression).ReturnType);
IIncludableQueryable <T,?> result = method.Invoke(null, new Object[] { collection, expression });
}
}
}
return collection; // (to be replaced by final as Queryable)
}
}
The main problem has been resolving the correct types for each Include and ThenInclude step and also which ThenInclude to use.
Is this even possible with the current EF7 Core? Did someone find a solution for dynamic Include?
The Include and ThenIncludeAfterReference and ThenIncludeAfterCollection methods are part of EntityFrameworkQueryableExtensions class in EntityFramework Github's repository.
Update:
Starting with v1.1.0, the string based include is now part of EF Core, so the issue and the below solution are obsolete.
Original answer:
Interesting exercise for the weekend.
Solution:
I've ended up with the following extension method:
public static class IncludeExtensions
{
private static readonly MethodInfo IncludeMethodInfo = typeof(EntityFrameworkQueryableExtensions).GetTypeInfo()
.GetDeclaredMethods(nameof(EntityFrameworkQueryableExtensions.Include)).Single(mi => mi.GetParameters().Any(pi => pi.Name == "navigationPropertyPath"));
private static readonly MethodInfo IncludeAfterCollectionMethodInfo = typeof(EntityFrameworkQueryableExtensions).GetTypeInfo()
.GetDeclaredMethods(nameof(EntityFrameworkQueryableExtensions.ThenInclude)).Single(mi => !mi.GetParameters()[0].ParameterType.GenericTypeArguments[1].IsGenericParameter);
private static readonly MethodInfo IncludeAfterReferenceMethodInfo = typeof(EntityFrameworkQueryableExtensions).GetTypeInfo()
.GetDeclaredMethods(nameof(EntityFrameworkQueryableExtensions.ThenInclude)).Single(mi => mi.GetParameters()[0].ParameterType.GenericTypeArguments[1].IsGenericParameter);
public static IQueryable<TEntity> Include<TEntity>(this IQueryable<TEntity> source, params string[] propertyPaths)
where TEntity : class
{
var entityType = typeof(TEntity);
object query = source;
foreach (var propertyPath in propertyPaths)
{
Type prevPropertyType = null;
foreach (var propertyName in propertyPath.Split('.'))
{
Type parameterType;
MethodInfo method;
if (prevPropertyType == null)
{
parameterType = entityType;
method = IncludeMethodInfo;
}
else
{
parameterType = prevPropertyType;
method = IncludeAfterReferenceMethodInfo;
if (parameterType.IsConstructedGenericType && parameterType.GenericTypeArguments.Length == 1)
{
var elementType = parameterType.GenericTypeArguments[0];
var collectionType = typeof(ICollection<>).MakeGenericType(elementType);
if (collectionType.IsAssignableFrom(parameterType))
{
parameterType = elementType;
method = IncludeAfterCollectionMethodInfo;
}
}
}
var parameter = Expression.Parameter(parameterType, "e");
var property = Expression.PropertyOrField(parameter, propertyName);
if (prevPropertyType == null)
method = method.MakeGenericMethod(entityType, property.Type);
else
method = method.MakeGenericMethod(entityType, parameter.Type, property.Type);
query = method.Invoke(null, new object[] { query, Expression.Lambda(property, parameter) });
prevPropertyType = property.Type;
}
}
return (IQueryable<TEntity>)query;
}
}
Test:
Model:
public class P
{
public int Id { get; set; }
public string Info { get; set; }
}
public class P1 : P
{
public P2 P2 { get; set; }
public P3 P3 { get; set; }
}
public class P2 : P
{
public P4 P4 { get; set; }
public ICollection<P1> P1s { get; set; }
}
public class P3 : P
{
public ICollection<P1> P1s { get; set; }
}
public class P4 : P
{
public ICollection<P2> P2s { get; set; }
}
public class MyDbContext : DbContext
{
public DbSet<P1> P1s { get; set; }
public DbSet<P2> P2s { get; set; }
public DbSet<P3> P3s { get; set; }
public DbSet<P4> P4s { get; set; }
// ...
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<P1>().HasOne(e => e.P2).WithMany(e => e.P1s).HasForeignKey("P2Id").IsRequired();
modelBuilder.Entity<P1>().HasOne(e => e.P3).WithMany(e => e.P1s).HasForeignKey("P3Id").IsRequired();
modelBuilder.Entity<P2>().HasOne(e => e.P4).WithMany(e => e.P2s).HasForeignKey("P4Id").IsRequired();
base.OnModelCreating(modelBuilder);
}
}
Usage:
var db = new MyDbContext();
// Sample query using Include/ThenInclude
var queryA = db.P3s
.Include(e => e.P1s)
.ThenInclude(e => e.P2)
.ThenInclude(e => e.P4)
.Include(e => e.P1s)
.ThenInclude(e => e.P3);
// The same query using string Includes
var queryB = db.P3s
.Include("P1s.P2.P4", "P1s.P3");
How it works:
Given a type TEntity and a string property path of the form Prop1.Prop2...PropN, we split the path and do the following:
For the first property we just call via reflection the EntityFrameworkQueryableExtensions.Include method:
public static IIncludableQueryable<TEntity, TProperty>
Include<TEntity, TProperty>
(
this IQueryable<TEntity> source,
Expression<Func<TEntity, TProperty>> navigationPropertyPath
)
and store the result. We know TEntity and TProperty is the type of the property.
For the next properties it's a bit more complex. We need to call one of the following ThenInclude overloads:
public static IIncludableQueryable<TEntity, TProperty>
ThenInclude<TEntity, TPreviousProperty, TProperty>
(
this IIncludableQueryable<TEntity, ICollection<TPreviousProperty>> source,
Expression<Func<TPreviousProperty, TProperty>> navigationPropertyPath
)
and
public static IIncludableQueryable<TEntity, TProperty>
ThenInclude<TEntity, TPreviousProperty, TProperty>
(
this IIncludableQueryable<TEntity, TPreviousProperty> source,
Expression<Func<TPreviousProperty, TProperty>> navigationPropertyPath
)
source is the current result. TEntity is one and the same for all calls. But what is TPreviousProperty and how we decide which method to call.
Well, first we use a variable to remember what was the TProperty in the previous call. Then we check if it is a collection property type, and if yes, we call the first overload with TPreviousProperty type extracted from the generic arguments of the collection type, otherwise simply call the second overload with that type.
And that's all. Nothing fancy, just emulating an explicit Include / ThenInclude call chains via reflection.
String-based Include() shipped in EF Core 1.1. I would suggest you try upgrading and removing any workarounds you had to add to your code to address this limitation.
Creating an "IncludeAll" extension on query will require a different approach from what you have initially done.
EF Core does expression interpretation. When it sees the .Include method, it interprets this expression into creating additional queries. (See RelationalQueryModelVisitor.cs and IncludeExpressionVisitor.cs).
One approach would be to add an additional expression visitor that handles your IncludeAll extension. Another (and probably better) approach would be to interpret the expression tree from .IncludeAll to the appropriate .Includes and then let EF handle the includes normally. An implementation of either is non-trivial and beyond the scope of a SO answer.
String-based Include() shipped in EF Core 1.1. If you keep this extension you will get error "Ambiguous match found". I spent half day to search solution for this error. Finally i removed above extension and error was resolved.

Getting the callee's name of an extension method

I've built up a simple ArgumentValidator class in order to simplify argument preconditions in any given method. Most of them are null or bounds checks and it gets pretty tedious after a couple of
if (arg == null ) throw new ArgumentNullException(nameof(arg));
So I've come up with the following set up:
public static class ArgumentValidator
{
public interface IArgument<T>
{
string ParamName { get; }
T Value { get; }
}
private class Argument<T>: IArgument<T>
{
public Argument(T argument, string paramName)
{
ParamName = paramName;
Value = argument;
}
public string ParamName { get; }
public T Value { get; }
}
public static IArgument<T> Validate<T>(this T argument, string paramName = null)
{
return new Argument<T>(argument, paramName ?? string.Empty);
}
public static IArgument<T> IsNotNull<T>(this IArgument<T> o)
{
if (ReferenceEquals(o.Value, null))
throw new ArgumentNullException(o.ParamName);
return o;
}
public static IArgument<T> IsSmallerThan<T, Q>(this IArgument<T> o, Q upperBound) where T : IComparable<Q> { ... }
//etc.
}
And I can use it in the following way:
public Bar Foo(object flob)
{
flob.Validate(nameof(flob)).IsNotNull().IsSmallerThan(flob.MaxValue);
}
Ideally I'd love to get rid of nameof(flob) in the Validate call and ultimately get rid of Validate alltogether; the only purpose of Validate is to avoid having to pass nameof(...) on every check down the chain.
Is there a way to get the name flob inside the Validate() method?
Doing that with an extension method is not that easy. It is easier with a static method that takes an LINQ expression (derived from devdigital's answer here):
public static T Validate<T>(this Expression<Func<T>> argument)
{
var lambda = (LambdaExpression)argument;
MemberExpression memberExpression;
if (lambda.Body is UnaryExpression)
{
var unaryExpression = (UnaryExpression)lambda.Body;
memberExpression = (MemberExpression)unaryExpression.Operand;
}
else
{
memberExpression = (MemberExpression)lambda.Body;
}
string name = memberExpression.Member.Name;
Delegate d = lambda.Compile();
return (T)d.DynamicInvoke();
}
The name inside is the name of the property you put in the method:
MyMethods.Validate(() => o);
Since the Validate returns T, you can use that further on. This might not be as performing as you want it to be, but this is the only viable option.
It is possible to make this an extension method too, you have to create the expression yourself by hand:
Expression<Func<object>> f = () => o; // replace 'object' with your own type
f.Validate();

Linq query - use string as property?

Code:
foreach (PropertyInfo prop in typeof(SomeObject).GetProperties())
{
if (Attribute.IsDefined(prop, typeof(SomeCustomAttribute)))
{
column.Expression(p => p.Name);
}
}
I want to add columns which have SomeCustomAttribute data-annotation assigned.
How to use prop.Name (name of the property) as an property instead of manual p.Name?
For example..
...
column.Expression(p => prop.Name);
...
column.Expression is
ITableColumn Expression<TProperty>(Expression<Func<TModel, TProperty>> expression)
Suppose you have this types defined:
interface ITableColumn { }
class SomeObject
{
public int MyProperty { get; set; }
}
class Column<TModel>
{
public ITableColumn Expression<TProperty>(Expression<Func<TModel, TProperty>> expression)
{
// just a stub
return null;
}
}
To invoke Column.Expression for single property, obtained via reflection, you have to build member expression first:
private static LambdaExpression MakeMemberExpression(PropertyInfo propertyInfo)
{
var instanceExpression = Expression.Parameter(propertyInfo.DeclaringType);
return Expression.Lambda(Expression.MakeMemberAccess(instanceExpression, propertyInfo), instanceExpression);
}
As long as you don't know TProperty statically, you don't need to build strongly typed lambda (Expression<Func<TModel, TProperty>>).
Now, let's invoke it:
var property = typeof(SomeObject)
.GetProperty("MyProperty");
var columnType = typeof(Column<>)
.MakeGenericType(typeof(SomeObject));
var expressionMethod = columnType
.GetMethod("Expression")
.MakeGenericMethod(property.PropertyType);
var expr = MakeMemberExpression(property);
expressionMethod.Invoke(new Column<SomeObject>(), new[] { expr });
Hope this helps.

Linq expressions and extension methods to get property name

I was looking at this post that describes a simple way to do databinding between POCO properties: Data Binding POCO Properties
One of the comments by Bevan included a simple Binder class that can be used to accomplish such data binding. It works great for what I need but I would like to implement some of the suggestions that Bevan made to improve the class, namely:
Checking that source and target are
assigned
Checking that the properties
identified by sourcePropertyName and
targetPropertyName exist
Checking for type compatibility
between the two properties
Also, given that specifying properties by string is error prone, you could use Linq expressions and extension methods instead. Then instead of writing
Binder.Bind( source, "Name", target, "Name")
you could write
source.Bind( Name => target.Name);
I'm pretty sure I can handle the first three (though feel free to include those changes) but I have no clue how to use Linq expressions and extension methods to be able to write code without using property name strings.
Any tips?
Here is the original code as found in the link:
public static class Binder
{
public static void Bind(
INotifyPropertyChanged source,
string sourcePropertyName,
INotifyPropertyChanged target,
string targetPropertyName)
{
var sourceProperty
= source.GetType().GetProperty(sourcePropertyName);
var targetProperty
= target.GetType().GetProperty(targetPropertyName);
source.PropertyChanged +=
(s, a) =>
{
var sourceValue = sourceProperty.GetValue(source, null);
var targetValue = targetProperty.GetValue(target, null);
if (!Object.Equals(sourceValue, targetValue))
{
targetProperty.SetValue(target, sourceValue, null);
}
};
target.PropertyChanged +=
(s, a) =>
{
var sourceValue = sourceProperty.GetValue(source, null);
var targetValue = targetProperty.GetValue(target, null);
if (!Object.Equals(sourceValue, targetValue))
{
sourceProperty.SetValue(source, targetValue, null);
}
};
}
}
The following will return a property name as a string from a lambda expression:
public string PropertyName<TProperty>(Expression<Func<TProperty>> property)
{
var lambda = (LambdaExpression)property;
MemberExpression memberExpression;
if (lambda.Body is UnaryExpression)
{
var unaryExpression = (UnaryExpression)lambda.Body;
memberExpression = (MemberExpression)unaryExpression.Operand;
}
else
{
memberExpression = (MemberExpression)lambda.Body;
}
return memberExpression.Member.Name;
}
Usage:
public class MyClass
{
public int World { get; set; }
}
...
var c = new MyClass();
Console.WriteLine("Hello {0}", PropertyName(() => c.World));
UPDATE
public static class Extensions
{
public static void Bind<TSourceProperty, TDestinationProperty>(this INotifyPropertyChanged source, Expression<Func<TSourceProperty, TDestinationProperty>> bindExpression)
{
var expressionDetails = GetExpressionDetails<TSourceProperty, TDestinationProperty>(bindExpression);
var sourcePropertyName = expressionDetails.Item1;
var destinationObject = expressionDetails.Item2;
var destinationPropertyName = expressionDetails.Item3;
// Do binding here
Console.WriteLine("{0} {1}", sourcePropertyName, destinationPropertyName);
}
private static Tuple<string, INotifyPropertyChanged, string> GetExpressionDetails<TSourceProperty, TDestinationProperty>(Expression<Func<TSourceProperty, TDestinationProperty>> bindExpression)
{
var lambda = (LambdaExpression)bindExpression;
ParameterExpression sourceExpression = lambda.Parameters.FirstOrDefault();
MemberExpression destinationExpression = (MemberExpression)lambda.Body;
var memberExpression = destinationExpression.Expression as MemberExpression;
var constantExpression = memberExpression.Expression as ConstantExpression;
var fieldInfo = memberExpression.Member as FieldInfo;
var destinationObject = fieldInfo.GetValue(constantExpression.Value) as INotifyPropertyChanged;
return new Tuple<string, INotifyPropertyChanged, string>(sourceExpression.Name, destinationObject, destinationExpression.Member.Name);
}
}
Usage:
public class TestSource : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string Name { get; set; }
}
public class TestDestination : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string Id { get; set; }
}
class Program
{
static void Main(string[] args)
{
var x = new TestSource();
var y = new TestDestination();
x.Bind<string, string>(Name => y.Id);
}
}
This question is very similar to: Retrieving Property name from lambda expression
(Cross-posting answer from https://stackoverflow.com/a/17220748/1037948)
I don't know if you need to bind to "subproperties", but inspecting the lambda.Body for Member.Name will only return the "final" property, not a "fully-qualified" property.
ex) o => o.Thing1.Thing2 would result in Thing2, not Thing1.Thing2.
This is problematic when trying to use this method to simplify EntityFramework DbSet.Include(string) with expression overloads.
So you can "cheat" and parse the Expression.ToString instead. Performance seemed comparable in my tests, so please correct me if this is a bad idea.
The Extension Method
/// <summary>
/// Given an expression, extract the listed property name; similar to reflection but with familiar LINQ+lambdas. Technique #via https://stackoverflow.com/a/16647343/1037948
/// </summary>
/// <remarks>Cheats and uses the tostring output -- Should consult performance differences</remarks>
/// <typeparam name="TModel">the model type to extract property names</typeparam>
/// <typeparam name="TValue">the value type of the expected property</typeparam>
/// <param name="propertySelector">expression that just selects a model property to be turned into a string</param>
/// <param name="delimiter">Expression toString delimiter to split from lambda param</param>
/// <param name="endTrim">Sometimes the Expression toString contains a method call, something like "Convert(x)", so we need to strip the closing part from the end</pa ram >
/// <returns>indicated property name</returns>
public static string GetPropertyName<TModel, TValue>(this Expression<Func<TModel, TValue>> propertySelector, char delimiter = '.', char endTrim = ')') {
var asString = propertySelector.ToString(); // gives you: "o => o.Whatever"
var firstDelim = asString.IndexOf(delimiter); // make sure there is a beginning property indicator; the "." in "o.Whatever" -- this may not be necessary?
return firstDelim < 0
? asString
: asString.Substring(firstDelim+1).TrimEnd(endTrim);
}//-- fn GetPropertyNameExtended
(Checking for the delimiter might even be overkill)
This is likely more than or not exactly what you asked for but I've done something similar to handle mapping of a property between two objects:
public interface IModelViewPropagationItem<M, V>
where M : BaseModel
where V : IView
{
void SyncToView(M model, V view);
void SyncToModel(M model, V view);
}
public class ModelViewPropagationItem<M, V, T> : IModelViewPropagationItem<M, V>
where M : BaseModel
where V : IView
{
private delegate void VoidDelegate();
public Func<M, T> ModelValueGetter { get; private set; }
public Action<M, T> ModelValueSetter { get; private set; }
public Func<V, T> ViewValueGetter { get; private set; }
public Action<V, T> ViewValueSetter { get; private set; }
public ModelViewPropagationItem(Func<M, T> modelValueGetter, Action<V, T> viewValueSetter)
: this(modelValueGetter, null, null, viewValueSetter)
{ }
public ModelViewPropagationItem(Action<M, T> modelValueSetter, Func<V, T> viewValueGetter)
: this(null, modelValueSetter, viewValueGetter, null)
{ }
public ModelViewPropagationItem(Func<M, T> modelValueGetter, Action<M, T> modelValueSetter, Func<V, T> viewValueGetter, Action<V, T> viewValueSetter)
{
this.ModelValueGetter = modelValueGetter;
this.ModelValueSetter = modelValueSetter;
this.ViewValueGetter = viewValueGetter;
this.ViewValueSetter = viewValueSetter;
}
public void SyncToView(M model, V view)
{
if (this.ViewValueSetter == null || this.ModelValueGetter == null)
throw new InvalidOperationException("Syncing to View is not supported for this instance.");
this.ViewValueSetter(view, this.ModelValueGetter(model));
}
public void SyncToModel(M model, V view)
{
if (this.ModelValueSetter == null || this.ViewValueGetter == null)
throw new InvalidOperationException("Syncing to Model is not supported for this instance.");
this.ModelValueSetter(model, this.ViewValueGetter(view));
}
}
This allows you to create an instance of this object and then use "SyncToModel" and "SyncToView" to move values back and forth. The following piece that goes with this allows you to group multiple of these things and move data back and forth with one call:
public class ModelViewPropagationGroup<M, V> : List<IModelViewPropagationItem<M, V>>
where M : BaseModel
where V : IView
{
public ModelViewPropagationGroup(params IModelViewPropagationItem<M, V>[] items)
{
this.AddRange(items);
}
public void SyncAllToView(M model, V view)
{
this.ForEach(o => o.SyncToView(model, view));
}
public void SyncAllToModel(M model, V view)
{
this.ForEach(o => o.SyncToModel(model, view));
}
}
Usage would look something like this:
private static readonly ModelViewPropagationItem<LoginModel, ILoginView, string> UsernamePI = new ModelViewPropagationItem<LoginModel, ILoginView, string>(m => m.Username.Value, (m, x) => m.Username.Value = x, v => v.Username, (v, x) => v.Username = x);
private static readonly ModelViewPropagationItem<LoginModel, ILoginView, string> PasswordPI = new ModelViewPropagationItem<LoginModel, ILoginView, string>(m => m.Password.Value, (m, x) => m.Password.Value = x, v => v.Password, (v, x) => v.Password = x);
private static readonly ModelViewPropagationGroup<LoginModel, ILoginView> GeneralPG = new ModelViewPropagationGroup<LoginModel, ILoginView>(UsernamePI, PasswordPI);
public UserPrincipal Login_Click()
{
GeneralPG.SyncAllToModel(this.Model, this.View);
return this.Model.DoLogin();
}
Hope this helps!
var pr = typeof(CCategory).GetProperties().Select(i => i.Name).ToList(); ;
declaration:
class Foo<T> {
public string Bar<T, TResult>(Expression<Func<T, TResult>> expersion)
{
var lambda = (LambdaExpression)expersion;
MemberExpression memberExpression;
if (lambda.Body is UnaryExpression)
{
var unaryExpression = (UnaryExpression)lambda.Body;
memberExpression = (MemberExpression)unaryExpression.Operand;
}
else
{
memberExpression = (MemberExpression)lambda.Body;
}
return memberExpression.Member.Name;
}
}
Usage:
var foo = new Foo<DummyType>();
var propName = foo.Bar(d=>d.DummyProperty)
Console.WriteLine(propName); //write "DummyProperty" string in shell

Categories