Blog: 2012 March

Create Task-based method from “legacy” callback method

March 20, 2012, (updated on November 5, 2015), 4 comments, Software Development

In my projects I have a lot of asynchronous methods with a callback as parameter which is called when the operation has completed. I asked myself how to port these methods to support the new async / await keywords and if possible allow me to use the class with older frameworks which do not support this new functionality. The samples in this article use the Http classes from my library project MyToolkit. We start with a simple method called LoadHtml which will call the callback action completed after completion: public void LoadHtml(String url, Action<HttpResponse> completed) { Http.Get(url, completed); } To […] Read more...

Tags: , , , ,

Retrieve Entity Framework DbContext from a proxy object

March 7, 2012, (updated on September 8, 2014), Software Development

There are some situations where you need to get the DbContext from a proxy object generated by the Entity Framework (current version: 4.5). Every generated proxy object holds a reference to the ObjectContext it belongs to. The problem is, that the ObjectContext does not have any information or reference to the corresponding DbContext. My solution is to maintain a static list with a ObjectContext-DbContext-Mapping: using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Data.Objects; namespace Sample { public class MyDbContext : DbContext { private static readonly Dictionary<ObjectContext, MyDbContext> contexts = new Dictionary<ObjectContext, MyDbContext>(); public static MyDbContext FromObjectContext(ObjectContext context) { lock (contexts) { if (contexts.ContainsKey(context)) […] Read more...

Tags: , ,

  Page 1 of 1