C# Generic Type Conversion

by UnquaLe 25. October 2007 02:45

I thought C# Generics are useful also for type convertion. I messed code alittle and the emergent code snippet looks fine for me. It is easy and simple. 

public class GenericConverter
{
    public static T Parse<T>(string sourceValue) where T : IConvertible
    {
      return (T)Convert.ChangeType(sourceValue, typeof(T));
    }

    public static T Parse<T>(string sourceValue, IFormatProvider provider) where T : IConvertible
    {
      return (T)Convert.ChangeType(sourceValue, typeof(T), provider);
    }
}

//Usage
DateTime datetime = GenericConverter.Parse<DateTime>("12.10.2007");


IFormatProvider providerEN = new System.Globalization.CultureInfo("en-US", true);
IFormatProvider providerTR = new System.Globalization.CultureInfo("tr-TR", true);

DateTime x = GenericConverter.Parse<DateTime>("12.10.2007", providerTR); // ddMMyyyy
DateTime y = GenericConverter.Parse<DateTime>("12.10.2007", providerEN); // MMddyyyy

int iValue = GenericConverter.Parse<int>("12");


Enjoy !.

Doga. 

Tags:

ASP.NET MVC Framework

by UnquaLe 22. October 2007 19:26
ScottGu had presented the new ASP.NET MVC Framework as a prototype last week in ALT.NET Conference.

Ultimately this is a group that believes in:

  • Continuous Learning
  • Being Open to Open Source Solutions
  • Challenging the Status Quo
  • Good Software Practices
  • DRY (Don't Repeat Yourself)
  • Common Sense when possible

It enables Test Driven Development by default. You can unit test the application without having to run the controllers within an ASP.NET process. You can use any unit-testing framework such as NUnit, NBUnit.

You can extend the framework by writing your own view engine. And the most amazing feature is to be pluggable. You can use any existing Dependency Injection and IOC container models such as Windsor, Spring.Net, NHibernate, etc.

You don't need URL-Rewriting frameworks anymore you can handle this process in your Controller classes. Your URLs will be SEO friendly easly.

These features and design choices represent a huge paradigm shift for Microsoft. They say to the open source .NET community: “We recognize your contributions, and we’re not going to try and reinvent what you’ve done. Instead, we’re going to make it pluggable, so you can use whatever technology you favor.”

It looks like Patterns & Practice team had really good job!. I wish i could be in this conference :( what a shame ! 

To watch The Gu and Scott Hanselman follow the link.

Thanks Guys !

Doga

Tags: , ,

.NET Framework 3.5 goes open source

by UnquaLe 4. October 2007 18:41

ScottGu posted about that and says his team has been working to enable ability for .NET developers to download and browse the source code of the .NET framework. So Visual Studio .NET 2008 will be delivered with source code(With source file comments included). That is really big feature that lets .Net developers to debug inside a .NET Framework class.

Click here and read the full Scott post! There are some great images on his post that will make us to dream within the final release.

To learn more about our source release plans and how the above debugger integration works, please check out this Podcast that Scott Hanselman and Shawn Burke recently recorded.  Shawn (who drove the source project on my team) is also going to be publishing a cool Channel9 video later this week that shows using the VS 2008 integrated debugging support with it.

Doga 

Tags: ,

C# 3.0 Orcas Language Features - Automatic Properties

by UnquaLe 1. October 2007 21:42

If you are developer you are probably quite used to writing classes with properties.

public class Customer
{
    private string firstname;
    public string Firstname
    {
        get { return firstname; }
        set { firstname = value; }
     }
}

 

This is a simple property and there is no logic in the getter and setter of the property. With new coming up C# 3.0 compiler provides short way to use properties with Automatic Properties feature.

When you are using C# 3.0 you can make your classes more concise, For example;

public class Customer
{
    public string Firstname { get; set; }
}

 

Yeah ! I know its cool to write a property in a line ;)

C# 3.0 Compiler will automaticly generate private fields in your class and implement a public getter / setter property implementation to it.

Bart De Smet has a great write-up on what happens under the covers when using automatic properties with the March CTP release of "Orcas".  You can read his excellent blog post on it here.

Tags: ,