Saturday, November 9, 2013

Entity Framework 6, CountAsync and PredicateBuilder

My previous subject was about the ToListAsync() method used with the PredicateBuilder from LinqKit which was not working due to the AsExpandable() call and the not implemented IDbAsyncEnumerable interface...

After the modification that you can find in the previous post, I have tried to call the CountAsync() method and a similar exception occured:
"System.InvalidOperationException : The provider for the source IQueryable doesn't implement IDbAsyncQueryProvider. Only providers that implement IDbAsyncQueryProvider can be used for Entity Framework asynchronous operations." 
Another modification on LinqKit is needed for this new case: we just need to take my previous modification and use this implementation of ExpandableQueryProvider class with the following code:


   1:  public class ExpandableQueryProvider<T> : IDbAsyncQueryProvider
   2:  {
   3:      readonly ExpandableQuery<T> _query;
   4:      
   5:      internal ExpandableQueryProvider(ExpandableQuery<T> query)
   6:      {
   7:          _query = query;
   8:      }
   9:   
  10:      IQueryable<TElement> IQueryProvider.CreateQuery<TElement>(Expression expression)
  11:      {
  12:          return new ExpandableQuery<TElement>(_query.InnerQuery.Provider.CreateQuery<TElement>(expression.Expand()));
  13:      }
  14:   
  15:      IQueryable IQueryProvider.CreateQuery(Expression expression)
  16:      {
  17:          return _query.InnerQuery.Provider.CreateQuery(expression.Expand());
  18:      }
  19:      
  20:      TResult IQueryProvider.Execute<TResult>(Expression expression)
  21:      {
  22:          return _query.InnerQuery.Provider.Execute<TResult>(expression.Expand());
  23:      }
  24:      
  25:      object IQueryProvider.Execute(Expression expression)
  26:      {
  27:          return _query.InnerQuery.Provider.Execute(expression.Expand());
  28:      }
  29:      
  30:      public Task<object> ExecuteAsync(Expression expression, CancellationToken cancellationToken)
  31:      {
  32:          return Task.FromResult(_query.InnerQuery.Provider.Execute(expression.Expand()));
  33:      }
  34:   
  35:      public Task<TResult> ExecuteAsync<TResult>(Expression expression, CancellationToken cancellationToken)
  36:      {
  37:          return Task.FromResult(_query.InnerQuery.Provider.Execute<TResult>(expression.Expand()));
  38:      }
  39:  }

With that new stuff, CountAsync() will work as usual even after a call to AsExpandable()!

Wednesday, November 6, 2013

Entity Framework 6, ToListAsync and PredicateBuilder

For a new personal project which uses Entity Framework 6, I needed a powerful way to play with a lot of criteria in a search request.

The query has to been done on a TPC Code First model: all the classes inherit from a common base class with other abstract classes and interfaces for some of them, etc. In conclusion, the model is pretty complex and the query possibilities are wide!

Thanks to AutoMapper, the usage of this model from the front-end to the back-end is really easy but I encounter a problem with the way to manage my criteria logic: how to correctly create some Expression<Func<TEntity, bool>> for the .Where(...) method?

For that kind of needs, PredicateBuilder is a very good helper! You just need to create a basic "true" predicate like that:

   1:  var where = PredicateBuilder.True<TEntity>();

And you add your conditions according to your logic with that kind of code:

   1:  if (condition)
   2:  {
   3:      where = where.Add(t => t.Something > 0);
   4:  }

