Friday, 12 October 2012

Entity Framework 6 Asyc- Is this the best feature yet?





Entity Framework 5 was long awaited by the developer community and not without its reasons.

EF (as the nick name goes), version 5 which is available on NuGet, introduced a host of features which immediately made it a hot cake with the Enterprise Developers looking to shed some weight off their shoulders in terms of Rapid ORM Prototyping and Application Development:

Some of the nicer ones being-

·         Enum support allows you to have enum properties in your entity classes. This new feature is available for Model, Database and Code First.

·         Table-valued functions in your database can now be used with Database First.

·         Spatial data types can now be exposed to Model using the DbGeography and DbGeometry types. Spatial data is supported in Model, Database and Code First.

What's more, EF5 automatically detects the database engine depending upon the development environment for the creation of new databases including the ability to make use of enum properties in entity classes. The framework also adds tables to the existing database if the target database doesn't contain any tables from the model [Cool!]

For Application Architects and Designers, the designer included with Visual Studio 2012 consist of new features such as DbContext code generation, multiple-diagrams per model, table-valued functions in addition to batch import of stored procedures which allows multiple stored procedures to be added during model creation. The new models created using the designer will generate a derived DbContext and POCO classes by default.

However, For enterprise developers, particularly working with performance and database intensive applications and NOSQL Databases all the time, a feature which has been long sought is the Task-based async - Allowing EF to take advantage of .NET 4.5. async support with async queries, updates, etc.

According to Microsoft, EF6 will provide support for async and can be implemented either using SaveChangesAsync() or ExecuteSqlCommandAsync() methods.


This will also enable developers to perform async operations on IQueryable (IQueryable<T>) operator and can be implemented using FindAsync() and SingleAsync() extension methods. 

Moreover, developers will be able to make use of ToListAsync() method to execute the code.


So What does this mean?

Distributed and Asynchronous programming has long been the holy grail for coders working on distributed, fault-tolerant applications that respond in real-time and never stop or halt.

With task based async now a reality with Entity Framework Users, it simply means more power to create better fault-tolerant applications, rapidly without worrying about the underlying Database schema changes.

Here is how the async version of SaveChanges looks in an ASP.NET MVC controller action:

[HttpPost]
public async Task<ActionResult> Edit(Thing thing)
{
    if(ModelState.IsValid)
    {
        _db.Entry(thing).State = EntityState.Modified;
        await _db.SaveChangesAsync();               
        return RedirectToAction("Index");
    }
    
    return View(thing);
}

The new IQueryable operators are async versions of operators that materialize a concrete result. For example, finding a single entity:

[HttpGet]
public async Task<ActionResult> Edit(int id)
{
    var model = await _db.Things.FindAsync(id);
    // or
    var model = await _db.Things.SingleAsync(t => t.Id == id);

    return View(model);
}

Also forcing execution with ToList:

public async Task<ActionResult> Index()
{
    var model = new HomeIndexViewModel();         
    model.Things = await _db.Things.ToListAsync();

    return View(model);
}

Getting an application up and running with EF 6 (including migrations) is slightly challenging, but I've found the easiest approach is to msbuild the Nuget.proj file in the NuGet directory, then adding the NuGet\bin\debug directory as a custom local NuGet repository and adding references to EF 6 via NuGet. I also had to register all the EF assemblies for verification skipping with sn -Vr.


EF 6 will be the next release after the completion of EF5 and Visual Studio 2012 release. 
Here is the download link: http://entityframework.codeplex.com/wikipage?title=Roadmap

No comments:

Post a Comment