I am using Linq-to-SQL with Unity in a Repository pattern. I am trying to add an interceptor for object security on the repository method [Securable]IQueryable<TEntity> List<TEntity>() that intercepts the call and returns only the entities that the user has rights to.
public class SecurableAttribute : HandlerAttribute
{...}
public class SecurableHandler : ICallHandler
{
...
IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
var message = getNext()(input, getNext);
var returnType = message.ReturnValue.GetType();
if (typeof(IQueryable).IsAssignableFrom(returnType))
{
var entityType = returnType.GetGenericArguments().Single();
var securableAttribute = entityType.GetAttribute<SecurableTypeAttribute>();
if(securableAttribute != null)
{
//Build expression to filter the list from the attribute and primary key of the entity
//Return the new IQueryable
}
}
return message;
}
}
I have built an expression, but I can't do message.ReturnValue.Where(expression) since the message.ReturnValue is object (message.ReturnValue is actually a System.Data.Linq.Table<TEntity>, but I don't want to be too tied to L2S), and it is at runtime so I can't cast it back to a generic and replace message.ReturnValue.
Alternatively, I tried
public interface ISecurable<TKey>
{
TKey SecurityId { get; }
}
on the entity, which locks me in a bit, but I am OK with that if I could separate the remaining security aspects. This allows me to do the following in IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext) where I build the expression above:
if(typeof(ISecurableType).IsAssignableFrom(entityType))
{
var secured = ((IQueryable<ISecurable>)message.ReturnValue).Where(expression);
//Need to return secured as IQueryably<TEntity>
}
I now have to cast secured to IQueryable<ISecurable> but typeof(IQueryable<TEntity>).IsAssignableFrom(secured.GetType()) returns false, and swapping out the return value throws an exception, but it does seem to work with delayed execution as far as I can tell. (Also, I don't know TEntity at design time in SecurableHandler, but I do know the reflected type - but I have tried using the class declaration that I know it is in testing.)
Is there any way to modify the return results somehow? I am stuck needing to return a generic that I don't know at design time, thus making that impossible, but I also can't modify the expression (((IQueryable)message.ReturnType).Expression is declared as Expression Expression { get; }).
Is there any brilliance out there that could point me in a way that works?
tl;dr Need to return an IQueryable<TEntity> at runtime from an object that is a Table<TEntity> : IQueryable<TEntity> with an additional .Where(expression).
You can try creating a dynamic expression at runtime. You shouldn't have to explicitly cast the IQueryable back to it's generic type, as long as you don't change the element types with a "Select".
Example:
public class SecurityHandler : ICallHandler
{
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
var message = getNext()(input, getNext);
var returnType = message.ReturnValue.GetType();
if (typeof(IQueryable).IsAssignableFrom(returnType))
{
var entityType = returnType.GetGenericArguments().Single();
var securableAttribute = entityType.GetAttribute<SecurableTypeAttribute>();
if (securableAttribute != null)
{
//Build expression to filter the list from the attribute and primary key of the entity
//Return the new IQueryable
message.ReturnValue = AddWhereExpression(
(IQueryable)message.ReturnValue,
securableAttribute.FilterValues,
securableAttribute.FilterPropertyName);
}
}
return message;
}
public int Order { get; set; }
private static IQueryable AddWhereExpression(IQueryable query, IEnumerable ids, string filterPropertyName)
{
// Build this expression:
// item => ids.Contains(item.[PrimaryKeyPropertyName])
var itemParameter = Expression.Parameter(query.ElementType, "item");
var itemParameterProperty = Expression.Property(itemParameter, filterPropertyName);
var listParameter = Expression.Constant(ids);
var containsExpression = Expression.Call(
typeof(System.Linq.Enumerable),
"Contains",
new[] { typeof(int) },
listParameter,
itemParameterProperty);
var delegateTypeExpression = Expression.GetFuncType(new[] { query.ElementType, typeof(bool) });
var whereExpression = Expression.Lambda(
delegateTypeExpression,
containsExpression,
new[] { itemParameter }
);
Expression callWhere = Expression.Call(
typeof(Queryable),
"Where",
new Type[] { query.ElementType }, // type args for Where<T>()
query.Expression,
whereExpression
);
return query.Provider.CreateQuery(callWhere);
}
}
I am assuming your attribute will provide some array of allowable values.
Here are some extension methods that will help with this process:
public static class TypeExtensions
{
public static TAttribute GetAttribute<TAttribute>(this Type type)
{
var attributes = type.GetCustomAttributes(typeof(TAttribute), true);
if (attributes.Length == 0) return default(TAttribute);
return (TAttribute)attributes[0];
}
public static PropertyInfo GetPropertyWithAttributeValue<TAttribute>(
this IEnumerable<PropertyInfo> properties,
Func<TAttribute, bool> findPredicate)
where TAttribute : Attribute
{
var property = from p in properties
where p.HasAttribute<TAttribute>() &&
findPredicate.Invoke(p.GetAttribute<TAttribute>())
select p;
return property.FirstOrDefault();
}
public static bool HasAttribute<TAttribute>(this PropertyInfo propertyInfo)
{
return propertyInfo.GetCustomAttributes(typeof(TAttribute), true).Any();
}
public static TAttribute GetAttribute<TAttribute>(this PropertyInfo propertyInfo)
{
var attributes = propertyInfo.GetCustomAttributes(typeof(TAttribute), true);
if (attributes.Length == 0) return default(TAttribute);
return (TAttribute)attributes[0];
}
}
I haven't tried running this myself, but hopefully it's enough to get you started.
Related
I am trying to add the ThenById() method which will be launched after a call to OrderBy() on IOrderedQueryable:
public static IOrderedQueryable<TEntity> ThenById<TEntity>(this IQueryable<TEntity> source)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
var command = "ThenBy";
var thenByProperty = "Id";
var type = typeof(TEntity);
if (type.GetProperty(thenByProperty) == null)
{
throw new MissingFieldException(nameof(thenByProperty));
}
var param = Expression.Parameter(type, "p");
var property = type.GetProperty(thenByProperty, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
var propertyAccess = Expression.MakeMemberAccess(param, property);
var orderByExpression = Expression.Lambda(propertyAccess, param);
var resultExpression = Expression.Call(
typeof(IOrderedQueryable),
command,
new Type[] { type, property.PropertyType },
source.Expression,
Expression.Quote(orderByExpression));
return (IOrderedQueryable<TEntity>)source.Provider.CreateQuery<TEntity>(resultExpression);
}
I receive the following error message:
No generic method 'ThenBy' on type 'System.Linq.IOrderedQueryable' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic.
The ThenBy extension method is in the System.Linq.Queryable class, not in IOrderedQueryable. You simply need to replace that in your code:
public static IOrderedQueryable<TEntity> ThenById<TEntity>(
this IOrderedQueryable<TEntity> source)
{
//snip
var resultExpression = Expression.Call(
typeof(System.Linq.Queryable),
command,
new Type[] { type, property.PropertyType },
source.Expression,
Expression.Quote(orderByExpression));
return (IOrderedQueryable<TEntity>)source.Provider.CreateQuery<TEntity>(resultExpression);
}
Note that the method should be extending IOrderedQueryable, not just IQueryable.
However, this will fail at runtime if TEntity doesn't have an Id property. My preference would be to give all entities with the Id property an interface and use a generic constraint here. That way you avoid expressions completely and get compile time safety. For example:
public interface IHasId
{
int Id { get; set; }
}
public class SomeEntity : IHasId
{
public int Id { get; set; }
public string Name { get; set; }
//etc
}
Which simplifies your extension method:
public static IOrderedQueryable<TEntity> ThenById<TEntity>(
this IOrderedQueryable<TEntity> source)
where TEntity : IHasId
{
return source.ThenBy(e => e.Id);
}
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();
This question is related to this other question.
I have the following method:
public static T GetNewData<T>(params Action<dynamic>[] actions) where T : class, new()
{
dynamic dynamicData = new DeepObject();
foreach (var action in actions)
{
action(dynamicData);
}
return Converter.Convert<T>(dynamicData);
}
The users of this method will include less technical people, even non-developers and as such the easier writing calls to this method is the better. My sticking point right now is that by using Action<dynamic> as the parameter type there is no intellisense provided to the user. In the context I know that the intellisense should be acting as if the dynamic was in fact T.
So is their a way I could either: Tell Visual Studio to use type T for the intellisense or change the parameter to be Action<T> and somehow programmatically change it to be Action<dynamic> or Action<DeepObject> so that the call to it will succeed?
EDIT: To clarify, the types that I am using for T are not of type DeepObject and they do not inherit any standard interface, the use of DeepObject is to allow setting up nested types without the user needing to explicitly instantiate at each level. This was the original usage before adding the dynamic and DeepObject code:
ExampleDataFactory.GetNewData<ServicesAndFeaturesInfo>(
x => x.Property1 = ExampleDataFactory.GetNewData<Property1Type>(),
x => x.Property1.Property2 = ExampleDataFactory.GetNewData<Property2Type>(),
x => x.Property1.Property2.Property3 = ExampleDataFactory.GetNewData<Property3Type>(),
x => x.Property1.Property2.Property3.Property4 = true);
Here is what it looks like now:
ExampleDataFactory.GetNewData<ServicesAndFeaturesInfo>(
x => x.Property1.Property2.Property3.Property4 = true);
EDIT: Here is the fully implemented solution based on nmclean's answer
public static DataBuilder<T> GetNewData<T>() where T : class, new()
{
return new DataBuilder<T>();
}
The DataBuilder Class:
public class DataBuilder<T>
{
public readonly T data;
public DataBuilder()
{
data = Activator.CreateInstance<T>();
}
public DataBuilder(T data)
{
this.data = data;
}
public DataBuilder<T> SetValue<T2>(Expression<Func<T, T2>> expression, T2 value)
{
var mExpr = GetMemberExpression(expression);
var obj = Recurse(mExpr);
var p = (PropertyInfo)mExpr.Member;
p.SetValue(obj, value);
return this;
}
public T Build()
{
return data;
}
public object Recurse(MemberExpression expr)
{
if (expr.Expression.Type != typeof(T))
{
var pExpr = GetMemberExpression(expr.Expression);
var parent = Recurse(pExpr);
var pInfo = (PropertyInfo) pExpr.Member;
var obj = pInfo.GetValue(parent);
if (obj == null)
{
obj = Activator.CreateInstance(pInfo.PropertyType);
pInfo.SetValue(parent, obj);
}
return obj;
}
return data;
}
private static MemberExpression GetMemberExpression(Expression expr)
{
var member = expr as MemberExpression;
var unary = expr as UnaryExpression;
return member ?? (unary != null ? unary.Operand as MemberExpression : null);
}
private static MemberExpression GetMemberExpression<T2>(Expression<Func<T, T2>> expr)
{
return GetMemberExpression(expr.Body);
}
}
The Usage:
ExampleDataFactory.GetNewData<ServicesAndFeaturesInfo>()
.SetValue(x=> x.Property1.EnumProperty, EnumType.Own)
.SetValue(x=> x.Property2.Property3.Property4.BoolProperty, true)
.Build();
Do not use Action<dynamic>, use Action<T>with method's constraint where T:DeepObject. Users will get intellisence and ability to use strongly typed objects:
public static DeepObject GetNewData<T>(params Action<T>[] actions)
where T : DeepObject, //restrict user only inheritors of DeepObject
new() //and require constructor
{
var data = new T();
foreach (var action in actions)
{
action(data);
}
return data;
}
Does the user need to access unknown properties or add new ones? If not, using dynamic objects seems like a step backwards. If your desired syntax does compile as an Action<T>, I think you should just declare it that way and then go with your first instinct of using the LINQ Expression API to decide how to interpret the code.
Unfortunately, although statements, such as an assignment, are part of the API, C# doesn't support converting them to expression trees. This is not allowed:
public static T GetNewData<T>(params Expression<Action<T>>[] actions)
where T : class, new() {
...
}
...
ExampleDataFactory.GetNewData<ServicesAndFeaturesInfo>(
x => x.Property1.Property2.Property3.Property4 = true);
Only single-line expressions that would have a return a value are supported. So I think the best you could do is something like this:
public class Assignment<T> {
public readonly Expression Expression;
public readonly object Value;
public Assignment(Expression<Func<T, object>> expression, object value) {
Expression = expression;
Value = value;
}
}
...
public static T GetNewData<T>(params Assignment<T>[] assignments)
where T : class, new() {
var data = Activator.CreateInstance<T>();
foreach (var assignment in assignments) {
// todo:
// - pull property names from assignment.Expression
// - initialize nested properties / assign to assignment.Value
}
return data;
}
...
ExampleDataFactory.GetNewData<ServicesAndFeaturesInfo>(
new Assignment<ServicesAndFeaturesInfo>(
x => x.Property1.Property2.Property3.Property4, true));
Getting the property names from an expression tree of chained property access is not too complicated. Here is one implementation.
Of course, the new Assignment<ServicesAndFeaturesInfo>(...) is ugly and repetitive, so maybe it could be restructured to something like this:
var newData = ExampleDataFactory.NewData<ServicesAndFeaturesInfo>();
newData.Add(x => x.Property1.Property2.Property3.Property4, true);
newData.Add(...);
...
newData.Get();
Instead of using Reflection, how can I set and get object properties using Expression Trees?
I have written the below class which works fine:
public class PropertyAccessor<TEntity>
{
private readonly PropertyInfo _memberInfo;
private readonly TEntity _nom;
public PropertyAccessor(Expression<Func<TEntity, object>> fieldSelector, TEntity nom)
{
if (fieldSelector.Body is MemberExpression)
_memberInfo = (PropertyInfo)((MemberExpression)fieldSelector.Body).Member;
else if (fieldSelector.Body is UnaryExpression)
_memberInfo = (PropertyInfo)((MemberExpression)((UnaryExpression)fieldSelector.Body).Operand).Member;
else
throw new NotImplementedException("Field selector not supported");
_nom = nom;
}
public object Value
{
get { return _memberInfo.GetValue(_nom, null); }
set { _memberInfo.SetValue(_nom, value, null); }
}
}
and I use it like this:
Product product = ProductFactory.Build();
var propertyAccessor = new PropertyAccessor<Product>(p => p.Name, product);
var name = propertyAccessor.Value;
Is there any way to improve its performance further? Is the implementation the best way to do it?
Shouldn't I call the Compile() method on the expression before or after the constructor call?
What happens when a lambda expression is passed to an Expression of that lambda expression?
Is converting the MemberExpression to a PropertyInfo the best option? any performance penalties?
Keep in mind that a property is actually two separate methods, a get and a set. I also did not see any reason to use expressions here so I used delegates instead. If you must use an expression you can just compile it first.
public class PropertyAccessor<TEntity,TProperty>
{
private readonly TEntity _nom;
Func<TEntity, TProperty> _getter;
Action<TEntity, TProperty> _setter;
public PropertyAccessor(Func<TEntity, TProperty> getter, Action<TEntity, TProperty> setter, TEntity nom)
{
_getter = getter;
_setter = setter;
_nom = nom;
}
public object Value // the return type can be changed to TProperty
{
get { return _getter(_nom); }
set { _setter(_nom, (TProperty)value); }
}
}
This can be called like this:
var propertyAccessor = new PropertyAccessor<Product, String>(p => p.Name, (p, v) => p.Name = v, product);
I was pretty sure that this was possible, but for some reason, I can't seem to figure this out... I am trying to make an extension method off of Type, that will take in a Func to a property from that type, and extract a DefaultValue from the DefaultValueAttribute.
I can get it working, but only if I specify the type arguments for the GetDefaultValue function call. Here is my code as I have it currently:
Person entity:
public class Person
{
public string FirstName { get; set; }
[DefaultValue("1234")]
public string DOB { get; set; }
}
Calls to the method:
//Messing around in LinqPad - .Dump() is LinqPad method
//Works
//typeof(Person).GetDefaultValue<Person, string>(x=>x.DOB).Dump();
//Trying to get to work
//Can't figure out how to get it to infer that TIn is of the Type type.....
typeof(Person).GetDefaultValue(x=> x.DOB).Dump();
This is where the method calls are going to... I am just trying to figure out the means right now before I incorporate into my actual program... Error checking will come into play once I've figured out either how to do it, or to give up b/c it can't be done...
public static class Extensions
{
// Works
// public static TProperty GetDefaultValue<TModel, TProperty>(this Type type, Expression<Func<TModel, TProperty>> exp)
// {
// var property = typeof(TModel).GetProperties().ToList().Single(p => p.Name == GetFullPropertyName(exp));
// var defaultValue = (DefaultValueAttribute)property.GetCustomAttributes(typeof(DefaultValueAttribute), false).FirstOrDefault();
// return (TProperty)defaultValue.Value;
// }
//trying to get to work
//I know that I can't do the following, but it is basically what I am trying to do... I think!
public static TProperty GetDefaultValue<TProperty>(this Type type, Expression<Func<typeof(type), TProperty>> exp)
{
var property = type.GetProperties().ToList().Single(p => p.Name == GetFullPropertyName(exp));
var defaultValue = (DefaultValueAttribute)property.GetCustomAttributes(typeof(DefaultValueAttribute), false).FirstOrDefault();
return (TProperty)defaultValue.Value;
}
//GetFullPropertyName c/o: http://stackoverflow.com/users/105570/
//ref: http://stackoverflow.com/questions/2789504/
public static string GetFullPropertyName<TModel, TProperty>(Expression<Func<TModel, TProperty>> exp)
{
MemberExpression memberExp;
if (!TryFindMemberExpression(exp.Body, out memberExp))
return String.Empty;
var memberNames = new Stack<string>();
do
memberNames.Push(memberExp.Member.Name);
while (TryFindMemberExpression(memberExp.Expression, out memberExp));
return String.Join(".", memberNames.ToArray());
}
private static bool TryFindMemberExpression(Expression exp, out MemberExpression memberExp)
{
memberExp = exp as MemberExpression;
if (memberExp != null)
return true;
if (IsConversion(exp) && exp is UnaryExpression)
{
memberExp = ((UnaryExpression)exp).Operand as MemberExpression;
if (memberExp != null)
return true;
}
return false;
}
private static bool IsConversion(Expression exp)
{
return exp.NodeType == ExpressionType.Convert || exp.NodeType == ExpressionType.ConvertChecked;
}
}
Am I crazy, or is this actually possible? Thank you in advance for your help!
That's not how it works- typeof(Person) doesn't have a property called DOB, but a class whose type is Person does. What you want is to make your extension method generic:
public static TValue GetDefaultValue<TClass, TValue>(this TClass val, Expression<Func<TClass, TValue>> getter) {
var type = typeof(TClass);
var property = type.GetProperties().ToList().Single(p => p.Name == GetFullPropertyName(exp));
var defaultValue = (DefaultValueAttribute)property.GetCustomAttributes(typeof(DefaultValueAttribute), false).FirstOrDefault();
return (TProperty)defaultValue.Value;
}
and call it something like:
Person somePerson = GetMeAPerson();
somePerson.GetDefaultValue(p=>p.DOB);
Note that I have not tested the above, but I've seen similar code in the past that worked. That said, I suspect that what you were originally trying to do isn't very appealing this way because you need to make a Person instance first.
Another, possibly more appealing approach is to not make it an extension method at all:
public static TValue GetDefaultValue<TClass, TValue>(Expression<Func<TClass, TValue>> getter) {
var type = typeof(TClass);
var property = type.GetProperties().ToList().Single(p => p.Name == GetFullPropertyName(exp));
var defaultValue = (DefaultValueAttribute)property.GetCustomAttributes(typeof(DefaultValueAttribute), false).FirstOrDefault();
return (TProperty)defaultValue.Value;
}
Then you can call it without an instance, but the inference isn't as nice):
var defaultValue = GetDefaultValue<Person, DateTime>(p => p.DOB);