When all your criteria have been added (OfType is used because it's a TPC model):

   1:  context.Entities.OfType<TEntity>().AsExpandable().Where(where).ToList();

AsExpandable() is a method from LinqKit that you have with the PredicateBuilder and which is necessary.

The problem that I have encountered  I am using Entity Framework 6 and I want to use the async/await capacities of this new version. So all my code in my repositories are using the new xxxAsync methods of Entity Framework. But if you use the actual version of LinqKit, and you call the AsExpandable method before the call to ToListAsync(), you will have this exception:
"System.InvalidOperationException : The source IQueryable doesn't implement IDbAsyncEnumerable..."
The source code of LinqKit is open an available here. For my project, I downloaded the source code, included the project in my solution and I have done the following modifications:
   1:  public class ExpandableQuery<T> : IQueryable<T>, IOrderedQueryable<T>, IOrderedQueryable, IDbAsyncEnumerable<T>
   2:  {
   3:      ExpandableQueryProvider<T> _provider;
   4:      IQueryable<T> _inner;
   5:   
   6:      internal IQueryable<T> InnerQuery { get { return _inner; } }
   7:   
   8:      internal ExpandableQuery (IQueryable<T> inner)
   9:      {
  10:          _inner = inner;
  11:          _provider = new ExpandableQueryProvider<T> (this);
  12:      }
  13:   
  14:      Expression IQueryable.Expression { get { return _inner.Expression; } }
  15:      Type IQueryable.ElementType { get { return typeof (T); } }
  16:      IQueryProvider IQueryable.Provider { get { return _provider; } }
  17:      public IEnumerator<T> GetEnumerator () { return _inner.GetEnumerator (); }
  18:      IEnumerator IEnumerable.GetEnumerator () { return _inner.GetEnumerator (); }
  19:      public override string ToString() { return _inner.ToString(); }
  20:      
  21:      public IDbAsyncEnumerator<T> GetAsyncEnumerator()
  22:      {
  23:          return new ExpandableDbAsyncEnumerator<T>(this.AsEnumerable().GetEnumerator());
  24:      }
  25:      
  26:      IDbAsyncEnumerator IDbAsyncEnumerable.GetAsyncEnumerator()
  27:      {
  28:          return GetAsyncEnumerator();
  29:      }
  30:  }

And I have added this new class:

   1:  public class ExpandableDbAsyncEnumerator<T> : IDbAsyncEnumerator<T>
   2:  {
   3:      private readonly IEnumerator<T> _inner;
   4:      public ExpandableDbAsyncEnumerator(IEnumerator<T> inner)
   5:      {
   6:          _inner = inner;
   7:      }
   8:      public void Dispose()
   9:      {
  10:          _inner.Dispose();
  11:      }
  12:      public Task<bool> MoveNextAsync(CancellationToken cancellationToken)
  13:      {
  14:          return Task.FromResult(_inner.MoveNext());
  15:      }
  16:      public T Current
  17:      {
  18:          get { return _inner.Current; }
  19:      }
  20:      object IDbAsyncEnumerator.Current
  21:      {
  22:          get { return Current; }
  23:      }
  24:  }

Thanks to this change, you can use the excellent PredicateBuilder and you can still use the ToListAsync() methods of the new version of Entity Framework!

I hope it will help someone!

News

That's a long time since my latest article on this blog!

Since the previous article, many things have changed for me: I quit my job at Emakina Belgium, I am now freelancer and I moved from Brussels to London for a contract of 6 months for my current client: Ink.

Next steps: creating my own company in Belgium at my return in Belgium and continuing to work on my next personal project!

Wednesday, May 1, 2013

New project for the belgian market

Edit: To comply with the laws of Belgium, the site has been put offline (some exchanges were not legal and I do not want to be responsible of that).

Last week, I launched a new site dedicated to the Belgian market (and maybe I will be targeting other countries after...): ChequeChange


The site is prety simple and is dedicated to the exchange of meal/eco vouchers in Belgium: this is not a business that fascinates me but making this project will be useful for some people and has help me to improve my skills! (the code and the databases of my next projects will be better!)

The website has been realised with the following technologies:

- ASP.NET MVC 4
- Entity Framework 5.x (but I still prefer using stored procedures that EF can map...)
- Windows Azure (2 small instances for the moment)
- MetricsHub (for monitoring and in case of automatic scale out needed... very good product!)
- SQL Azure
- Azure Storage
- Azure CDN
- Azure AppFabric Caching (thanks to MVC Donut Caching...)

The design is -almost- responsive and the site can be used on all the devices ;-)

This is my second experience with Windows Azure... I like this product! The SDK is excellent and up to date, the portal is well done and easy to use, the analytics are useful, performance are good, etc. A great product!

Monday, April 22, 2013

My team won the first place in Belgium!

The second step of the "Super Hackathon" in Belgium ended on April 19... And my team won the competition! Congrats to my teammates: Benoit Jadinon, Giuliano Dore and Esteban Goffaux.

