add

Tuesday, June 9, 2009

New Features of dot Net 3.5 Framework

Dot net framework 3.5 is built on dot net 2.0 runtime library with addition of few dlls like: System.Core.dll, System.XML.LINQ.dll, System.AjaxExtension.dll etc..

Mainly enhancements are done with related to LINQ and Entitiy Model. Here are few enhancements listed:


1. Automatic Properties :

If you remember, prior to framework 3.5, we used to declare property within a class like this:

class Class1 {

privtae string _CustomerName;
public string CustomerName
{
get { return _CustomerName; }
set { _CustomerName = value; }
}
}


Now, we will see how to implement the same code in dot net 3.5:

class Class1 {
public string CustomerName {get; set;}
}





2. Object Initializers :

Another new feature coming with .NET 3.5, C# 3 and VB 9 is object initialization. Object Initializers allow you to pass in named values for each of the public properties that will then be used to initialize the object.

Suppose we have a class called School and few fields, we used to initialization the class like this:

School school1=new School();
school1.Name="ABC";
school1.Address="DEF";


Now the problem is that if we need to initialize different fields based on logic, we need to create different constructors based on our logic. Here comes the use of Object Initializers.

Object Initializers allow you to pass in any named public property to the constructor of the class. This is a great feature as it removes the need to create multiple overloaded constructors using different parameter lists to achieve the same goal.

So, from framework 3.5 onwards, we will use:

School school1=new School {
Name="ABC";
Address="DEF";
};





3. Collection Initializers :

Like Object Initializers, the new Collection Initializers allow you to create a collection and initialize it with a series of objects in a single statement. The following statement demonstrates how the syntax is very similar to that of the Object Initializers. Initializing a List is accomplished by passing the instances of the Customer objects wrapped inside of curly braces.

Getting this feature as part of framwork, we don not need to declare the collection first and then add items to it.


List SchoolList = new List
{
new School {ID = 101, SchoolName = "School1"},
new School {ID = 102, SchoolName = "School2"},
new School {ID = 103, SchoolName = "School3"}
};




4. Extension Method

Extension methods are a new feature that allows you to enhance an existing class by adding a new method to it without modifying the actual code for the class. This is especially useful when using LINQ because several extension methods are available in writing LINQ query expressions.

For more details on extension method, check "What is Enxtension Method "

Extension Methods are very useful when you cannot add a method to the class itself, as in the case of creating a Cube method on the int class. Just because you can use a tool, does not mean you should use a tool.




5. Anonymous Types

When you create an Anonymous Type you need to declare a variable to refer to the object. Since you do not know what type you will be getting (since it is a new and anonymous type), you can declare the variable with the var keyword. This technique is called using an Implicitly Typed Variable.

Chek more details on "All about Anonymous Types

Steps in Compliation in ASP.NET

We all know the term compilation in Asp.Net. But do you know what is the order of compilation in an asp.net project. Do you know which folder in the application structure gets compiled first and which when get compiled last?

Here is the compilation life cycle of an Asp.Net project in the order they get compiled.

App_GlobalResources: First of all the global resources are compiled and the resource assembly is built. All the assembly's in the bin folder are linked to this assembly.

App_WebResources: Next the web resource folder will be compiled. This will create all the proxy types for the web services. The web reference created is then linked to the resource assembly (if it exists.).

Profile properties (defined in the Web.config file): If we have defined the profile properties in the web.config file than an assembly will be generated for the profile objects.

App_Code: Next all the files in the App_Code folder will be compiled. All code assemblies and the profile assembly are linked to the resources and Web references assemblies if any.

Global.asax: Next the Application object is compiled and then it is linked with all other previously generated assembly's.

Once the top-level items are compiled, the compilation of folder, pages and other items start as and when needed. Here is a list of how this process continues?

App_LocalResources: If the folder containing the requested item contains an App_LocalResources folder, the contents of the local resources folder are compiled and linked to the global resources assembly.

Individual Web pages (.aspx files),
User Controls (.ascx files),
HTTP handlers (.ashx files),

HTTP modules (.asmx files): Next these files are compiled.

Themes, master pages, other source files are compiled when referenced page is compiled.

Monday, June 8, 2009

What's the difference between encapsulation and abstraction?

Encapsulation protects abstractions. Encapsulation is the bodyguard; abstraction is the VIP.

Encapsulation provides the explicit boundary between an object's abstract interface (its abstraction) and its internal implementation details. Encapsulation puts the implementation details "in a capsule." Encapsulation tells users which features are stable, permanent services of the object and which features are implementation details that are subject to change without notice.

