Archive for November, 2008

Creating Entity Connection in Linq To Entities (Ado.net Entity Framework)

there are key scenarios in which you want to create your connection string at runtime (dynamically), instead of old Connectionstring Ado.net entity framework uses "EntityConnection" string

now how one will proceed if he wants to create EntityConnection at runtime say read from configuration and then use it or in case of DDR (data dependent routing)  here is the solution that will save you from this trouble.

  • Create Instance of EntityConnectionStringBuilder
  • Assign it the "SQL Connection String"
  • provide the meta data
  • Assign the provider name
  • then all you have to request EntityConnectionStringBuilder to return value from ToString();
  • and you are good to go 🙂
   1: EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
   2: entityBuilder.ProviderConnectionString = connectionstring;
   3: entityBuilder.Metadata = "res://*/";
   4: entityBuilder.Provider = "System.Data.SqlClient";
   5: return entityBuilder.ToString();

kick it on DotNetKicks.com  Shout it

Advertisements

3 Comments

Extension Methods (Decorator Pattern Microsoft Way)

With traditional object oriented development, extending objects (classes) is normally achieved by deriving class from base class and placing extra functionality there.

but some times you are not allowed to extend classes (marked not extendible sealed/NotInheritable, as a result there is no way to extend these classes although you can still use some patterns like decorator to achieve the goal but the hard way.

A good example of this is System.String class as its non extendible

To solve this issue the easy way Microsoft implemented in the form of Extension Methods in Dotnet framework 3.5, it lets you extend any existing type’s functionality even when a type is non extendible

How to create Extension Method

lets create a sample for Extension Methods, we will extend String class and will provide 3 extension methods namely

  • Is Valid (just verifies instant of the string returns true/false
  • ToString (will talk about this one later)
  • ToAlternateString (this will return a simple string)

lets start (I am using c# for this example):

  1. Create a C# console application
  2. Create a new class as in my example I have named it “Extensions” with the namespace “StringExtensions” and mark the class as “static”
  3. Create the 3 methods we talked about above and mark them as static too.
  4. now here comes the real trick the first parameter of the method specifies the type that the method operates on; it must be preceded with the this modifier e.g. in my case
       1: public static bool IsValid(this string value)

    you can see that in the parameter I have added this and after that the objecttype which in my case is string, it ensures that this method will work only on string type objects.
    Do note that Extension Methods can not access any private data in the extended class.

  5. here is my Extensions class
       1: namespace StringExtensions
       2: {
       3:     public static class Extenions
       4:     {
       5:         /// <summary>
       6:         /// to validate if value is valid or not just an alternate implementation of isnullorempty
       7:         /// </summary>
       8:         /// <param name="value"></param>
       9:         /// <returns></returns>
      10:         public static bool IsValid(this string value)
      11:         {
      12:             return string.IsNullOrEmpty(value);
      13:         }
      14:  
      15:         /// <summary>
      16:         /// Extending string method to prove that it always calls base
      17:         /// </summary>
      18:         /// <param name="obj"></param>
      19:         /// <returns></returns>
      20:         public static string ToString(this string obj)
      21:         {
      22:             return "To String method Extended";
      23:         }
      24:  
      25:  
      26:         /// <summary>
      27:         /// Writing my string method
      28:         /// </summary>
      29:         /// <param name="obj"></param>
      30:         /// <returns></returns>
      31:         public static string ToAlternateString(this string obj)
      32:         {
      33:             return "My alternate String method called";
      34:         }
      35:     }
      36: }
  6. here is my Program class
       1: using System;
       2: using StringExtensions;
       3:  
       4: namespace Extension_Methods
       5: {
       6:     class Program
       7:     {
       8:         static void Main(string[] args)
       9:         {
      10:             string obj=null;
      11:             Console.WriteLine(String.Format("Calling extended method IsValid"));
      12:             Console.WriteLine(String.Format("Object is Null={0}", obj.IsValid()));
      13:             Console.WriteLine();
      14:  
      15:             Console.WriteLine(String.Format("Intializing string object with value obj=string"));
      16:             obj="string";
      17:             Console.WriteLine(String.Format("Value of String Object is {0}", obj.ToString()));
      18:             Console.WriteLine();
      19:  
      20:             Console.WriteLine(String.Format("Calling Alternate string Value method"));
      21:             Console.WriteLine(String.Format("Value of String Object is {0}", obj.ToAlternateString()));
      22:             Console.ReadKey();
      23:         }
      24:     }
      25: }
  7. Now execute the program

you will notice that Isvalid and ToAlternateString methods are working fine but “ToString()” method we extended it is not working as our output was supposed to be “To String method Extended” but in this case its the actual value.

That is the one of the beauty of Extension Methods “An extension method will never be called if it has the same signature as a method defined in the type/Instance.

Source Code:

 http://cid-61e3517bd730d0c7.skydrive.live.com/embedrowdetail.aspx/Public/ExtensionMethods.zip

there are some other topics related to this which I will cover later.

 

kick it on DotNetKicks.com  Shout it

Leave a comment

Switching from Linq2SQL To LINQ-TO-ENTITIES

now a days for a new project with enterprise level database, I am considering LINQ To Entities as an option; I really like LINQ2SQL but after Microsoft’s Announcement regarding decreasing there support from it (you can read more at my previous post ), this shift was necessary.

Ado.Net has tried to clarified in there post that they are unable to continue support for both due to large number of feature request.

I am facing problems limitations and stuff like that in coming days I will post them here.

Leave a comment

Linq to SQL: Is Microsoft really planning to end it?

just from last couple of days every one is buzzing about the future of "Linq to SQL". the main reason of this hotshot was Microsoft itself there on Ado.net team blog "Update on LINQ to SQL and LINQ to Entities Roadmap"
"We’re making significant investments in the Entity Framework such that as of .NET 4.0 the Entity Framework will be our recommended data access solution for LINQ to relational scenarios.  We are listening to customers regarding LINQ to SQL and will continue to evolve the product based on feedback we receive from the community as well."

I have used Linq to SQL a couple of times and in one of our main project we are using it as a data layer I liked it because of its ease of use and drop in development cost as it is really good for small level data layers efficient (I know not most of you agreed to me), effective and easy to use. on Other hand Ado.net Entity Framework requires a substantial learning curve and quite some configurations; also by doing this they have helped NHibernate as they will be the one really happy about all this fiasco.

I had just started to wonder what would be the case even with Silverlight… will Microsoft keeps sending pre-cooked products….. well! if it does (or doing) they are just killing themselves slowly

Technorati Tags: ,

kick it on DotNetKicks.com

Leave a comment

Microsoft Could Platform Windows Azure

At last Microsoft has announced its upcoming cloud platform "".

Microsoft defines this as "Windows Azure is a cloud services operating system that serves as the development, service hosting, and service management environment for the Azure Services Platform. Windows Azure provides developers with on-demand compute and storage to host and manage web applications on the Internet through Microsoft data centers."

Stephen Forte has a good blog entry on this

Technorati Tags:

Leave a comment