• IEnumberable, IQueryable , Lambda expression - part2

    I have seen such following piece of code written by a developer from a client.

    interface IContactRepository
    {
        IEnumberable<contact> GetSomeContacts();
    }
    
    
  • IEnumberable, IQueryable , Lambda expression - part1

    When we type the following code

    IEnumerable<int> intEnumerable = null;
    var q1 = intEnumerable.Where( x => x > 10);
    


    we know that Where method is not part of the IEnumberable<t> interface or IEnumberable interface, it comes from extension method of Enumerable, which is static class and it has no inheritance relation With IEnumerable or IEnumberable<t>. The power of Linq-To-Object does not come from IEnumberable or IEnumberable<t> or its implemenation, it comes from the extension method. Let's take a look what does the extension method do? Using Reflector we get the following source code.


    public static class Enumerable
    {
    public static IEnumerable<tsource> Where<tsource>(this IEnumerable<tsource> source, Func<TSource, bool> predicate)
    {
        if (source == null)
        {
            throw Error.ArgumentNull("source");
        }
        if (predicate == null)
        {
            throw Error.ArgumentNull("predicate");
        }
        if (source is Iterator<tsource>)
        {
            return ((Iterator<tsource>) source).Where(predicate);
        }
        if (source is TSource[])
        {
            return new WhereArrayIterator<tsource>((TSource[]) source, predicate);
        }
        if (source is List<tsource>)
        {
            return new WhereListIterator<tsource>((List<tsource>) source, predicate);
        }
        return new WhereEnumerableIterator<tsource>(source, predicate);
    }
    }
    

    We can see there , the delegate passed in to the method is the code that does the filtering.

    IQueryable<t> inherit from IEnumerable. But what extra value does the IQueryable bring. Let's take a look of the following code. and the code it generated by c# compiler.

    public interface IQueryable<t> : IEnumerable<t>, IQueryable, IEnumerable
    {}
    
    
  • Raise Event from outside

    Normally raising event is the responsibility side of a class, but in workflow there is a need to raise event from outside. Here is the code that allow client dynamically raise event externally.

    class EventRaiser
        {
            static BindingFlags getEventFlags = BindingFlags.Instance | BindingFlags.NonPublic;
            private object _eventObject;
            private MethodInfo _eventInvoker;
    
    
  • Disconnected Update with Entity Framework

    In the first demo, we get disconnected entity, make change of it, and get copy of original copy from the database, and apply the changed entity to the original entity by using context.ApplyPropertyChanges method, and save it back to the database.

  • Reference and EntityKey

    When add an entity to your objectContext, if the entity reference an other existing entity, but that entity is not in memory, you need to create a EntityKey like the following.