Encapsulation helps the developers of an abstraction: it provides the freedom to implement the abstraction in any way consistent with the interface. (Encapsulation tells developers exactly what users can and cannot access.) Encapsulation also helps the users of an abstraction: it provides protection by preventing dependence on volatile implementation details.

Abstraction provides business value; encapsulation "protects" these abstractions. If a developer provides a good abstraction, users won't be tempted to peek at the object's internal mechanisms. Encapsulation is simply a safety feature that reminds users not to stray over the line inadvertently.

What is an abstraction ?

An abstraction is a simplified view of an object in the user's own vocabulary.

In OO ,an abstraction is the simplest interface to an object that provides all the features and services the intended users expect.

An abstraction tells users everything they need to know about an object but nothing else. It is the well-defined, unambiguously specified interface. For example, on a vending machine, the abstraction is formed by the buttons and their meanings; users don't have to know about levers, internal counters, or other parts that are needed for the machine to operate. Furthermore the vending machine's price list implies a legally binding promise to users: if users put in the right amount of money, the machine promises to dispense the desired item.

The key to a good abstraction is deep knowledge of the problem domain. A good abstraction allows users to use an object in a relatively safe and predictable manner. It reduces the learning curve by providing a simple interface described in terms of the user's own vocabulary.

A good abstraction separates specification from implementation. It doesn't expose too much nor does it hide features that users need to know about. If an abstraction is good, users aren't tempted to peek at the object's implementation. The net result is simpler, more stable user code.

Sunday, June 7, 2009

How to use RunWithElevatedPrivileges in Sharepoint

The SPSecurity class provides a static method named RunWithElevatedPrivileges that enables code to execute as system code running under the identity of SHAREPOINT\system. This allows code to run in an escalated security context to perform actions as the system.

This method should be used with care and should not expose direct access to system resources, but rather should be used when you need to perform actions on behalf of the system. The method is simple. You can either create a delegate to a public void method or simply write code within an inline delegate.

The signature looks like the following:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
// Code runs as the "SharePoint\system" user
});


Note that :
Code running with the escalated privilege should use a new SPSite object for code running as the system and use the SPContext.Current property to access the actual calling user’s identity.

To modify WSS content under the System credentials, you need to create a new SPSite site collection that generates a new security context for objects referenced from the site, as in the following example. You cannot switch the security context of the SPSite once it has been created, but must instead create a new SPSite reference to switch user contexts. The following code uses the system credentials to add a list item using the profile data of the current Web user:

SPSecurity.RunWithElevatedPrivileges(
delegate() {
using (SPSite site = new SPSite(web.Site.ID)) {
using (SPWeb web2 = site.OpenWeb()) {
SPList theList = web2.Lists["visitors"];
SPListItem record = theList.Items.Add();
record["User"] = SPContext.Current.Web.CurrentUser;
record.Update();
}
}
);

How to add a SPGroup to a Site Collection- Sharepoint

Groups cannot directly be added to a site-they must be added to the site collection.
If you try to add a group to the site’s Groups collection, you get an exception stating, “You cannot add a group directly to the Groups collection. You can add a group to the SiteGroups collection.”

This situation occurs because the SPGroup is always created at the Site Collection level and assigned to the site. The following code is valid and adds the LitwareSecurityGroup to the site collection groups.

// Adds a new group to the site collection groups:
site.SiteGroups.Add("LitwareSecurityGroup", site.CurrentUser,site.CurrentUser, "A group to manage Litware Security");

However, this still does not associate the group with our site, nor would it be useful within the site without any permissions. To add the group to the site, create a new SPRoleAssignment by associating an SPRoleDefinition with the SPGroup, and then add that role assignment to the site, as in the following code sample:


SPGroup secGroup = site.SiteGroups["LitwareSecurityGroup"];
SPRoleAssignment roleAssignment = new SPRoleAssignment(secGroup);
SPRoleDefinition roleDefinition = site.RoleDefinitions["Full Control"];
roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
site.RoleAssignments.Add(roleAssignment);

How to get list of SPGroups in Sharepoint Site Collection

Sharepoint Gropus (or SPGroups) are never created in the context of the site-they are always created in the context of the site collection and assigned to a site.

We can get list of groups of a site by suing site.Groups


SPSite siteCollection = new SPSite("http://localhost/litware/sales/");
SPWeb site = siteCollection.OpenWeb();

foreach(SPGroup group in site.Groups){
Console.WriteLine(group.Name);
}