RSS
 

Archive for July, 2010

C# 3.0 New Features

28 Jul

In this post, i will write about all the new features in C# 3.0, this features will help you more and more in your programming life, and class design.

this list of C# features which will be covered here,

1. Extension method

2. Lambda expression

1. Extension Method

  • extension method is new feature in C# 3.0, it is like static methods which we know in our object oriented background but with more main differences:
    • Extension methods must written in static class not like static method which can written in any class
    • To specify extension method, you need to write this keyword before the first argument in the method, static methods don’t have this keyword at declaration
    • at consumed, the argument with this keyword isn’t passed to the function
    • extension methods can be called only on instances, calling them on class will result in compilation errors, the class instances on which they are called are determined by the first argument in the declaration in other words the argument has this keyword
    • Note that extension methods can’t access private methods of extended type
    • Note that if you don’t have source code of type, and you don’t add new methods to type
    • Note that if you create extension methods that have the same signature methods inside the type you are extending, then the extension methods will never called.
    • extension methods can’t be used to override existing methods
    • concept of extension methods can’t be applied to fields, properties or events
    • if the class is sealed than there in no concept of extending its functionality, for this a new concept is introduced

But why you determine that you want to implement this new feature, let’s assume the following scenario you need to implement new method in Int class to get the power of the integer, as the traditional thinking you will design class which inheriting from the parent class Int and implement new method called Power but with C# 3.0 you have another option implement extension method  Power() on Int class, let’s try it with your hand

 
No Comments

Posted in C#

 

Be Higher Productive with Visual Studio 2010

22 Jul

use Extension Manager, Search in the Online Gallary for Power Productive tools and check the difference

 

Creating Map File for DataContext in LINQ to SQL

12 Jul

Creating a mapping document manually is a time-consuming, error-prone activity. Visual Studio includes a command-line tool named SQLMetal.exe that can be used to generate the mapping file based on a database schema.

 
 

Simple Look @ Transaction in ADO.Net

08 Jul

   // C#   
             DbTransaction tx;
             try 
             {
                 conn.Open();
                 tx = conn.BeginTransaction();
                 cmdDeleteOrders.ExecuteNonQuery();
                 cmdDeleteCustomer.ExecuteNonQuery();
                 tx.Commit();
                 conn.Close();
             }
             catch (Exception exception)
             {
                 if (tx != null)
                 {
                     tx.Rollback();
                 }
                 if (conn != null)
                 {
                     if (conn.State != ConnectionState.Closed)
                     {
                         conn.Close();
                     }
                 }
             }

The exception handling can be simplified if the previous code is refactored as shown in this next code. The using statement calls Dispose() on the transaction object. The Dispose method checks to see if there are any uncommitted changes. If there are, it rolls back the transaction

  
              // C#   
             conn.Open();
             using (DbTransaction tx = conn.BeginTransaction())
             {
                 cmdDeleteOrders.ExecuteNonQuery();
                 cmdDeleteCustomer.ExecuteNonQuery();
                 tx.Commit();
                 conn.Close();
             }

 
 

Multiple Active Result Set (MARS) @ADO.Net

05 Jul

While my certification study i want to share this information with any one, On a standard connection—that is, one that doesn’t use MARS—it is possible to have only a single open result set at any moment during the lifetime of the connection. This means that should multiple operations be required, the connection must be closed and reopened each time, which can be an expensive process. (ADO.Net MCTS EBook)

but you need to do this by To use MARS, a simple modification to the connection string is required, namely, another name-value pair called MultipleActiveResultSets=true. Look at what happens with this setting enabled

 
 

Deep Look @ConnectionString.. Connection Pooling

05 Jul

In terms of processing and Inter-Process Communication (IPC), it is quite expensive to create a connection to a data source. You can see this if you step through any code that creates an initial data source connection—there is a significant delay while the network channel is opened, handshaking with the server occurs, a connection to the SQL Server is made, and user credentials are authenticated. This process is true even with the most simple connection—more complex connections that include transactions or the attaching of database files take even longer.