You can see here three screenshots of "Fit Beast":




There are still some missing functionalities but the app is now available on the belgian market (not other countries for the moment, more to come...)!

You can find our app here but we plan to remove it from my Microsoft account and put it on the Emakina account: the link will become unavailable.

Thanks to this first place, I will attend to TechEd Europe 2013 at madrid! I never attended to a so big Microsoft event except for the TechDays in Belgium and France and I am delighted to go there!

Thanks Microsoft for the free ticket and thanks to Emakina for the journey!

Thursday, March 28, 2013

Super Hackathon at the Microsoft Innovation Center


Yesterday, I participated to the "Super Hackathon" at Microsoft Innovation Center in Mons... It was a great experience for me and my team! My team (for Emakina Belgium): Denis Huvelle, Benoit Jadinon, Giuliano Dore and Esteban Goffaux.

We have created a Windows 8 application that uses the "fitbit" web services: you can discover more on their services here.

Our team has finished at the first place at Mons and we will participate to the next part of the competition against the other Microsoft Innovation Centers (Brussels, Genk, Kortrijk).

Here are some photos of the event:










Tuesday, March 26, 2013

Entity Framework with SQL Azure Federation

If you want to use Entity Framework with SQL Azure Federation, you should know some details... According to this article, you have to ensure that the USE FEDERATION command has rerouted the connection to the appropriate federated member before an active transaction is established on the connection.

The way to do this:


   1:  using (var dc = new xxxxEntities())
   2:  {
   3:      ((IObjectContextAdapter)dc).ObjectContext.Connection.Open();
   4:      dc.Database.ExecuteSqlCommand(@"USE FEDERATION FederationName (FederationKeyName = FederationKey) WITH FILTERING = ON, RESET");
   5:      using (var scope = new TransactionScope (TransactionScopeOption.RequiresNew))
   6:      {
   7:          [...]
   8:      }
   9:  }

I have implemented it in a project and it works well with Entity Framewok 5.0.0 but an exception is thrown with Entity Framework 6.0.0-alpha3: "USE FEDERATION statement not allowed within multi-statement transaction."

I have created an issue on the Entity Framework Codeplex: "EF 6 and SQL Azure Federations".

Entity Framework 6 is very important for SQL Azure because it will contain a system of connection resiliency like the Transient Fault Handling Application Block can do. I hope that a solution will be found soon!

Moreover, like in my previous articleDon't Repeat Yourself!

If you want to use Entity Framework with SQL Azure Federation, take a look at this base repository class:


   1:  using System;
   2:  using System.Data.Entity;
   3:  using System.Data.Entity.Infrastructure;
   4:  using System.Transactions;
   5:   
   6:  namespace Admin.Persistence.Repositories.Impl
   7:  {
   8:      public class FederatedRepository
   9:      {
  10:          protected string FederationName { get; set; }
  11:          protected string FederationKeyName { get; set; }
  12:   
  13:          public FederatedRepository(string federationName, string federationKeyName)
  14:          {
  15:              FederationName = federationName;
  16:              FederationKeyName = federationKeyName;
  17:          }
  18:   
  19:          protected TK Execute<T, TK>(Func<T, TK> f, object federationKey) where T : DbContext, new()
  20:          {
  21:              TK result;
  22:   
  23:              using (var db = new T())
  24:              {
  25:                  SetFederationScope(db, federationKey);
  26:   
  27:                  using (var transactionScope = new TransactionScope(TransactionScopeOption.RequiresNew))
  28:                  {
  29:                      result = f(db);
  30:                      transactionScope.Complete();
  31:                  }
  32:              }
  33:   
  34:              return result;
  35:          }
  36:   
  37:          private void SetFederationScope(DbContext dbContext, object federationKey)
  38:          {
  39:              var federationCmdText = string.Format(@"USE FEDERATION {0} ({1}={2}) WITH FILTERING=ON, RESET", FederationName, FederationKeyName, federationKey);
  40:              ((IObjectContextAdapter)dbContext).ObjectContext.Connection.Open();
  41:              dbContext.Database.ExecuteSqlCommand(federationCmdText);
  42:          }
  43:      }
  44:  }

