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