Recently I've started working more and more with the ADO.NET Entity framework and I must say that I like it a lot. There are folks who absolutely love EDM and folks that think its really evil.
It's still a v1.0 product and I treat it as such. The designer does leave a lot to be desired.A lot of bells and whistles will be added to the VS 2010 release to address a lot of the designer "desirables".
It's not been an easy buy in at work for adopting EDM, but my team has been among the first to have adopted it (against some odds of course - <nudge >DBAs).
I've found myself doing the same repetitve tasks working with EDM such as adding a collection of items to an EntityCollection, lazily loading entities, converting enumerables to EntityCollections etc.
So, I decided to whip up some extension methods to address the most common tasks that I do from day to day working in EDM. I'll be updating this list as I come up with more. So here goes....
- public static class EDMExtensions
- {
-
-
-
-
-
-
-
- public static void ForEach<T>(this IEnumerable<T> collection, Action<T> action) where T : class, IEntityWithRelationships
- {
- foreach (var item in collection)
- {
- action(item);
- }
- }
-
-
-
-
-
-
-
- public static EntityCollection<T> LazyLoad<T>(this EntityCollection<T> collection) where T : class, IEntityWithRelationships
- {
- if (!collection.IsLoaded)
- {
- collection.Load();
- }
- return collection;
- }
-
-
-
-
-
-
-
- public static void LazyLoad<T>(this EntityReference<T> reference) where T : class, IEntityWithRelationships
- {
- if (!reference.IsLoaded)
- {
- reference.Load();
- }
- }
-
-
-
-
-
-
-
-
- public static void Add<T>(this EntityCollection<T> collection, IEnumerable<T> items) where T : class, IEntityWithRelationships
- {
- foreach (var item in items)
- {
- collection.Add(item);
- }
- }
-
-
-
-
-
-
-
-
- public static EntityCollection<T> ToEntityCollection<T>(this IEnumerable<T> items) where T : class, IEntityWithRelationships
- {
- EntityCollection<T> coll = new EntityCollection<T>();
- foreach (var item in items)
- {
- coll.Add(item);
- }
- return coll;
- }
-
- }