The utilisation is the same than in my previous article ;-)

Are you using Entity Framework? DRY!

I'm not a big fan of Entity Framework... I don't use Model First with some tables mapped into an .edmx or Code First... My usage of EF is prety simple: I work with stored procedures and EF saves me time (a lot...) on the code which uses my stored procedures.

Going back to my title: "Are you using Entity Framework? DRY!" Why "Don't Repeat Yourself"? Because a lot of people repeat the same lines of code:


   1:  [...]
   2:   
   3:  int count;
   4:   
   5:  using (var db = new xxxEntities())
   6:  {
   7:       count = db.OneTable.Count();
   8:  }
   9:   
  10:  [...]

As you know, the using statement is very important (if you don't know why, don't hesitate to do some research) but it is not a reason to repeat yourself!

The line using (var db = new xxxEntities()) will be in each of your calls to the database (and maybe more redundant code like a call to a retry policy...)!

A good solution is to use an unique place where you put your using statement (and other redundant stuff):


   1:  using System;
   2:  using System.Data.Entity;
   3:  using System.Transactions;
   4:   
   5:  namespace Admin.Persistence.Repositories.Impl
   6:  {
   7:      public class Repository
   8:      {
   9:          protected TK Execute<T, TK>(Func<T, TK> f) where T : DbContext, new()
  10:          {
  11:              TK result;
  12:   
  13:              using (var db = new T())
  14:              {
  15:                  result = f(db);
  16:              }
  17:   
  18:              return result;
  19:          }  
  20:      }
  21:  }

Here is an exemple of an utilisation of the previous class:


   1:  using System.Data.Objects;
   2:  using Admin.Persistence.Data;
   3:   
   4:  namespace Admin.Persistence.Repositories.Impl
   5:  {
   6:      public sealed class TenantFurnisherRepository : Repository, ITenantFurnisherRepository
   7:      {
   8:          public long GetTenantId(string name)
   9:          {
  10:              var id = new ObjectParameter("id", typeof (long));
  11:   
  12:              Execute<TenantIdsFurnisherEntities, int>(ttif => ttif.GetTenantIdByName(name, id));
  13:   
  14:              return long.Parse(id.Value.ToString());
  15:          }
  16:   
  17:          public long RegisterTenant(string name)
  18:          {
  19:              var id = new ObjectParameter("id", typeof(long));
  20:   
  21:              Execute<TenantIdsFurnisherEntities, int>(ttif => ttif.RegisterTenant(name, id));
  22:   
  23:              return long.Parse(id.Value.ToString());
  24:          }
  25:      }
  26:  }

(note: the return value is not used in this case)

With that kind of code you can be more confortable in the maintenance of your code, etc. You can imagine to wrap your using statement with a retry policy for SQL Azure:


   1:  [...]
   2:   
   3:  SqlAzureRetryPolicy.ExecuteAction(() =>
   4:  {
   5:      using (var db = new T())
   6:      {
   7:          result = f(db);
   8:      }
   9:  });
  10:   
  11:  [...]

Thursday, February 28, 2013

Chronic: "Programming ASP.NET MVC 4"

I recently read a new book: Programming ASP.NET MVC 4. This interesting book has been written by Jess Chadwick, Todd Snyder and Hrusikesh Panda and is about the latest ASP.NET MVC version (4).


