Archive

Archive for the ‘Entity Framework’ Category

Pluralization Services

 

Sometimes it is useful to allow a user to define their own entities or rename existing entities within an application. For example, in a contact management system, one could envisage the user wanting to tailor the software to change, say, ‘customer’ to ‘client’, or ‘lead’ to ‘prospect’, as well as defining new entities, such as contact methods, e.g. ‘network meeting’, ‘mailshot’, etc.

One issue when giving end users freedom like this is how to pluralise the words, e.g. “You have 3 new lead”. One way is to just add an ‘s’ in brackets, such as “you have 3 new lead(s)”, but this falls down with some words, such as ‘entity’, ‘person’, ‘child’, etc. where the plurals don’t follow the simple “add an ‘s'” rule.

Luckily, but for English only, .Net includes a pluralisation service which was required for the development of Entity Framework, and we can access it.

The screenshot below shows a very simple demonstration application (which can be downloaded here).

 

 

The code extract below is the key to using the service, and it is very simple.

private void buttonPluralize_Click(object sender, EventArgs e)
{
  if (!String.IsNullOrWhiteSpace(textBoxSingular.Text)) {
    textBoxPlural.Text = PluralizationService.CreateService(CultureInfo.GetCultureInfo("en-us")).Pluralize(textBoxSingular.Text);
  } else {
    textBoxPlural.Text = String.Empty;
  }
}

 

Unfortunately, the only cultures supported at the minute are English based. If you attempt to use anything else you will be met with the lovely message “We don’t support locales other than english yet”.

To utilise the PluralizationService you need to reference the following two DLLs.

System.Data.Entity.dll
System.Data.Entity.Design.dll

On my machine, these can both be found in the following folder:
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\

The second DLL, ‘System.Data.Entity.Design.dll’, is not in the .Net 4 Client framework, but rather the full framework.

You will also require the following ‘using’ statements.

using System.Data.Entity.Design.PluralizationServices;
using System.Globalization;

 

So, play around and have fun. It would be great to hear what people are doing with the service, so please let me know if you have time.

Downloads

Pluralization.zip

Advertisements

Entity Framework, LazyLoading & WCF

March 29, 2011 1 comment

Entity Framework and WCF work well together in providing a good back end service layer for a range of application types. One gotcha however that can easily catch you out without being very visible is LazyLoading.

LazyLoading defers the loading of related entities, e.g. Order for Customers, until they are accessed via a navigation property.

In the Entity Framework runtime, the default value of the LazyLoadingEnabled property in an instance of an ObjectContext is false. However, if you use the Entity Framework tools to create a new model and the corresponding generated classes, LazyLoadingEnabled is set to true in the object context’s constructor.

Thus it is possible for LazyLoading to be enabled without really explicitly requesting the feature.

There is no problem with this and LazyLoading is a great feature if you are not using WCF, however the serialisation of objects to XML before being sent over the wire causes all the navigation properties to be accessed throughout the model and thus it is very easy to accidentally serialise the whole object graph which consequently has the effect of slowing down the data access via WCF.

LazyLoading can be switched of explicitly in the ObjectContext using the code below or for Entity Framework as a whole by changing the properties of the EDMX.

public void QueryWithoutLazyLoading()
{
    using (AdventureWorksEntities context = new AdventureWorksEntities())
    {
        context.ContextOptions.LazyLoadingEnabled = false;
        ...
    }
}

Listing 1 – Disabling LazyLoading on the ObjectContext
In the next post I’ll look at how to work around not using LazyLoading with WCF as well as how to build up queries in manageable chunks that can be specialised based on logic.