Inside an application, it would be unusual to find that a different type of connection or data source location is required for each database operation. Perhaps, then, it would make sense if, instead of the connection being destroyed after each operation, it could be reused to save the expense of opening a new connection? Connection pooling performs this connection optimization.

If the connection string values match between connection requests and an existing connection exists in the connection pool, ADO.NET reuses the existing connection instead of creating a new connection. Different connection pools are created based upon differing connection string values, transactions, and Integrated Security identity. It is good practice— and strongly recommended—that you close the Connection object after each operation so it can be returned to the connection pool.

The good news is that connection pooling is enabled by default and in most cases does not require any configuration to suit your requirements

A persistent connection is one which, once opened, remains available for use until it is closed explicitly after performing all required database operations. To do this, connection pooling must be disabled. This is configured by setting Pooling=false in the connection string

From: ADO.Net MCTS Certification book

 

Encrypt Connection String @Web.config in ASP.Net

05 Jul
  1. using RSA public key encryption. This type of encryption allows the export and import of keys which is useful when the solution is deployed in distributed fashion such as web farm

i implement this method to enable you to encrypt connection string section

         public  void  EncryptConnectionString()
         {
             Configuration  configuration = WebConfigurationManager .OpenWebConfiguration("~" );
             ConfigurationSection  configurationSection = configuration.GetSection("connectionstrings" );
             if (configurationSection != null  && !configurationSection.SectionInformation.IsLocked && !configurationSection.SectionInformation.IsProtected
                 && !configurationSection.IsReadOnly())
             {
                 configurationSection.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider" );
                 configurationSection.SectionInformation.ForceSave = true ;
                 configuration.Save(ConfigurationSaveMode .Full);
             }
             else 
             {
                 if  (configurationSection != null )
                 {
                     configurationSection.SectionInformation.UnprotectSection();
                     configurationSection.SectionInformation.ForceSave = true ;
                 }
                 configuration.Save(ConfigurationSaveMode .Full);
             }
         }
Encryption and decryption is performed using the Windows Data Protection API  (DPAPI). Because
DPAPI does not allow the exporting of keys, this provider  should be used in situations where
the deployed solution is limited to a single  server.

if you want to encrypt any section of configuration file in non- programmatic way. Doing this
means that the configuration section can be encrypted before use and  then, as suggested
earlier, it is decrypted transparently at run time only for  use by the server. This can all
be achieved at the command prompt using the  Aspnet_regiis utility.

Keep, this in your mind ...> It should be noted that the encryption of configuration sections
can cause  problems. If the section is encrypted using DataProtectionConfigurationProvider and
the config file is  moved to a different machine, this new location cannot decrypt the value. With  RSAProtectedConfigurationProvider, however, the key can be  exported to a different machine along with the configuration file; this means  decryption can take place using the same exported key.

 
No Comments

Posted in ASP.Net

 

Model View ViewModel Pattern @Session 2

01 Jul

http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

http://martinfowler.com/eaaDev/PresentationModel.html

http://blogs.msdn.com/b/johngossman/archive/2005/10/08/478683.aspx

http://mvvmfoundation.codeplex.com/

http://www.galasoft.ch/mvvm/getstarted/

http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

http://en.wikipedia.org/wiki/Model_View_ViewModel

http://weblogs.asp.net/craigshoemaker/archive/2009/02/26/hands-on-model-view-viewmodel-mvvm-for-silverlight-and-wpf.aspx

http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx

http://blogs.msdn.com/b/dancre/archive/2006/10/11/datamodel-view-viewmodel-pattern-series.aspx

 
 

Windows 7 Drivers On Windows Server 2008 R2

01 Jul

http://johndball.blaize.net/2010/02/26/vostro-15xx-on-windows-7-64-bit-drivers/

 
 
 

You need to log in to vote

The blog owner requires users to be logged in to be able to vote for this post.

Alternatively, if you do not have an account yet you can create one here.

Powered by Vote It Up