DCSIMG
June 2010 - Posts - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2012 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

June 2010 - Posts

SQL For .Net Developers – Entity Framework 4 Second Cycle Slide Deck

SQL For .Net Developers – Entity Framework 4 Second Cycle Slide Deck

SQL For .Net Developers – Entity Framework 4 Second Cycle Slide Deck

Yesterday’s evening I delivered a session at Microsoft Raanana about
Entity Framework 4 as a part of Microsoft’s
SQL For .Net Developers series of sessions. I want to thank all the
attendees who came to the session. I really enjoyed to talk about
Entity Framework and the audience was very cooperative with a lot
of really good questions.

As I promised, I uploaded the session slide deck and demos to my
Skydrive and you can download it from here. Pay attention that in the
demos main directory there is a backup file for the demo database.

Enjoy!

Eager Loading with Repository Pattern and Entity Framework

Eager Loading with Repository Pattern and Entity Framework

One question that I Eager Loading with Repository Pattern and Entity Framework
received yesterday
after I published the
Revisiting the Repository 
and Unit of Work Patterns

with Entity Framework
post was
how to include the eager loading
ability of Entity Framework.
This post is offering a solution.

Revisiting Eager Loading and Lazy Loading

Lazy loading is a design pattern that is commonly used to defer
initialization of an object up until it is needed by the program.
The gains of using the pattern include efficiency (if it’s used right)
and sometime performance. Eager loading is the opposite pattern of
lazy loading
. In this pattern we initialize the object before hand and
don’t wait to the second we really need it.
In Entity Framework we can use the Include method in order to
eager load an entity graph.

Eager Loading with Repository Pattern

In the interface of the Repository I will add a new method which
will be called QueryObjectGraph. That method will receive a string
which indicate the children to load. Now the interface will look like:

public interface IRepository<T> where T : class
{        
    T GetById(int id);
    IEnumerable<T> GetAll();
    IEnumerable<T> Query(Expression<Func<T, bool>> filter);
    IEnumerable<T> QueryObjectGraph(Expression<Func<T, bool>> filter, string children);
    void Add(T entity);
    void Remove(T entity);   
}

and the implementation of the abstract Repository will change to

public abstract class Repository<T> : IRepository<T>
                                  where T : class
{
  #region Members
 
  protected ObjectSet<T> _objectSet;
 
  #endregion
 
  #region Ctor
 
  public Repository(ObjectContext context)
  {
    _objectSet = context.CreateObjectSet<T>();
  }
 
  #endregion
 
  #region IRepository<T> Members
 
  public IEnumerable<T> GetAll()
  {
    return _objectSet;
  }
 
  public abstract T GetById(int id);
 
  public IEnumerable<T> Query(Expression<Func<T, bool>> filter)
  {
    return _objectSet.Where(filter);
  }
 
  public IEnumerable<T> QueryObjectGraph(Expression<Func<T, bool>> filter, string children)
  {
    return _objectSet.Include(children).Where(filter);
  }
 
  public void Add(T entity)
  {
    _objectSet.AddObject(entity);
  }
 
  public void Remove(T entity)
  {
    _objectSet.DeleteObject(entity);
  }
 
  #endregion
}

Pay attention that I replaced the IObjectSet<T> into it’s
Entity Framework implementation of ObjectSet<T>. The reason
is that the IObjectSet<T> interface doesn’t include the Include method.
Now I can continue using the same Unit of Work implementation and
also use the eager loading ability like in the following example:

using (SchoolEntities context = new SchoolEntities())
{
  UnitOfWork uow = new UnitOfWork(context);
  foreach (var department in uow.Departments.GetAll())
  {
    Console.WriteLine(department.Name);
  }
 
  foreach (var department in uow.Departments.Query(d => d.Budget > 150000))
  {
    Console.WriteLine("department with above 150000 budget: {0}",
        department.Name);
  }
 
  foreach (var department in uow.Departments.QueryObjectGraph(d => d.Budget > 150000, "Courses"))       
  {
    Console.WriteLine("department with above 150000 budget: {0}, {1}",
        department.Name, department.Courses.First().Title);
  }
}

