Wednesday, December 2, 2009

Brazilian With Hemorrhoids

Reloaded Run 2009 - the new Visual Studio Spying 2010!

A week ago was in the Autonomous City of Buenos Aires Microsoft event Reloaded Run, a super cool event which took place on 2 consecutive days and each day was split to average theme during the morning was devoted to IT professionals and then by the afternoon it came time for developers. And it is precisely this space that I will write a series of posts where I will comment precencia and those talks seemed super interesting, they all have the same thread, the new features and improvements in Visual Studio 2010 and Framework. Net 4.0 . I do wish to say that the event I like a lot, and I believe the level of the speakers and attendees was very good. I really had a great two days I could attend. Just as I love the space where they perform, the UADE (Universidad Argentina de la Empresa). I completed this little introduction to introduce the themes that I will try in this series of posts, then:
  • . Net 4.0 C # new support for dynamic programming (Dynamic Programming)
  • Entity Framework 4
  • Visual Studio 2010 new architecture tools

C # 4.0 dynamic programming

This is the first subject addressed in this series of three which corresponds to one of the presentations made during the event mentioned above, in a talk I had the immense pleasure to attend and where they commented on some progress with the language that has C #, you play the track of dynamic types types, and this for me is one of the most significant advances of language in this new release. Before
see some examples of code, we see a bit of what we mean by dynamic languages, in short a dynamic language performs certain tasks at runtime to perform static languages \u200b\u200bat compile time, tasks such as analysis of expressions , redefinition and alteration of rates and verification of data types.
Now what is the benefit that this brings with? because in many aspects, including assessing real-time formulas and code, facilitate the creation of DSL's (Domain Specific Language) including I have spoken in a previous post in this blog, provide the loose coupling and enable greater productivity in programming.
before turning himself to examples of code for the implementation of dynamic programming specific to C # 4.0, if we look a little history of the evolution of language c # we will see that historically began on the opposite side of this paradigm, ie C # language has emerged as a strongly typed language, structured, perhaps those who have ever programmed in Visual Basic 6 highly value the virtues of c # as microsoft before birth. Net Framework poseeia only visual c + + as a strongly typed language suitable for Work Related to object-oriented paradigm, this clarification was made on the basis that I have heard some critical voices regarding the implementation of dynamic programming in c #, but I think we have to take into account that this implementation is to give us a new tool when programming in C #. Dynamic programming gives us a great power but (to paraphrase Spiderman)
ALL A GREAT BIG BEAR RESPONSIBILITY CAN
This was an issue that it was raised during the talk by one of the speakers attending, and was brilliantly cleared by them , and that is the spirit of constructive discucion what I want to convey here.
short, C # 4.0 implements a solution to provide dynamic programming (soon we get into this solution), this means that we put aside the strongly typed code which in my point of view must always be the first choice, nor to forget the good practice of programming static or that we move away from the concepts of object-oriented paradigm, just think we use the dynamic programming when we really report a real benefit and to achieve those objectives to which we could not get past the language and was very tedious to do.

New
Heraion called dynamic !

Ok, made these clarifications and introductions see how C # 4.0 implements the dynamic programming, which first thing to mention is the new dynamic keyword, Visual C # 2010 introduced this new type that allows me to skip the static type checking at compile time.

An example in code:
 

{namespace ConsoleApplication1

class Train {public void
Brake (int i) {

/ / some code here}


} class Program {

static void Main (string [] args) {
Train
myTren = new Train ();
/ / myTren.Freno (12, 29);
dynamic
dynamic_myTren = new Train ();
dynamic_myTren.Freno (12, 29 )
dynamic_myTren.Freno ();

}}}


In this simple example shows how to work with the new keyword dynamic, with the first two lines of code I create an instance of the class and invoke the method train brake with two integer arguments, this action generates a compile-time error. Since the brake method only accepts one parameter and only one.
The second invocation using the new dynamic type allows me to invoke the method of the newly created object with two parameter or none, this method will be determined at runtime.
error when compiling (F5), which as expected, I get is: No overload for
method 'brake' Takes 2 arguments
Consider what happens when I use the dynamic keyword, such as sengunda instance I believe is the dynamic type checking compiler does not perform the method at compile time to be leaving this action execution time, now the error is not postponed indefinitely but the way it is written, this code will cause an exception at runtime.
The magic behind this is a new API Framework 4.0 Beta 2 called Dynamic Language Runtime (DLR) this component is that it provides the necessary infrastructure to implement dynamic programming in C # 4.0, in the following figure shows the architecture of it:



The DLR is part of Framework 4.0 Beta 2 and we can obtain information about the here. Place where I extracted the image above.

another example with code

Suppose we have 3 classes
 class 
Mariposa

{public void Fly ()

{Console.WriteLine ("Flying Butterfly");

}} class Bird


{public void Fly ()

{Console.WriteLine ("bird flying");

}} {

Train
class public void brake (int i) {

/ / some code here}



} Note that none of these classes implement a common interface or inherit from abstract class alguan or other basis, this means that there is no code that allows me to relate in a strongly typed these three classes.
The following method returns an instance of Pajaro
 
ObtenerAlgoQueVuela public object ()

{return new Bird ();}


Note that the return type is an object, which is suitable to simulate the situation in the we can get an object and we know quite well its kind, we think that the object may come from a third party dll or be an object written in a dynamic as IronPhyton or IronRuby. Maybe the only thing we know the order we receive is that it has a method called Fly, a reference in C # is object, I have no way of invoking the method flying object, as this causes an error at compile time, Yet we know that the returned object has this method that we need to call, here comes to our aid our new super hero called dynamic, then invoke the method as follows:
 dynamic 
ObtenerAlgoQueVuela AlgoQueVuela = ();
AlgoQueVuela.Volar ();

Thus we assign the reference returned by the method to a variable of type dynamic. The dynamic type is similar to the object (in this case) but I can do things dynamic and operate in a free jumping type checking at compile time because the binding of a dynamic type occurs in Timpo of execution.
For once we have programmed using Reflection to work this way where we get an arbitrary object and we must "dive" ourselves to execute a method, dynamic gives us invaluable assistance.
If you invoke a method on an instance of dynamic type and the method does not exist, instead of getting an error at compile time I get an exception at run-time, and fortunately the exception message we get at runtime is the same error message you get at compile time. In the following snippet of code generates an exception at runtime:
 
ObtenerCosasQueVuelan public IEnumerable ()

{yield return new Bird ();
yield return new Butterfly ();
yield return new Train ();}

foreach (dynamic ObjetoVolador in ObtenerCosasQueVuelan ()) {

ObjetoVolador.Volar ();}


The problem here is that when iterating through the collection returned by the method, to get the return value and this is an instance of Train this method has no fly and method exception occurs there. Conclusion



In this first post try to dump the best possible one of the topics that showed us speakers on developments in the. Net Framework 4, which was the dynamic programming in C # 4.0. As I said before this is a feature well-publicized (see the light as the future of C # at PDC 2008) and in turn eagerly awaited by many programmers who needed this flexibilidada in C #, and that to me makes C # a language programming increasingly powerful and multipurpose which gives us the opportunity to work in a true multi-paradigm environment.
This was all for now, greetings to all and happy programming!

0 comments:

Post a Comment