Archive for November, 2008
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 🙂
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 Decorator pattern 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):
- Create a C# console application
- Create a new class as in my example I have named it “Extensions” with the namespace “StringExtensions” and mark the class as “static”
- Create the 3 methods we talked about above and mark them as static too.
- 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
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.
- here is my Extensions class
- here is my Program class
- 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.”
there are some other topics related to this which I will cover later.
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 Linq To SQL: Is Microsoft really planning to end it?), 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.
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
At last Microsoft has announced its upcoming cloud platform "Windows Azure".
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