Summary

In the previous post I only showed the way to implement your
repositories. As you can see in this post I can take the offered
solution and make it specific to my needs.

Revisiting the Repository and Unit of Work Patterns with Entity Framework

Revisiting the Repository and Unit of Work Patterns with Entity Framework

In the past I wrote twoRevisiting the Repository and Unit of Work Patterns with Entity Framework
posts about the
Repository and the
Unit of Work patterns
(here and here). Today
I want to show a better
and less naive solution
for imposing the Unit of Work and the Repository patterns with
Entity Framework.

Revisiting The Repositoy Implementation

In the Repository pattern, I added to the interface two new
methods for adding and removing an entity:

public interface IRepository<T>
                where T : class
{
  T GetById(int id);
  IEnumerable<T> GetAll();
  IEnumerable<T> Query(Expression<Func<T, bool>> filter);    
  void Add(T entity);
  void Remove(T entity);
}

Also I’ve changed the return type of the Query from IQueryable
to IEnumerable in order not to enable additional composition
on that query that will hit the database.
The implementation of the Repository itself will change as follows:

public abstract class Repository<T> : IRepository<T>
                                  where T : class
{
  #region Members
 
  protected IObjectSet<T> _objectSet;
 
  #endregion
 
  #region Ctor
 
  public Repository(ObjectContext context)
  {
    _objectSet = context.CreateObjectSet<T>();
  }
 
  #endregion
 
  #region IRepository<T> Members
 
  public IEnumerable<T> GetAll()
  {
    return _objectSet;
  }
 
  public abstract T GetById(int id);
 
  public IEnumerable<T> Query(Expression<Func<T, bool>> filter)
  {
    return _objectSet.Where(filter);
  }
 
  public void Add(T entity)
  {
    _objectSet.AddObject(entity);
  }
 
  public void Remove(T entity)
  {
    _objectSet.DeleteObject(entity);
  }
 
  #endregion

One of the things to notice is that I hold an IObjectSet as my data 
member of the Repository itself instead of holding the context like
in my previous post. The IObjectSet is a new interface in EF4
which helps to abstract the use of the created entity sets.
Now the Repository is better implemented and looks like an
in-memory collection as it was suppose to be. If I need more specific
methods I can inherit from this Repository implementation and add the
desired functionality. As you can see I don’t implement the GetById
method and enforce my concretes to implement it.
The DepartmentRepository from the previous post will look like:

public class DepartmentRepository : Repository<Department>
{
   #region Ctor
 
   public DepartmentRepository(ObjectContext context)
     : base(context)
   {
   }
 
   #endregion
 
   #region Methods
 
   public override Department GetById(int id)
   {
     return _objectSet.SingleOrDefault(e => e.DepartmentID == id);
   }
 
   #endregion
}

Revisiting The Unit of Work Implementation

After we have our Repository we would like to create a
Unit of Work to hold the application repositories and to
orchestrate the persisting of data for some business transactions.
The new interface for the Unit of Work will look like:

public interface IUnitOfWork
{
  IRepository<Department> Departments { get; }    
  void Commit();
}

By of course if you have more repositories in your application you
will add them to the IUnitOfWork (in the example I only have the
departments repository). Now the implementation of the Unit of Work
won’t be a part of the repository (like in the old post) and will look like:

public class UnitOfWork : IUnitOfWork
{
  #region Members
 
  private readonly ObjectContext _context;
  private DepartmentRepository _departments;
 
  #endregion
 
  #region Ctor
 
  public UnitOfWork(ObjectContext context)
  {
    if (context == null)
    {
      throw new ArgumentNullException("context wasn't supplied");
    }
 
    _context = context;
  }
 
  #endregion
 
  #region IUnitOfWork Members
 
  public IRepository<Department> Departments
  {
    get
    {
      if (_departments == null)
      {
        _departments = new DepartmentRepository(_context);
      }
      return _departments;
 
    }
  }
 
  public void Commit()
  {
    _context.SaveChanges();
  }
 
  #endregion
}

As can be seen the Unit of Work holds the ObjectContext but
you could use an IContext interface instead if you like to be more
abstract and with no dependency on Entity Framework at all. This can be
achieved by using the T4 templates that were provided in EF4 which you
will use to add the IContext interface implementation. The
Unit of Work has a constructor that can be injected using an IoC container.
The testing program will change into:

using (SchoolEntities context = new SchoolEntities())
{
  UnitOfWork uow = new UnitOfWork(context);
  foreach (var department in uow.Departments.GetAll())
  {
    Console.WriteLine(department.Name);
  }
 
  foreach (var department in uow.Departments.Query(d => d.Budget > 150000))
  {
    Console.WriteLine("department with above 150000 budget: {0}",
        department.Name);
  }
}

Some thing to notice here is that I create the context during
the running of the program. As I wrote earlier, this can be changed
to using an IoC container instead which will inject the constructor
dependency.

Summary

Lets sum up, this offered solution is much better then the naive
example I gave in the previous posts. It is much more testable and
abstract and as I wrote you could go further and use an IoC container
to inject the use of Entity Framework in the Unit of Work
implementation.

Consuming OData Feed using Microsoft PowerPivot

Consuming OData Feed using Microsoft PowerPivot

PowerPivot is a data analysis
add-inConsuming OData Feed Using Microsoft PowerPivot for excel that brings to
it computational power.
It also helps “to create compelling
self-service BI solutions, facilitates
sharing and collaboration
on user-generated BI solutions”
(taken from the PowerPivot site).
This post will help you to understand how to consume OData feed
from PowerPivot in order to use the data the OData exposes.

The OData Feed

OData exposed feeds can be found in the http://www.odata.org/ site.
Under the Producers tab you will find OData producers which expose
their data using the OData protocol. In my example I’ll use the
Netflix’s feed that can be found here: http://odata.netflix.com/Catalog/.

Consuming Netflix OData Feed using PowerPivot

In order to use PowerPivot for excel you first need to download it.
There are some prerequisites for doing that including having Office
2010 installed on your machine. After installing PowerPivot open your
excel and go to the PowerPivot tab and open PowerPivot window.
One of the options in the PowerPivot window is to get data from
data feeds (you will see the OData protocol logo beside it):
PowerPivot Window

Use the From Data Feeds menu item in order to open the Table
Import Wizard
:
Table Import Wizard 

In the Data Feed Url textbox insert the relevant OData feed (in my
case the Netflix’s feed Url). Press Next to import the data definition
and then you will see the entity sets the feed exposes as tables:
Data Feed Tables

Choose the tables you want to import and press Finish to end the
process and import the tables. This can take some time depending
on the size of the exposed data set.
I chose the Genres set and this is the result:
Imported Results
Now I can use the PowerPivot’s features to build my relevant report
or to manipulate the data I’ve received.

Summary

Let sum up, I showed how to consume OData feed using Microsoft’s
PowerPivot. The OData ecosystem is growing very fast and as you
could see from this example it consumers can also be BI tools like
PowerPivot and many more. This is one of the reasons to start
learning this protocol now.

Using Paging in WCF Data Services

Using Paging in WCF Data Services

One of the mechanismsUsing Paging in WCF Data Services
which were provided
in WCF Data Services
from the start was
client side paging.
In the new release of
WCF Data Services we also
get a server side paging and this will be addressed in this post.

WCF Data Services Client Side Paging

From the early days of WCF Data Services we could achieve paging
on the client side using the $top and $skip query parameters.
For example the following URI for a data service will bring the 11-20
courses which were requested:

The problem starts when you expose resources with a lot of items.
Lets say that we have 10000 courses in our course library. The following
URI will bring to the client all of them:

Running this request will decrease the amount of clients for the service
since it will take ages for the query to return. So what can we do?
use server side paging instead.

WCF Data Services Server Side Paging

So how can we limit our returning result set to the client? we can
use a new feature of WCF Data Services. In order to create a
server side paging behavior all we need to do is to configure the
service to return some portion of data. We will use the new method
of the DataServiceConfiguration class which is called
SetEntitySetPageSize for each entity set or for all of them at once
(using * ). The following code create server side paging for all the
entity sets exposed by the data service to return only 10 items
at once:

public class SchoolDataService : DataService<SchoolEntities>
{    
  public static void InitializeService(DataServiceConfiguration config)
  {
    config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
    config.SetEntitySetPageSize("*", 10);
    config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
  }
}

When I use the service now with a URI to retrieve all the courses I will
get only the first ten courses. Also as you can see in the figure we
get a link at the bottom that leads us the next ten results:
Server Side Paging

and the link itself is written as:

http://localhost:8322/SchoolDataService.svc/Courses?$skiptoken=4041

Summary

In the new release of WCF Data Services we get a new paging
feature which enable server side paging. Using this method
or combining it with client side paging extend the capabilities
of WCF Data Services. The use of the server side paging is very
easy – just configure the service. In the post I showed an example
of how you can achieve that behavior.

FeedBurner Counter Hiccups

FeedBurner Counter Hiccups

I’m using the FeedBurner Feedburner Counter Hiccups
counter in my blog from
day one in order to see
how many readers I have
in my blog. Using the service
I could see that during time the amount of readers in my
blog started to grow. In the last couple of weeks I could see
some counter hiccups. One day I had above 400 readers and
in the next day all the readers that are registered with
Google Feedfetcher suddenly disappeared and the counter dropped
to 200 readers. This behavior was written in the FeedBurner Status Blog
Today suddenly my counter exploded with 1261 readers. Since
yesterday it was 381 readers, I went to my FeedBurner account and
checked it. I could see that suddenly I have 885 readers which use
Netvibes.
Even though it is flattering to have such an amount of readers, I guess
that this number isn’t true.
Since I remembered that in the past there were hacks that tempered
the counter using Netvibes, I write this post in order not to damage my
credibility. To be honest I wish this amount is true but I currently don’t
know if it is and I couldn’t find a way to contact FeedBurner in order
to check it out.

Using Conditional Mapping in Entity Framework

Using Conditional Mapping in Entity Framework

There are timesUsing Conditional Mapping in Entity Framework
that we want to
retrieve only
a portion of data
from a table in
the database
permanently by some
filter. For that purpose
we can use the conditional mapping in Entity Framework.

What is Conditional Mapping?

Conditional mapping is a fixed condition that helps use to
filter the result set that is being returned from the database
for a specific entity. Also it enforces that an entity is mapped
to data in the database under only certain conditions which are
supplied inside the conditional mapping.
In order to use conditional mapping we need to open the
Mapping Details View. In the view we can add for each entity
a conditional mapping using the <Add a Condition>:
Conditional Mapping
When we create a condition, that condition will be added to each
query that we will make to the database.

Available Conditional Mappings

There are some possible conditional mapping in Entity Framework
which are provided by two kind of operators – the equality operator
and the Is operator.
The equality operator (=) can have values of strings or integers.
The Is operator check whether a column is Null or Not Null.
When we have more then one condition it will construct an And
operation between all the conditions.

Conditional Mapping Example

A very common example for using a conditional mapping is having
a table field that indicate a logical delete (for example an IsDeleted
field). Since we want to present only undeleted rows/entities
then a conditional mapping can be a valid solution.

In the example I’m using the following table that represent a course data:
Course Table
The IsDeleted has a default value of 0 to indicate that a row’s logical
state is not deleted. If the row is deleted I change the value to 1.

This is the entity in the generated model before I make any
changes:
Course Entity
Now I want to impose the conditional mapping on the course entity.
The first thing to do is to delete the IsDeleted property since it
will be used by the conditional mapping. Then I create a conditional
mapping
that retrieve only entities with IsDeleted equals to 0 in
the following way:
The Conditional Mapping

That is it.
Now whenever I query for courses, the generated query will be
created with a where clause that check whether IsDeleted equals 0.
For example if I query for all courses the following query will be sent to
the database:

SELECT 
[Extent1].[CourseID] AS [CourseID], 
[Extent1].[Title] AS [Title], 
[Extent1].[Days] AS [Days], 
[Extent1].[Time] AS [Time], 
[Extent1].[Location] AS [Location], 
[Extent1].[Credits] AS [Credits], 
[Extent1].[DepartmentID] AS [DepartmentID]
FROM [dbo].[Course] AS [Extent1]
WHERE [Extent1].[IsDeleted] =  CAST( '0' AS tinyint)

Since I use tinyint as data type in the database then there is a
casting in the query.

Summary

A conditional mapping in Entity Framework helps to filter returning
results. If you have a fixed behavior such as logical delete then
conditional mapping can help you achieve the relevant behavior.

Cache Retrieval Pattern

Cache Retrieval Pattern

In my previous post I wrote about cache layer and its position
in every application. In this post I’m going to explain what is
the cache retrieval pattern and show an example of how to
implement it.

Cache Retrieval Pattern

When we implement a cache layer we need a strategy in order to
retrieve cached items. The cache retrieval pattern is very
simple and can be imposed into any application very fast.
So how does it work?
The business logic component will use the cache API in order to
check whether some data exists in the cache. If the data doesn’t exists
in the cache, the business logic component responsibility will be to
insert the data into the cache. Since cache is volatile you can’t depend
on it to hold the data you need. This is why you always need to check
whether the data exists in the first place. The business logic component
will use a data access component to retrieve the relevant data from a
data storage. After it has the data in hand it will place the data in
the cache and return it to its consumer. The following diagram
shows how this pattern work:
Cache Retrieval Pattern

Cache Retrieval Pattern Example

After we understood the what lets take a look at the how.
The following BL class implement the data retrieval pattern:

public class BLComponent
{
  #region Properties
 
  private ICacheManager CacheManager { get; private set; }    
 
  #endregion
 
  #region Ctor
 
  public BLComponent(ICacheManager cacheManager)
  {
    CacheManager = cacheManager;
  }
 
  #endregion
 
  #region Methods
 
  public Department GetDepartmentById(int departmentId)
  {
    if (!CacheManager.Contains(departmentId.ToString()))
    {
      DALComponent dataAccessor = new DALComponent();
      Department department = dataAccessor.GetDepartmentById(departmentId);
      CacheManager.Insert(departmentId.ToString(), department);
      return department;
    }
    return CacheManager.Get<Department>(departmentId.ToString());
  }
 
  #endregion
}

As you can see I use the ICacheManager interface in order to
create an abstraction against the cache layer. This will enable
me to create a class which uses the cache API of my chosen
cache implementation (in memory, distributed and etc).
The ICacheManager can look like:

public interface ICacheManager
{
  /// <summary>     
  /// Add a new object into the cache     
  /// </summary>     
  /// <param name="key">The key of the object to add</param>     
  /// <param name="value">The value of the object to add</param>     
  void Add(string key, object value);
 
  /// <summary>     
  /// Check whether the key is contained by the cache     
  /// </summary>     
  /// <param name="key">The key to check</param>     
  /// <returns>Returns true if the key is contained by the cache</returns>     
  bool Contains(string key);
 
  /// <summary>     
  /// Returns the number of items in the cache     
  /// </summary>     
  /// <returns></returns>     
  int Count();
 
  /// <summary>     
  /// Insert a new object into the cache      
  /// </summary>     
  /// <param name="key">The key of the object to insert</param>     
  /// <param name="value">The value of the object to insert</param>     
  void Insert(string key, object value);
 
  /// <summary>     
  /// Get the object that its key is given    
  /// </summary>     
  /// <typeparam name="T">The object</typeparam>     
  /// <param name="key">The given key to check</param>     
  /// <returns>returns the object or null if it doesn't exists</returns>     
  T Get<T>(string key);
 
  /// <summary>     
  /// Removes the object that is referenced by the given key     
  /// </summary>     
  /// <param name="key">The given key</param>     
  void Remove(string key);
 
  /// <summary>     
  /// Get/Set the the given key and object     
  /// </summary>     
  /// <param name="key">The given key to the indexer</param>     
  /// <returns>Returns the object that the given key reference</returns>     
  object this[string key]
  {
    get;
    set;
  }
}

The data access component isn’t relevant to our discussion because
underneath it can be implemented by IRepository, some service or
whatever method you pick to retrieve your relevant data. Also,
pay attention that this example isn’t thread safe.

Summary

The cache retrieval pattern is simple to understand and implement
in any application. As I wrote in my previous post using caching in
applications is a must and the pattern for retrieving data will help
you to insure you have the relevant data close to your application.

Cache Layer

Cache Layer

Lately I found myself in some architecture consulting sessions at
some customers. In every one of those customers I found myself
explaining how to implement a cache layer in order to decrease
the amount of round trips to the database and for better scalability.
In this post I’ll try to explain in high level how to build a cache layer.

Deciding to Build a Cache Layer

Every application that performance is important to its developers
and managers must contain some sort of caching. The cache is a very
fast in memory resources container which holds relevant data close to the
application itself. I explained in the past the candidate resource types
for caching in this post. So you have decide to implement caching
in your application, what next?
The answer is simple – create a cache layer.

Cache Layer Position in Application Architecture

The next thing to understand is the location of the cache layer in your
application. The next diagram shows were you should place the cache layer:
Cache Layer
As you can see the cache layer's best place is between the business
logic layer and the data access layer. The business logic responsibility
will be to check whether an entity or some data exists in the cache
and then if not to make a round trip to the database (using the data
access layer of course) and bring the data to application. When the data
exists we first put it in the cache and then we have the data in hand
and also it exists our the cache. This is called the cache retrieval pattern.
There are many ways to create a cache layer for example by using
distributed cache like AppFabric caching services, Enterprise Library
Caching Application Block, ASP.NET caching and many more cache services.
It’s your responsibility to evaluate which cache technology to use in your
application by answering to your application requirements.

Summary

Caching in applications is a very vital way to improve the application
performance and make the application more scalable. It’s our
responsibility to create a cache layer which will exists between
your business logic and data access layer. By using the cache retrieval
pattern
you will be able to use your cache in a very efficient way.

Microsoft Israel Sites were DNS Hijacked Yesterday

Microsoft Israel Sites were DNS Hijacked Yesterday

Yesterday there was a DNS hijacking attack on some of
Microsoft Israel sites including this Microsoft blog community.
The sites were defaced with some offensive content which show
that the people behind the attack are very childish and narrow
minded. 

This blog community is sharing its experience and knowledge with
all the Microsoft community all over the world. Denying its service
only hurts the people who search the data from this source of
knowledge.
You won’t see here political posts or other stuff since there are other
places which you can write your opinions such as Facebook, Twitter,
Talkbacks and many more. The posts here are mainly technical
for the Microsoft community worldwide. Since this is the case, attacking
this site or other sites only because they are Israeli sites only show
how shameless and narrow minded the attackers.

One last thought, changing a DNS register can be caused by inside job in
a domain register company (by of course there are other options and I’m
not accusing anyone). I hope that Microsoft will make sure this won’t
happen again.

Entity Framework 4 Course

Entity Framework 4 Course

In the last couple of months,
I have updated myEntity Framework 4 Course
Entity Framework course
to include the new subjects
and features of EF4. Yesterday I
finished to deliver the first cycle
of this course and got good reactions
about it.


If you want to learn what is Entity Framework, how to use it and
how to integrate it into applications you can take a look at the
following syllabus and to register for the three days instructor lead
course I’ve created.
See you there.

SQL For .Net Developers – Another Sessions Cycle

SQL For .Net Developers – Another Sessions Cycle

SQL For .Net Developers – Another Sessions Cycle

Since the SQL For .Net Developers series of sessions was successful
Microsoft is conducting another sessions cycle for whoever missed
the first cycle. I’m scheduled to deliver a session about
Entity Framework 4.0 & LINQ on 27.06.2010 at Microsoft office
in Raanana starting at 16:30 until 19:30.
For further details (in Hebrew) and registration you can use this link.
You can read a review of Gadi J. Meir (in Hebrew) about the session
here (Thanks Gadi).
See you there!