Here is the list of the topics (by chapter) covered in this book:
  1. Fundamentals of ASP.NET MVC: The MVC Architecture, New in ASP.NET MVC 4, ...
  2. ASP.NET MVC for Web Forms Developers: More Differences than Similarities, ...
  3. Working with Data: Building a Form, Validating Data, ...
  4. Client-Side Development: Working with JavaScript, AJAX, Client-Side Validation, ...
  5. Web Application Architecture: Architecting a Web Application, Design Principles, ...
  6. Enhancing Your Site with AJAX: Partial Rendering, Sending Data to the Server, ...
  7. The ASP.NET Web API: Building a Data Service, Exception Handling, ...
  8. Advanced Data: Data Access Patterns, Building a Data Access Layer, ...
  9. Security: Building Secure Web Applications, Guarding Against Attacks, ...
  10. Mobile Web Development: Application Mobile Friendly, Adaptive Rendering, ...
  11. Parallel, Asynchronous, and Real-Time Data Operations: Asynchronous Controllers, ...
  12. Caching: Types of Caching, Server-Side Caching Techniques, Client-Side Caching Techniques, ...
  13. Client-Side Optimization Techniques: Anatomy of a Page, Best Practices, Measuring Client-Side Performance, ...
  14. Advanced Routing: URLs and SEO, Building Routes, Route Constraints, ...
  15. Reusable UI Components: What ASP.NET MVC Offers out of the Box, Unit Testing Razor Views, ...
  16. Logging: Error Handling in ASP.NET MVC, Logging and Tracing, ...
  17. Automated Testing: Levels of Automated Testing, What Is an Automated Test Project, Testing an ASP.NET MVC Application, ...
  18. Build Automation: Creating Build Scripts, Automating the Build, Continuous Integration, ...
  19. Deployment: What Needs to Be Deployed, Deploying to Internet Information Server, Deploying to Windows Azure, ...
And some appendixes: ASP.NET MVC and Web Forms Integration, Leveraging NuGet as a Platform, Appendix Best Practices, etc.

I enjoyed reading this book: as the title says, it's really about the Real World Web Applications. My knowledge of ASP.NET MVC was already advanced, but I had to rethink my expertise in architecture development, consider tight coupling of classes, etc.
I recommend this big book (500 pages...) to people who already know the basics of  ASP.NET MVC or to the ASP.NET Webform developers.

I will soon receive another book: Exploring CQRS and Event Sourcing: A journey into high scalability, availability, and maintainability with Windows Azure. Perhaps another small chronic to come ;-)

Wednesday, February 13, 2013

Simple Chrome extension: show a counter from API

I already talked about my game "Blind Friends" (which also exists on Windows Phone)...

The game was not a great success but the number of registered players in my database increases progressively.
I had already created a simple action that sends the number of registered players (in JSON: so I can add more information if I need it):


   1:  public JsonResult playercount()
   2:  {
   3:      return Json(new { Count = Db.GetCountPlayers() }, JsonRequestBehavior.AllowGet);
   4:  }

At the beginning, I was checking that number at the URL corresponding to the action in my web app... But it was not very convenient... So I decided to create a small extension for Chrome:


The code is very simple: I just need to check an URL every two minutes.
The extension contains 4 files:

  • background.js
  • icon.png (I took the favicon of my web app)
  • jquery.js (a local copy of a recent version of Jquery)
  • manifest.json

Code of background.js:


   1:  var pollInterval = 2;  // 2 minutes
   2:   
   3:  function updateCounter() {
   4:      $.getJSON('{the url which returns a json with a "Count" property...}', function(json) {
   5:          chrome.browserAction.setBadgeText({
   6:              text: json.Count.toString()
   7:          });
   8:      });
   9:  }
  10:   
  11:  $(document).ready(function () {
  12:      updateCounter(); // we call the function one time here because
  13:                       // the alarm will be triggered one minute after
  14:   
  15:      chrome.browserAction.setBadgeBackgroundColor({
  16:          color: "#000"
  17:      })
  18:          
  19:      chrome.alarms.onAlarm.addListener(function (alarm) {
  20:          updateCounter();
  21:      });
  22:          
  23:      chrome.alarms.create('blindFriendsAlarm', {periodInMinutes: pollInterval});
  24:  })

Code of manifest.json:


   1:  {
   2:    "manifest_version": 2,
   3:   
   4:    "name": "Blind Friends Counter",
   5:    "description": "Count the number of Blind Friends users.",
   6:    "version": "1.0",
   7:   
   8:    "permissions": [
   9:      "https://www.blindfriendsthegame.com/",
  10:      "alarms"
  11:    ],
  12:    "browser_action": {
  13:      "default_icon": "icon.png"
  14:    },
  15:    "background": {
  16:      "scripts": ["jquery.js", "background.js"],
  17:      "persistent": false
  18:    }
  19:  }

The installation is simple: you have to click on "Load unpacked extension" in the extensions page of Chrome


And you just have to select the folder which contains the previous files...