ASP.net Optimization

  • Upload
    gautham

  • View
    218

  • Download
    0

Embed Size (px)

Citation preview

  • 8/10/2019 ASP.net Optimization

    1/8

    Coding Optimization

    Use Data Set in Following Situation

    Navigate between multiple discrete tables of results.

    Manipulate data from multiple sources (for example, a mixture of data from more than onedatabase, from an XML file, and from a spreadsheet).

    Exchange data between tiers or using an XML eb service. !nli"e the DataReader, the

    DataSetcan be passed to a remote client.

    #euse the same set of rows to achieve a performance gain b$ caching them (such as for

    sorting, searching, or filtering the data).

    %erform a large amount of processing per row. Extended processing on each row returned

    using a DataReaderties up the connection serving the DataReaderlonger than necessar$,impacting performance.

    Manipulate data using XML operations such as Extensible &t$lesheet Language

    'ransformations (X&L' transformations) or X%ath ueries.

    Use the DataReader in your application if you:

    o not need to cache the data.

    *re processing a set of results too large to fit into memor$.

    Need to uic"l$ access data once, in a forward+onl$ and read+onl$ manner.

    Use Datatable In he Following Situation:

    'he ata'able is a central obect in the *-.NE' librar$. -ther obects that use theata'able include the ata&et and the ataiew.

    /f $ou are creating a ata'able programmaticall$, $ou must first define its schema b$ addingata0olumn obects to the ata0olumn0ollection 'o add rows to a ata'able, $ou must first usethe New#ow method to return a new ata#ow obect. 'he New#ow method returns a row withthe schema of the ata'able, as it is defined b$ the table1s ata0olumn0ollection.

    'he schema of a table is defined b$ the ata0olumn0ollection, the collection ofata0olumn obects. 'he ata0olumn0ollection is accessed through the 0olumns propert$.

    'he ata'able contains a collection of 0onstraint obects that can be used to ensure theintegrit$ of the data..

    'o determine when changes are made to a table, use one of the following events2

    #ow0hanged,#ow0hanging,#oweleting, and #oweleted.

    'o #etrieve records from one 'able then no need to select ata&et instead we canselect ata'able.

  • 8/10/2019 ASP.net Optimization

    2/8

  • 8/10/2019 ASP.net Optimization

    3/8

  • 8/10/2019 ASP.net Optimization

    4/8

    Aasp2 datagrid Enableiew&tate?;false; datasource?;...; runat?;server;C=or %ageAFG %age Enableiew&tate?;false; FC

    8.Use Caching to impro/e the performance of your application+ -utput0aching enables $our page to be cached for specific duration and

    can be made invalid based on various parameters that can be specified. 'he0ache exists for the duration $ou specif$ and until that time, the reuests do notgo to the server and are served from the 0ache.

    o not assign cached items a short expiration. /tems that expire uic"l$cause unnecessar$ turnover in the cache and freuentl$ cause more wor" forcleanup code and the garbage collector. /n case $ou have static as well asd$namic sections of $our page, tr$ to use %artial 0aching (=ragment 0aching) b$brea"ing up $our page into user controls and specif$ 0aching for onl$ those0ontrols which are more+or+less static.>.&lways use the String builder to concatenate string

    The memory representation of string is an array of characters, So on re-assigningthe new array of Char is formed & the start address is changed. Thus keeping the old

    string in memory for garbage collector to be disposed. Hence application is slow down.Always use the string builder for concatenating string.

    1est )ractice For #andling $2ception:

    $2ception #andling

    3. Never do a 1catch exception and do nothing1. /f $ou hide an exception, $ou will never "now if

    the exception happened or not.5. /n case of exceptions, give a friendl$ message to the user, but log the actual error with all

    possible details about the error, including the time it occurred, method and class name etc.6. *lwa$s catch onl$ the specific exception, not generic exception.

    4ood2void #ead=rom=ile ( string fileName )H

    tr$I H

    read from file.Jcatch (=ile/-Exception ex)H

    log error. re+throw exception depending on $our case.throwK

    JJ

    Not 4ood2void #ead=rom=ile ( string fileName )H

    tr$

  • 8/10/2019 ASP.net Optimization

    5/8

    H read from file.

    Jcatch (Exception ex)H

    0atching general exception is bad... we will never "now whether it was a file error or some other error.

    9ere $ou are hiding an exception. /n this case no one will ever "now that an exception happened.

    return ;;KJ

    J 7. No need to catch the general exception in all $our methods. Leave it open and let theapplication crash. 'his will help $ou find most of the errors during development c$cle.

    8. Dou can have an application level (thread level) error handler where $ou can handle allgeneral exceptions. /n case of an 1unexpected general error1, this error handler should catch theexception and should log the error in addition to giving a friendl$ message to the user beforeclosing the application, or allowing the user to 1ignore and proceed1.

    >. o not write tr$+catch in all $our methods. !se it onl$ if there is a possibilit$ that a a specificexception ma$ occur. =or example, if $ou are writing into a file, handle onl$ =ile/-Exception.I. o not write ver$ large tr$+catch bloc"s. /f reuired, write separate tr$+catch for each tas" $ouperform and enclose onl$ the specific piece of code inside the tr$+catch. 'his will help $ou findwhich piece of code generated the exception and $ou can give specific error message to the user.. Dou ma$ write $our own custom exception classes, if reuired in $our application. o notderive $our custom exceptions from the base class &$stemException. /nstead, inherit from

    *pplicationException.. !se Exception, if the Event is reall$ Exceptional and it gives error(Li"e !nExcepted End of=ile), because catching the error programmaticall$ ta"es less time than exception.3. *lwa$s order exceptions in catch bloc"s from the most specific to the least specific. 'histechniue handles the specific exception before it is passed to a more general catch bloc".33. /nclude a locali@ed description string in ever$ exception. hen the user sees an error

    message, it is derived from the description string of the exception that was thrown, rather thanfrom the exception class.35. !se grammaticall$ correct error messages, including ending punctuation. Each sentencein a description string of an exception should end in a period.36. %rovide $2ceptionproperties for programmatic access. /nclude extra information in anexception (in addition to the description string) onl$ when there is a programmatic scenariowhere the additional information is useful.

    Concatenation of String

    0ancatenation of string should not be done li"e this

    &tring s ? ;abc;K

    sO? ;def;K

    sO?;ghi;

    instead

    &tring

  • 8/10/2019 ASP.net Optimization

    6/8

    %ote2 'he &tring

  • 8/10/2019 ASP.net Optimization

    7/8

    ariables, controls, and procedures should be named clearl$ enough that inline

    commenting is onl$ needed for complex implementation details.

    Note: This structure should be implemented in all functional pagesAFG Language?Pava&criptFC

    QRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRName 2 Name of the *&%XQescription 2 * brief description of the pageSs functionalit$ Qeveloper 2 Name of the developer QList of pages this aspx navigates to2 page3, page5 Q0reation ate 2 ate on which the page was created QLast revised 2 ate on which the page was last revised Q#evision histor$ QModified b$ ate Modified #eason for modification Qxxxxx xxxxxxxx xxxxx Q$$$$ $$$$$$$$ $$$$$$$$ QRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR

    eclare variablesint n0ounterstring str=Name

    File Comments

    *t the top of $our .cs file please have a description of $our cs file2

    Asummar$C33File %ame 2 *pplication Name33Description 2 'he main purpose of this file is to.................33De/eloper 2 eveloper Name33Creation Date 2 0reation ateAsummar$CA#E/&/-NT9/&'-#DCA&No value?UUC Modified

  • 8/10/2019 ASP.net Optimization

    8/8

    Aparam name ? ; ; t$pe ? ;/nput;C0onnection &tring of the format AparamCAparam name ? ; ; t$pe ? ;-utput;C&tring containing the #ecord /dAparamCAreturnsC'he result as a booleanAreturnsC

    A#E/&/-NT9/&'-#DCA&No value?UUC Modified