views:

171

answers:

1

Hi,

While reading an article on EF 4, I read that EF pluralizes certain objects (EntitySet's, Navigation's pointing to EntityCollection's, etc.), while using the singular form for other objects. Out of curiosity - how does it do this ? Is it using a built-in dictionary ?

Thanks,

Scott

+2  A: 

Microsoft added a PluralizationService base abstract class to the .NET framework - which can be used for other purposes as well!

public abstract  class PluralizationService
{ 
        public static PluralizationService CreateService(CultureInfo culture);

        public abstract string Pluralize(string word);
        public abstract string Singularize(string word);
} 

See a great blog post on EF Pluralization which explains it in great detail. Microsoft provides a few concrete implementations of that service in various languages / cultures, but you're totally free to roll your own.

I don't know for a fact how the EF4 provided pluralization services work - but most likely it's a combination of certain linguistic rules, and a plethora of exceptions to handle differently. Those are most likely stored as resources or in some other way inside the relevant assemblies.

marc_s
Wow, that's cool! One more question - why'd they pick an abstract base class ? I thought programming to an interface was preferable to building inheritance hierarchies ?
Scott Davies
I wasn't there for the design meetings :-) but an abstract base class can implement some basic behavior that all descendants will inherit - you can't do that with an interface. Maybe they had their reasons to do something like that - not sure.
marc_s
Hmm. According to "C# 4.0 in a Nutshell" - "Abstract members are like virtual members, except they don't provide a default implementation. That implementation must be provided by the subclass, unless that subclass is also declared abstract." Would the difference be wanting to specify static members (like they did above) ?
Scott Davies