Some updated download links for the latest .NET version:
Tags
-
Recent Posts
Archives
Friends
Work
Meta
Some updated download links for the latest .NET version:
Ruby on Rails allows you to use ‘dynamic’ finders to query the database. This is actually a feature from ActiveRecord to dynamicly use methods which will represent where clauses on the database.
Some examples:
User.find(:first, :conditions => ["name = ?", name]) User.find_by_name(name) User.find(:all, :conditions => ["city = ?", city]) User.find_all_by_city(city) User.find(:all, :conditions => ["street = ? AND city IN (?)", street, cities]) User.find_all_by_street_and_city(street, cities)
With .NET 4.0 we have dynamics of our own so I thought why not recreate this feature…
By creating an extension method for IEnumerable<T> and IQueryable<T> each enumerable source can now use the dynamic finder feature
public static class DynamicExtensions
{
public static dynamic AsDynamic<T>(this IEnumerable<T> source)
{
return new DynamicEnumerable<T>(source);
}
public static dynamic AsDynamic<T>(this IQueryable<T> source)
{
return new DynamicQueryable<T>(source);
}
}
The Dynamic classes will inherit from DynamicObject to give basic dynamics support.
sealed class DynamicQueryable<T> : DynamicObject
{
private readonly IQueryable<T> source;
public DynamicQueryable(IQueryable<T> source)
{
this.source = source;
}
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
var match = methodMatcher.Match(binder.Name);
if (match.Success)
{
var properties = match.Groups[2].Value.Split(new[] { "And" }, StringSplitOptions.RemoveEmptyEntries);
var predicate = BuildExpression<T>(properties, args);
if (match.Groups[1].Success)
result = source.Where(predicate);
else
result = source.FirstOrDefault(predicate);
return true;
}
return base.TryInvokeMember(binder, args, out result);
}
}
With the only missing part the BuildExpression method which will create the expression tree:
private static Expression<Func<T, bool>> BuildExpression<T>(string[] properties, object[] args)
{
if (properties.Length < 1)
throw new InvalidOperationException("Need to specify at least one property.");
if (args.Length != properties.Length)
throw new InvalidOperationException("Method expects " + properties.Length + " parameters and only got " + args.Length + " values.");
var param = Expression.Parameter(typeof(T), "p");
var body = Expression.Equal(Expression.Property(param, properties[0]), Expression.Constant(args[0]));
for (var i = 1; i < properties.Length; i++)
body = Expression.AndAlso(body, Expression.Equal(Expression.Property(param, properties[i]), Expression.Constant(args[i])));
return Expression.Lambda<Func<T, bool>>(body, param);
}
And the only difference with the DynamicEnumerable class is that the expression will be compiled to use for the Where/FirstOrDefault.
Full source at pastebin.
UPDATE: Vidyano 2.0 released
The Vidyano Release Candidate is now available
Together with a new website we offer complete support now for any application build with Vidyano, think of it as a Go Live license.
More information (including documentation, tutorials and downloads) at:
We released the latest version of Vidyano last night.
A lot of new features have been added to this release, here are just a couple:
We greatly encourage you to play around with this new build and are looking forward to your feedback!
More information and download on our team blog.
UPDATE: .NET 4.0 Links
You can download it here:
Didn’t even know it was possible but it seems like Expression Blend supports AddIns. And Stefan implemented to feature I was missing the most in Blend: IntelliSense!
You can get the Add-In at his blog.
David has made a complete step-by-step getting started for Vidyano.
You can get more information at our blog.
The download contains the document as a .doc file or a .xps file, and it also contains the complete result so you can quickly check it out.
This is just the beginning, I’m currently working on a sample browser that should show more advanced scenarios in Vidyano (with full source code so you can check how it’s done) that should be available in a few days