di .NET e di altre amenità

VS2005: Uno snippet per validare i parametri

Forse non sono in molti che gestiscono la validazione dei parametri di input di un metodo, tuttavia considerato che sarebbe una buona cosa farlo, vi propongo uno snippet che mi sono creato che dovrebbe semplificare questa ripetitiva attività. Lo snippet, che nell'esempio è dedicato al caso delle stringhe, compila per noi una sezione di validazione e ci consente di immettere il nome del parametro e il messaggio che vogliamo dare nell'eccezione.

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet 
Format="1.0.0">
        <Header>
            <
Title>param_str</Title>
            <Shortcut>
param_str</Shortcut>
            <Description>
Code snippet for string parameter validation</Description>
            <Author>
Andrea Boschin</Author>
            <SnippetTypes>
                <SnippetType>
Expansion</SnippetType>
            <
/SnippetTypes>
        <
/Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <
ID>argument</ID>
                    <ToolTip>
Argument name</ToolTip>
                    <Default>
name</Default>
                <
/Literal>
                <Literal>
                    <
ID>messageNull</ID>
                    <ToolTip>
Exception Message for null string</ToolTip>
                    <Default>
Argument cannot be null</Default>
                <
/Literal>
                <Literal>
                    <
ID>messageEmpty</ID>
                    <ToolTip>
Exception Message for empty string</ToolTip>
                    <Default>
Argument cannot be empty</Default>
                <
/Literal>
            <
/Declarations>
            <
Code Language="csharp">
                <![CDATA[if 
($argument$ == null)
                throw new ArgumentNullException("$argument$", "$messageNull$");
                if 
($argument$ == string.Empty)
                throw new ArgumentException("$argument$", "$messageEmpty$");$end$]]>
            <
/Code>
        <
/Snippet>
    <
/CodeSnippet>
<
/CodeSnippets>

Alla vostra fantasia tutte le possibili varianti.

powered by IMHO 1.3

 

Commenti

2006-03-20T08.38.00+01.00 #

Di .NET e di altre amenita'

Aggiungi commento




  Country flag

biuquote
  • Commento
  • Anteprima
Loading