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:  [...]