Archive

Archive for the ‘LINQ’ Category

Entity Framework assications (Independent vs FK)

November 8, 2009 amiraryani Leave a comment

Entity Framework association is a different concept to FKs in the database schema.  Supporting or not supporting the Schema FKs has been a topic for debate in the community until now. Faisal Mohamood (Program Manager of Entity Framework) believes in the following prons and cons of using FKs in a model (Ref 1):

Benefits of Foreign Keys

  1. Keeps it simple (for the simple cases)  and allows you to deal with relationship like you deal with them in the database
  2. Technically, you can update relationships without having both ends loaded/materialized. This is however in reality not always interesting since you will likely load both ends but this feature is definitely useful.

Disadvantages of Foreign Keys in the Model

  1. It is a part of the impedance mismatch problem.
  2. It doesn’t allow the concepts that you would expect from relationships in objects (easily getting from one end to the other) for instance.
  3. Having foreign keys as well as object references for relationship navigation presents the problem of two different artifacts representing relationships – this introduces complexity and now you have to make sure that you keep these two in sync.

I think the biggest disadvantage of FKs (stated in number 2) is the conflict with the object model pattern.

However, it seems that Microsoft decided that both sides of this discussion have their valid points. So in Entity Framework .Net 4, Fks are supported and it is up to the software architects to make their decision to just use  independent associations (.Net 3.5 SP1) or add the FKs to the model.

The following code snippets form ADO.Net blog illustrate the implication of using Fks as part of the model (Ref 2). In our model there are two entities Product and Category, with the association one to many where each Product belongs to one Category.

First:  Using the independent association between Product and Category.

public void Create_new_Product_in_existing_Category_by_reference()
{
    using (var context = new Context())
    {
        //Create a new product and relate to an existing category
        Product p = new Product
        {
            ID = 1,
            Name = "Bovril",
            Category = context.Categories
                        .Single(c => c.Name == "Food")
        };
        // Note: no need to add the product, because relating
        // to an existing category does that automatically.
        // Also notice the use of the Single() query operator
        // this is new to EF in .NET 4.0 too.
        context.SaveChanges();
    }
}

Second: Using the Foreign Key.

using (var context = new Context())
{
    //Create a product and a relationship to a known category by ID
    Product p = new Product
    {
        ID = 1,
        Name = "Bovril",
        CategoryID = 13
    };
    //Add the product (and create the relationship by FK value)
    context.Products.AddObject(p);
    context.SaveChanges();
}

Further Reading:

References:

  1. [blogs.msdn.com/EFDesign]Foreign Keys in the Conceptual and Object Models
  2. [blogs.msdn.com/EFDesign]Foreign Keys in the Entity Framework
Categories: Entity Framework, LINQ

Dispatcher.Invoke (call an anonymous function)

July 13, 2009 amiraryani Leave a comment

In WPF to execute a code related to UI from a different thread the Dispatcher.Invoke should be used. And to avoid writing an extra delegate function you can use a anonymous function such as:

//Clear the contents of myStackPanel
this.Dispatcher.Invoke(
(Action)delegate { this.myStackPanel.Children.Clear(); }, null);

Stackoverflow.com: You need to tell the compiler what type of delegate to create ; MethodInvoker (2.0) or Action (3.5) are common choices (note they have the same signature); like so:

control.Invoke((MethodInvoker) delegate {this.Text = “Hi”;});

Another option is to write an extension method:

public static void Invoke(this Control control, Action action)
{
control.Invoke((Delegate)action);
}

then:

this.Invoke(delegate { this.Text = “hi”; });
// or simce we are using C# 3.0
this.Invoke(() => { this.Text = “hi”; });

You can of course do the same with BeginInvoke:

public static void BeginInvoke(this Control control, Action action)
{
control.BeginInvoke((Delegate)action);
}

Reference: http://stackoverflow.com/questions/253138/anonymous-method-in-invoke-call

LINQ – Makes programming easier

September 8, 2008 amiraryani Leave a comment

I am in favor of the idea that every new technology should be only used if they can solve a problem or makes or at least its benefit can justify the required effort for learning curve.

LINQ is one of those technologies that when I learn about that I could not find a really benefit, but when I tried that I stunt how effective it can be in reducing programming efforts.

This is a very basic and simple example from Microsoft website:

public void Linq13() {
    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
    string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

    var lowNums =
        from n in numbers
        where n < 5
        select digits[n];

    Console.WriteLine("Numbers < 5:");
    foreach (var num in lowNums) {
        Console.WriteLine(num);
    }    
}

Result

Numbers < 5:
four
one
three
two
zero

To for examples on LINQ:

C# 101 Examples: http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx

VB 101 Examples: http://msdn.microsoft.com/en-us/vbasic/bb688088.aspx

VB Query Examples: http://msdn.microsoft.com/en-us/vbasic/bb688084.aspx

Categories: LINQ