Thursday, April 22, 2010

.NET Framework General Reference

.NET Framework General Reference
Design Guidelines for Class Library Developers



http://msdn.microsoft.com/en-us/library/czefa0ke(vs.71).aspx

Monday, February 15, 2010

Limit the number of namespaces used in your .net project of VS

How to limit the number of namespaces included in your .net project in VS?

Method 1:

it involves changing the default project item templates. You can find them in %ProgramFiles%\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates (for Visual Studio 2008). You may need to
clear out \Common7\IDE\ItemTemplatesCache before Visual Studio picks up your changes, although it should do this automatically.

Be careful and make sure to backup these directories first, because damaging them will render you unable to add new items from Visual Studio.

Not sure whether VS Express support this feature!

Method2:

Right click your code in the editor, and on the context menu, select "Organize Usings", then on the pop-out, select "Remove Unused Usings".

Sunday, February 14, 2010

DbLinq to Linq2Anydb

http://linq.to/db

LINQ: Anonymous Class - select new

http://nayyeri.net/use-anonymous-types-to-select-data-in-linq

When using LINQ, normally you select data by using regular types.  But thank to new language features in .NET 3.5, you're able to select entities with Anonymous Types, too.  Follow this post to see how!

Sample Application

Let me write a simple sample console application to use in this post.  After creating this console application for .NET 3.5, I create a database with two tables named Person and Address.  As names suggest, they keep data for persons and their addresses and there is a relationship between a field in Address and a field in Person.
Now I add a new LINQ to SQL file to my project and drop my tables into it.
Tables
Now that I have all generated classes for my purpose, I can implement my code in the console application.

Select Entities with Anonymous Types

First I write a simple code to select the name of persons.
static void Main(string[] args)
{
    Console.Title = "Use Anonymous Types to Select Entities in LINQ";

    DataClasses1DataContext dataContext = new DataClasses1DataContext();

    var myQuery = from p in dataContext.Persons
                  select p.Name;

    foreach (var person in myQuery)
    {
        Console.WriteLine(person);
    }

    Console.ReadLine();
}
Output
Don't laugh to sample data, please!!
This was a sample of selecting all persons with a normal string type for their Name field.  But alternatively, you can select them with anonymous types to have a combination of them.
static void Main(string[] args)
{
    Console.Title = "Use Anonymous Types to Select Entities in LINQ";

    DataClasses1DataContext dataContext = new DataClasses1DataContext();

    var myQuery = from p in dataContext.Persons
                  select new { p.Name, p.Age };

    foreach (var person in myQuery)
    {
        Console.WriteLine(person);
    }

    Console.ReadLine();
}
Output
Using anonymous types, I can also get the benefit of assigning new names to data fields as is shown below.
static void Main(string[] args)
{
    Console.Title = "Use Anonymous Types to Select Entities in LINQ";

    DataClasses1DataContext dataContext = new DataClasses1DataContext();

    var myQuery = from p in dataContext.Persons
                  select new
                        {
                            PersonName = p.Name,
                            PersonAge = p.Age,
                            PersonAddress = p.Address.Line1 + "\n" +
                            p.Address.Line2 + "\n" +
                            p.Address.Zip
                        };

    foreach (var person in myQuery)
    {
        Console.WriteLine(person);
    }

    Console.ReadLine();
}
Output

LINQ: Anonymous types - select new

 http://msdn.microsoft.com/en-us/library/bb397696.aspx

Anonymous Types (C# Programming Guide)

Updated: July 2008

Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to first explicitly define a type. The type name is generated by the compiler and is not available at the source code level. The type of the properties is inferred by the compiler. The following example shows an anonymous type being initialized with two properties called Amount and Message.

var v = new { Amount = 108, Message = "Hello" };

Anonymous types are typically used in the select clause of a query expression to return a subset of the properties from each object in the source sequence. For more information about queries, see LINQ Query Expressions (C# Programming Guide).

Anonymous types are created by using the new operator with an object initializer. For more information about object initializers, see Object and Collection Initializers (C# Programming Guide).

Anonymous types are class types that consist of one or more public read-only properties. No other kinds of class members such as methods or events are allowed. An anonymous type cannot be cast to any interface or type except for object.

The most common scenario is to initialize an anonymous type with some properties from another type. In the following example, assume a class that is named Product that includes Color and Price properties together with several other properties that you are not interested in. Products is a collection of Product objects. The anonymous type declaration starts with the new keyword. It initializes a new type that uses only two properties from Product. This causes a smaller amount of data to be returned in the query.

If you do not specify member names in the anonymous type, the compiler gives the anonymous type members the same name as the property being used to initialize them. You must provide a name to a property that is being initialized with an expression.
C#

var productQuery =
    from prod in products
    select new { prod.Color, prod.Price };

foreach (var v in productQuery)
{
    Console.WriteLine("Color={0}, Price={1}", v.Color, v.Price);
}

When an anonymous type is assigned to a variable, that variable must be initialized with the var construct. This is because only the compiler has access to the underlying name of the anonymous type. For more information about var, see Implicitly Typed Local Variables (C# Programming Guide).
 Remarks

Anonymous types are reference types that derive directly from object. The compiler gives them a name although your application cannot access it. From the perspective of the common language runtime, an anonymous type is no different from any other reference type, except that it cannot be cast to any type except for object.

If two or more anonymous types have the same number and type of properties in the same order, the compiler treats them as the same type and they share the same compiler-generated type information.

An anonymous type has method scope. To pass an anonymous type, or a collection that contains anonymous types, outside a method boundary, you must first cast the type to object. However, this defeats the strong typing of the anonymous type. If you must store your query results or pass them outside the method boundary, consider using an ordinary named struct or class instead of an anonymous type.

Anonymous types cannot contain unsafe types as properties.

Because the Equals and GetHashCode methods on anonymous types are defined in terms of the Equals and GetHashcode of the properties, two instances of the same anonymous type are equal only if all their properties are equal.

Courtesy: MSDN

Thursday, January 21, 2010

RESTful service

http://www.ibm.com/developerworks/webservices/library/ws-restful/

Wednesday, December 23, 2009

.Net Naming Convention

http://msdn.microsoft.com/en-us/library/ms229043.aspx