<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blog de Oxxigeno &#187; .NET</title>
	<atom:link href="http://www.oxxigeno.com/blog/categoria/tecnologias/net-tecnologias/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.oxxigeno.com/blog</link>
	<description>Blog corporativo de Oxxigeno</description>
	<lastBuildDate>Tue, 23 Feb 2010 10:11:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Buenas Prácticas en .NET: Uso estructurado del fichero de configuración</title>
		<link>http://www.oxxigeno.com/blog/2009/06/buenas-practicas-en-net-uso-estructurado-del-fichero-de-configuracion/</link>
		<comments>http://www.oxxigeno.com/blog/2009/06/buenas-practicas-en-net-uso-estructurado-del-fichero-de-configuracion/#comments</comments>
		<pubDate>Mon, 01 Jun 2009 17:08:02 +0000</pubDate>
		<dc:creator>ivinuales</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Desarrollo]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[app.config]]></category>
		<category><![CDATA[ascx]]></category>
		<category><![CDATA[aspx]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[web.config]]></category>

		<guid isPermaLink="false">http://www.oxxigeno.com/blog/?p=467</guid>
		<description><![CDATA[Una práctica habitual en desarrollos asp net es utilizar el fichero de configuración (o el app config en aplicativos de escritorio) como almacén de valores configurables que luego se utilizarán en el aplicativo para uno u otro propósito.
Lo que en principio se nos antoja como una buena práctica, en la mayoría de las ocasiones, debido [...]]]></description>
			<content:encoded><![CDATA[<p>Una práctica habitual en desarrollos asp net es utilizar el fichero de configuración (o el app config en aplicativos de escritorio) como almacén de valores configurables que luego se utilizarán en el aplicativo para uno u otro propósito.</p>
<p>Lo que en principio se nos antoja como una buena práctica, en la mayoría de las ocasiones, debido a una utilización mal estructurada del fichero de configuración, se convierte en una lista incontrolable y sin organización alguna de entradas en el nodo <em>appSettings</em>.</p>
<p>Para evitar esto existe una solución alternativa, más elegante, más clara y, más optima. Las clases <em>ConfigurationSection</em>, <em>ConfigurationElement </em>y <em>ConfigurationElementCollection </em>permiten <strong>definir secciones, elementos y colecciones de elementos en el fichero de configuración</strong> y utilizarlas como cualquier otro objeto en nuestro aplicativo.</p>
<p>Vamos a definir una pequeña sección en el fichero de configuración y comentamos un poquito cómo realizarla.</p>
<p><span id="more-467"></span><br />
La clase <em>EjemploData </em>heredará de <em>ConfigurationElement </em>y tendrá dos propiedades <em>Name </em>y <em>PropiedadBooleana</em>, de tipo string y bool respectivamente. Las propiedades de ambas deberán tener el atributo <em>ConfigurationProperty </em>con el nombre que tomará en el fichero de configuración, su valor por defecto (si queremos especificarlo) y si es obligatoria.</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> EjemploData <span style="color: #008000;">:</span> ConfigurationElement
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080;">#region Public Properties</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">string</span> NamePropertyName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;name&quot;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>ConfigurationProperty<span style="color: #000000;">&#40;</span>NamePropertyName, IsRequired <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Name
        <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">this</span><span style="color: #000000;">&#91;</span>NamePropertyName<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
            set <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">this</span><span style="color: #000000;">&#91;</span>NamePropertyName<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">string</span> PropiedadBooleanaPropertyName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;propiedadBooleana&quot;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#91;</span>ConfigurationProperty<span style="color: #000000;">&#40;</span>PropiedadBooleanaPropertyName, DefaultValue <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;true&quot;</span>, IsRequired <span style="color: #008000;">=</span> <span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> PropiedadBooleana
        <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">bool</span><span style="color: #000000;">&#41;</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#91;</span>PropiedadBooleanaPropertyName<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
            set <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">this</span><span style="color: #000000;">&#91;</span>PropiedadBooleanaPropertyName<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080;">#endregion</span>
    <span style="color: #000000;">&#125;</span></pre></div></div>

</blockquote>
<p>Crearemos una clase <em>EjemploDataCollection </em>que herede de <em>ConfigurationElementCollection</em>, tal y como se muestra, no requiere mucha explicación</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> EjemploDataCollection <span style="color: #008000;">:</span> ConfigurationElementCollection
<span style="color: #000000;">&#123;</span>
<span style="color: #008080;">#region ConfigurationElementCollection Members</span>
&nbsp;
<span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> ConfigurationElement CreateNewElement<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
<span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> EjemploData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">object</span> GetElementKey<span style="color: #000000;">&#40;</span>ConfigurationElement element<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
<span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>EjemploData<span style="color: #000000;">&#41;</span>element<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Name</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> EjemploData <span style="color: #0600FF;">this</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">int</span> index<span style="color: #000000;">&#93;</span>
<span style="color: #000000;">&#123;</span>
get
<span style="color: #000000;">&#123;</span>
<span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>EjemploData<span style="color: #000000;">&#41;</span>BaseGet<span style="color: #000000;">&#40;</span>index<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
set
<span style="color: #000000;">&#123;</span>
<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>BaseGet<span style="color: #000000;">&#40;</span>index<span style="color: #000000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
BaseRemoveAt<span style="color: #000000;">&#40;</span>index<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
BaseAdd<span style="color: #000000;">&#40;</span>index, value<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008080;">#endregion</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</blockquote>
<p>Finalmente, crearemos la clase que nos permita manejar este tipo de objetos. Esta clase debe heredar de <em>ConfigurationSection</em>, la cual nos servirá para marcar el ámbito en el cual obtendremos los valores dentro del fichero de configuración. Debe tener:</p>
<ul>
<li>Una constante pública que nos indique cómo llegar a la sección adecuada del fichero de configuración.</li>
<li>Constantes privadas que nos marquen cómo se llamarán los atributos.</li>
<li>Nodos hijos que podremos definir en el fichero de configuración</li>
<li>Sus respectivas propiedades públicas que nos permitirán utilizarlas en cualquier parte de nuestro aplicativo.</li>
</ul>
<p>En nuestro ejemplo definiremos la seccion como “<em>oxxigeno/ejemploSettings</em>”, lo que significa que en el fichero de configuración tendremos que definir un nodo <em>oxxigeno</em> y dentro uno <em>ejemploSettings</em>.</p>
<p>Queremos que exista una atributo en el nodo <em>ejemploSettings</em>, para lo cual crearemos una constante privada que nos diga como debe llamarse, “<em>nuevaPropiedad</em>”, y una propiedad pública que nos permita recuperarlo del fichero de configuración y utilizarlo en el aplicativo. Para ello crearemos una propiedad normal que nos devolverá un objeto de tipo string y le establecemos un atributo a la propiedad de tipo <em>ConfigurationProperty</em><strong>, </strong>que indique cómo se va a llamar ese atributo en el fichero de configuración y si es obligatorio o no que exista.</p>
<p>Además nos interesa que bajo el nodo <em>ejemploSettings </em>exista una colección de nodos del mismo tipo (en nuestro caso <em>EjemploData</em>). Para ello procederemos igual que para la propiedad actual, pero definiremos su salida como <em>EjemploDataCollection</em><strong>, </strong>y le agregaremos el atributo <em>ConfigurationCollection</em><strong> </strong>pasándole como parámetro el tipo de objetos que se coleccionarán.</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">sealed</span> <span style="color: #FF0000;">class</span> EjemploSettings <span style="color: #008000;">:</span> ConfigurationSection
<span style="color: #000000;">&#123;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">string</span> SectionName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;oxxigeno/ejemploSettings&quot;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080;">#region Public Properties</span>
&nbsp;
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">string</span> NuevaPropiedadPropertyName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;nuevaPropiedad&quot;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #000000;">&#91;</span>ConfigurationProperty<span style="color: #000000;">&#40;</span>NuevaPropiedadPropertyName, IsRequired <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> NuevaPropiedad
<span style="color: #000000;">&#123;</span>
get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">this</span><span style="color: #000000;">&#91;</span>NuevaPropiedadPropertyName<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
set <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">this</span><span style="color: #000000;">&#91;</span>NuevaPropiedadPropertyName<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">string</span> ColeccionDeEjemploPropertyName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;coleccionDeEjemplo&quot;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #000000;">&#91;</span>ConfigurationProperty<span style="color: #000000;">&#40;</span>ColeccionDeEjemploPropertyName, IsRequired <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span>, IsDefaultCollection <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #000000;">&#91;</span>ConfigurationCollection<span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>EjemploData<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> EjemploDataCollection ColeccionDeEjemplo
<span style="color: #000000;">&#123;</span>
get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>EjemploDataCollection<span style="color: #000000;">&#41;</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#91;</span>ColeccionDeEjemploPropertyName<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008080;">#endregion</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</blockquote>
<p>Así pues, para cargar datos en el fichero de configuración, debemos cargar la clase controladora (<em>EjemploSettings</em>) y montar el árbol de nodos que hemos definido en las clases.</p>
<p>Dentro del nodo <em>configurationSections </em>debemos crear un nodo <em>SectionGroup </em>como se muestra, y dentro un nodo <em>section</em>. Pueden existir múltiples nodos <em>sectionGroup </em>pero no con el mismo nombre. Cada nodo <em>sectionGroup </em>puede tener en su interior multiples nodos <em>section</em>.</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sectionGroup</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;oxxigeno&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;section</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;ejemploSettings&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;Oxxigeno.Ejemplo.Configuration.EjemploSettings, Oxxigeno.Ejemplo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=96df930e30970c00&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/sectionGroup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

</blockquote>
<p>Y despues creamos el árbol de nodos tal como lo hemos definido, quedaría algo así:</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;oxxigeno<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ejemploSettings</span> <span style="color: #000066;">nuevaPropiedad</span>=<span style="color: #ff0000;">&quot;valorDeLaPropiedad&quot;</span>&amp;gt; </span>
<span style="color: #009900;">         <span style="color: #000000; font-weight: bold;">&lt;coleccionDeEjemplo</span>&amp;gt; <span style="color: #000000; font-weight: bold;">&lt;clear</span>/&amp;gt;</span>
<span style="color: #009900;">            <span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;nombreDeLaEntrada1&quot;</span> <span style="color: #000066;">propiedadBooleana</span>=<span style="color: #ff0000;">&quot;false&quot;</span> /&amp;gt; </span>
<span style="color: #009900;">            <span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;nombreDeLaEntrada2&quot;</span> <span style="color: #000066;">propiedadBooleana</span>=<span style="color: #ff0000;">&quot;true&quot;</span> /&amp;gt; </span>
<span style="color: #009900;">         <span style="color: #000000; font-weight: bold;">&lt;/coleccionDeEjemplo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ejemploSettings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/oxxigeno</span>&amp;gt;</span></pre></div></div>

</blockquote>
<p>La forma de recuperar estos valores sería la siguiente:</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">EjemploSettings ejemploSettings <span style="color: #008000;">=</span> ConfigurationManager.<span style="color: #0000FF;">GetSection</span><span style="color: #000000;">&#40;</span>EjemploSettings.<span style="color: #0000FF;">SectionName</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">as</span> EjemploSettings<span style="color: #008000;">;</span>
<span style="color: #FF0000;">string</span> nuevaPropiedad <span style="color: #008000;">=</span> ejemploSettings.<span style="color: #0000FF;">NuevaPropiedad</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>EjemploData obj <span style="color: #0600FF;">in</span> ejemploSettings.<span style="color: #0000FF;">ColeccionDeEjemplo</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
<span style="color: #FF0000;">string</span> nombre <span style="color: #008000;">=</span> obj.<span style="color: #0000FF;">Name</span><span style="color: #008000;">;</span>
<span style="color: #FF0000;">bool</span> propiedadBooleana <span style="color: #008000;">=</span> obj.<span style="color: #0000FF;">PropiedadBooleana</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</blockquote>
<p>Considero que este método de utilización del fichero de configuración es:</p>
<ul>
<li><strong>Más útil</strong>: Los valores se encuentran parseados al tipo necesario, no se obtienen unicamente valores de tipo string como pasa si utilizamos AppSettings, lo cual facilita el trabajo del desarrollador.</li>
<li><strong>Más estructurado</strong>: Los valores dentro del fichero de configuración son facilmente localizables y, por lo tanto, modificables.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.oxxigeno.com/blog/2009/06/buenas-practicas-en-net-uso-estructurado-del-fichero-de-configuracion/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Nuevas Tecnologías &#8211; Windows Comunication Foundation</title>
		<link>http://www.oxxigeno.com/blog/2009/05/nuevas-tecnologias-windows-comunication-foundation/</link>
		<comments>http://www.oxxigeno.com/blog/2009/05/nuevas-tecnologias-windows-comunication-foundation/#comments</comments>
		<pubDate>Mon, 11 May 2009 13:55:17 +0000</pubDate>
		<dc:creator>ivinuales</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Desarrollo]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[.net 3.5]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[servicios]]></category>
		<category><![CDATA[wcf]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.oxxigeno.com/blog/?p=500</guid>
		<description><![CDATA[Este nuevo modulo incluido en el .NET Framework 3.5 establece un modelo de programación unificado para la implementación de sistemas distribuidos, especialmente orientado a servicios.
 Esta tecnología integra en una única metodología de programación las hasta ahora incompatibles COM+ (Serviced Components), .NET Remoting y Servicios Web.  Para ello, un servicio WCF debe definir una [...]]]></description>
			<content:encoded><![CDATA[<p>Este nuevo modulo incluido en el .NET Framework 3.5 establece un modelo de programación unificado para la implementación de sistemas distribuidos, especialmente orientado a servicios.</p>
<p><span id="more-500"></span> Esta tecnología integra en una única metodología de programación las hasta ahora incompatibles COM+ (Serviced Components), .NET Remoting y Servicios Web.  Para ello, un servicio WCF debe definir una serie de puntos de acceso (denominados EndPoints) que representan una puerta de entrada al servicio publicado, permitiendo así que un mismo servicio sea consultado mediante distintos tipos de comunicación.<br />
Los EndPoints deben presentar:</p>
<ul>
<li><strong>Dirección</strong>: representada por la clase EndPointAddress, indica dónde está la aplicación</li>
<li><strong>Enlace</strong>: representada por la clase Binding, indica cómo se puede accesar esa aplicación: Protocolo del transporte, Codificación y Seguridad.</li>
<li><strong>Contrato</strong>: representada por una clase con atributos de tipo <strong>ServiceContractAttribute</strong>, se trata de una definición de los datos que van a ser comunicados por el aplicativo.</li>
</ul>
<p>WCF utiliza mensajes SOAP para la comunicación de estos datos, siguiendo así un estándar de comunicación. Sin embargo, cuando se realiza una comunicación entre dos procesos WCF se optimiza esta comunicación, aplicando una codificación en formato binario para los mensajes SOAP. De esto podemos olvidarnos totalmente, ya que al fin podemos crear un servicio sin preocuparnos de cómo ni quién va a acceder a él.</p>
<p>Vayamos con un ejemplo y veamos si es realmente lo que nos venden. Creamos una interfaz y una entidad que serán las que utilizaremos para dar vida al servicio.</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>ServiceContract<span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">interface</span> IExample
    <span style="color: #000000;">&#123;</span>
        <span style="color: #000000;">&#91;</span>OperationContract<span style="color: #000000;">&#93;</span>
        <span style="color: #FF0000;">string</span> GetData<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> value<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>OperationContract<span style="color: #000000;">&#93;</span>
        ExampleType GetDataUsingDataContract<span style="color: #000000;">&#40;</span>ExampleType composite<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>OperationContract<span style="color: #000000;">&#93;</span>
        <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> GetNames<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #000000;">&#91;</span>DataContract<span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ExampleType
    <span style="color: #000000;">&#123;</span>
        <span style="color: #FF0000;">bool</span> _delete <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span>
        <span style="color: #FF0000;">string</span> _name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Name&quot;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>DataMember<span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> Delete
        <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _delete<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
            set <span style="color: #000000;">&#123;</span> _delete <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>DataMember<span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Name
        <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _name<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
            set <span style="color: #000000;">&#123;</span> _name <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span></pre></div></div>

</blockquote>
<p>Los atributos DataContract y OperationContract son, para entendernos, los atributos que sustituyen a WebService y WebMethod. Mientras que los atributos DataContract y DataMember son los atributos que servirán para serializar las entidades que se publicarán a través del servicio.</p>
<p>Ahora, una vez creada la interfaz que publicará el servicio y la entidad, procedemos a implementar la funcionalidad del servicio.</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ExampleService <span style="color: #008000;">:</span> IExample
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> GetData<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> value<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;You entered: {0}&quot;</span>, value<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> ExampleType GetDataUsingDataContract<span style="color: #000000;">&#40;</span>ExampleType composite<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>composite.<span style="color: #0000FF;">Delete</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                composite.<span style="color: #0000FF;">Name</span> <span style="color: #008000;">+=</span> <span style="color: #666666;">&quot; y Apellido&quot;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">return</span> composite<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> GetNames<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span> lista <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">5</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                lista.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;nombre&quot;</span> <span style="color: #008000;">+</span> i.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">return</span> lista.<span style="color: #0000FF;">ToArray</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span></pre></div></div>

</blockquote>
<p>Con esto ya tenemos el código necesario para crear un servicio con WCF. Nos falta darle posibles puntos de entrada, para poder publicarlo. Lo hacemos a través del web.config del servicio.</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;system.serviceModel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;services<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;service</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Namespace.ExampleService&quot;</span> <span style="color: #000066;">behaviorConfiguration</span>=<span style="color: #ff0000;">&quot;Namespace.ExampleServiceBehavior&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #808080; font-style: italic;">&lt;!-- Service Endpoints --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;endpoint</span> <span style="color: #000066;">address</span>=<span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000066;">binding</span>=<span style="color: #ff0000;">&quot;wsHttpBinding&quot;</span> <span style="color: #000066;">contract</span>=<span style="color: #ff0000;">&quot;Namespace.IExample&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;identity<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dns</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;localhost&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/identity<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/endpoint<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;endpoint</span> <span style="color: #000066;">address</span>=<span style="color: #ff0000;">&quot;mex&quot;</span> <span style="color: #000066;">binding</span>=<span style="color: #ff0000;">&quot;mexHttpBinding&quot;</span> <span style="color: #000066;">contract</span>=<span style="color: #ff0000;">&quot;IMetadataExchange&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/service<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/services<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;behaviors<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;serviceBehaviors<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;behavior</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Namespace.ExampleServiceBehavior&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;serviceMetadata</span> <span style="color: #000066;">httpGetEnabled</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;serviceDebug</span> <span style="color: #000066;">includeExceptionDetailInFaults</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/behavior<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/serviceBehaviors<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/behaviors<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/system.serviceModel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

</blockquote>
<p>Voy a intentar explicarlo de forma esquemática. Dentro de la etiqueta “services” creamos cada uno de los servicios que queramos publicar, mientras que dentro de la etiqueta “behaviors” definiremos diferentes comportamientos que podrán utilizarse en los servicios publicados.</p>
<p>Pero volvamos a la etiqueta “services”. Dentro de ella crearemos tantas etiquetas “service” como servicios queramos publicar. Cada una se define mediante nombre y comportamiento (“name” y “behaviorConfiguration”). Además debemos definir una serie de puntos de entrada (“endpoint”) con las propiedades “address”, “binding” y “contract”, y un nodo interno de tipo “identity” (como se ve en el código). Vamos con ellos:</p>
<ul>
<li>Etiqueta Service:
<ul>
<li>Address: Especificamos la URI, que puede estar como cadena vacía.</li>
<li>Binding: Especificamos el binding a utilizar, lo cual define el tipo de transporte, la seguridad y el encoding utilizado. Podemos utilizar un binding definido por nosotros.</li>
<li>Contract: Especifica la interfaz que define se publicará con el servicio.</li>
</ul>
</li>
<li>Etiqueta Behavior:
<ul>
<li>Name: Nombre del behavior, único en el web.config</li>
<li>ServiceMetadata: Especifica la publicación de los metadatos del servicio y de la información asociada.</li>
<li>ServiceDebug: Especifica las características de la información de depuración y de la ayuda para un servicio de la WCF.</li>
</ul>
</li>
</ul>
<p>Para una descripción más detallada podemos recurrir a la siguiente url de Microsoft: <a href="http://msdn.microsoft.com/en-us/library/ms733932.aspx">http://msdn.microsoft.com/en-us/library/ms733932.aspx</a></p>
<p>Respecto al cliente, su uso es tan sencillo como agregar la referencia al servicio a la solucion e invocarlo desde el código.</p>
<p>Al agregar la referencia al servicio podemos ver que el web.config ha sido modificado, añadiendo la configuracion necesaria para accesar dicho servicio. Veamos qué ha añadido:</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;system.serviceModel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bindings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;wsHttpBinding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;binding</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;WSHttpBinding_IExample&quot;</span> <span style="color: #000066;">closeTimeout</span>=<span style="color: #ff0000;">&quot;00:01:00&quot;</span></span>
<span style="color: #009900;">                    <span style="color: #000066;">openTimeout</span>=<span style="color: #ff0000;">&quot;00:01:00&quot;</span> <span style="color: #000066;">receiveTimeout</span>=<span style="color: #ff0000;">&quot;00:10:00&quot;</span> <span style="color: #000066;">sendTimeout</span>=<span style="color: #ff0000;">&quot;00:01:00&quot;</span></span>
<span style="color: #009900;">                    <span style="color: #000066;">bypassProxyOnLocal</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000066;">transactionFlow</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000066;">hostNameComparisonMode</span>=<span style="color: #ff0000;">&quot;StrongWildcard&quot;</span></span>
<span style="color: #009900;">                    <span style="color: #000066;">maxBufferPoolSize</span>=<span style="color: #ff0000;">&quot;524288&quot;</span> <span style="color: #000066;">maxReceivedMessageSize</span>=<span style="color: #ff0000;">&quot;65536&quot;</span></span>
<span style="color: #009900;">                    <span style="color: #000066;">messageEncoding</span>=<span style="color: #ff0000;">&quot;Text&quot;</span> <span style="color: #000066;">textEncoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span> <span style="color: #000066;">useDefaultWebProxy</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #009900;">                    <span style="color: #000066;">allowCookies</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;readerQuotas</span> <span style="color: #000066;">maxDepth</span>=<span style="color: #ff0000;">&quot;32&quot;</span> <span style="color: #000066;">maxStringContentLength</span>=<span style="color: #ff0000;">&quot;8192&quot;</span> <span style="color: #000066;">maxArrayLength</span>=<span style="color: #ff0000;">&quot;16384&quot;</span></span>
<span style="color: #009900;">                        <span style="color: #000066;">maxBytesPerRead</span>=<span style="color: #ff0000;">&quot;4096&quot;</span> <span style="color: #000066;">maxNameTableCharCount</span>=<span style="color: #ff0000;">&quot;16384&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;reliableSession</span> <span style="color: #000066;">ordered</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">inactivityTimeout</span>=<span style="color: #ff0000;">&quot;00:10:00&quot;</span></span>
<span style="color: #009900;">                        <span style="color: #000066;">enabled</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;security</span> <span style="color: #000066;">mode</span>=<span style="color: #ff0000;">&quot;Message&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;transport</span> <span style="color: #000066;">clientCredentialType</span>=<span style="color: #ff0000;">&quot;Windows&quot;</span> <span style="color: #000066;">proxyCredentialType</span>=<span style="color: #ff0000;">&quot;None&quot;</span></span>
<span style="color: #009900;">                            <span style="color: #000066;">realm</span>=<span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;message</span> <span style="color: #000066;">clientCredentialType</span>=<span style="color: #ff0000;">&quot;Windows&quot;</span> <span style="color: #000066;">negotiateServiceCredential</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #009900;">                            <span style="color: #000066;">algorithmSuite</span>=<span style="color: #ff0000;">&quot;Default&quot;</span> <span style="color: #000066;">establishSecurityContext</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/security<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/binding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/wsHttpBinding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bindings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;client<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;endpoint</span> <span style="color: #000066;">address</span>=<span style="color: #ff0000;">&quot;http://localhost:3153/ExampleService.svc&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">binding</span>=<span style="color: #ff0000;">&quot;wsHttpBinding&quot;</span> <span style="color: #000066;">bindingConfiguration</span>=<span style="color: #ff0000;">&quot;WSHttpBinding_IExample&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">contract</span>=<span style="color: #ff0000;">&quot;ExampleServiceReference.IExample&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;WSHttpBinding_IExample&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;identity<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dns</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;localhost&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/identity<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/endpoint<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/client<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/system.serviceModel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

</blockquote>
<p>Él solo ha creado todo el arbol de etiquetas de configuracion necesario para acceder al servicio mediante el endpoint que deseemos. Si nos fijamos en la etiqueta “endpoint” dentro de “client” podemos ver cómo ha agregado la direccion del servicio, el tipo de binding, la configuracion del mismo, la interfaz que presenta y el nombre (que es lo que utilizaremos para crear el objeto client en código):</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">ExampleClient clientDefault <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ExampleClient<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;WSHttpBinding_IExample&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> namesDefault <span style="color: #008000;">=</span> clientDefault.<span style="color: #0000FF;">GetNames</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

</blockquote>
<p>Vamos a crear otro endpoint en el web.config del servicio y vamos a refrescar la referencia en el cliente para ver cómo cambia el web.config, añadiendo el nuevo endpoint y veremos cómo referenciarlo.  Agregamos dos endpoints:</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;endpoint</span> <span style="color: #000066;">address</span>=<span style="color: #ff0000;">&quot;Soap11&quot;</span> <span style="color: #000066;">contract</span>=<span style="color: #ff0000;">&quot;Namespace.IExample&quot;</span> <span style="color: #000066;">binding</span>=<span style="color: #ff0000;">&quot;basicHttpBinding&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;endpoint</span> <span style="color: #000066;">address</span>=<span style="color: #ff0000;">&quot;Soap12&quot;</span> <span style="color: #000066;">contract</span>=<span style="color: #ff0000;">&quot;Namespace.IExample&quot;</span> <span style="color: #000066;">binding</span>=<span style="color: #ff0000;">&quot;wsHttpBinding&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

</blockquote>
<p>Y refrescamos la referencia al servicio en el cliente. Miramos el web.config y descubrimos que tenemos nuevas formas de acceder al servicio:</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;endpoint</span> <span style="color: #000066;">address</span>=<span style="color: #ff0000;">&quot;http://localhost:3153/ExampleService.svc/Soap11&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">binding</span>=<span style="color: #ff0000;">&quot;basicHttpBinding&quot;</span> <span style="color: #000066;">bindingConfiguration</span>=<span style="color: #ff0000;">&quot;BasicHttpBinding_IExample&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">contract</span>=<span style="color: #ff0000;">&quot;ExampleServiceReference.IExample&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;BasicHttpBinding_IExample&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;endpoint</span> <span style="color: #000066;">address</span>=<span style="color: #ff0000;">&quot;http://localhost:3153/ExampleService.svc/Soap12&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">binding</span>=<span style="color: #ff0000;">&quot;wsHttpBinding&quot;</span> <span style="color: #000066;">bindingConfiguration</span>=<span style="color: #ff0000;">&quot;WSHttpBinding_IExample&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">contract</span>=<span style="color: #ff0000;">&quot;ExampleServiceReference.IExample&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;WSHttpBinding_IExample1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;identity<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;userPrincipalName</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;ivinuales@oxxigeno-networks.spain&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/identity<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/endpoint<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

</blockquote>
<p>Además, nos ha creado una nueva entrada dentro de bindings:</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;basicHttpBinding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;binding</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;BasicHttpBinding_IExample&quot;</span> <span style="color: #000066;">closeTimeout</span>=<span style="color: #ff0000;">&quot;00:01:00&quot;</span></span>
<span style="color: #009900;">                    <span style="color: #000066;">openTimeout</span>=<span style="color: #ff0000;">&quot;00:01:00&quot;</span> <span style="color: #000066;">receiveTimeout</span>=<span style="color: #ff0000;">&quot;00:10:00&quot;</span> <span style="color: #000066;">sendTimeout</span>=<span style="color: #ff0000;">&quot;00:01:00&quot;</span></span>
<span style="color: #009900;">                    <span style="color: #000066;">allowCookies</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000066;">bypassProxyOnLocal</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000066;">hostNameComparisonMode</span>=<span style="color: #ff0000;">&quot;StrongWildcard&quot;</span></span>
<span style="color: #009900;">                    <span style="color: #000066;">maxBufferSize</span>=<span style="color: #ff0000;">&quot;65536&quot;</span> <span style="color: #000066;">maxBufferPoolSize</span>=<span style="color: #ff0000;">&quot;524288&quot;</span> <span style="color: #000066;">maxReceivedMessageSize</span>=<span style="color: #ff0000;">&quot;65536&quot;</span></span>
<span style="color: #009900;">                    <span style="color: #000066;">messageEncoding</span>=<span style="color: #ff0000;">&quot;Text&quot;</span> <span style="color: #000066;">textEncoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span> <span style="color: #000066;">transferMode</span>=<span style="color: #ff0000;">&quot;Buffered&quot;</span></span>
<span style="color: #009900;">                    <span style="color: #000066;">useDefaultWebProxy</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;readerQuotas</span> <span style="color: #000066;">maxDepth</span>=<span style="color: #ff0000;">&quot;32&quot;</span> <span style="color: #000066;">maxStringContentLength</span>=<span style="color: #ff0000;">&quot;8192&quot;</span> <span style="color: #000066;">maxArrayLength</span>=<span style="color: #ff0000;">&quot;16384&quot;</span></span>
<span style="color: #009900;">                        <span style="color: #000066;">maxBytesPerRead</span>=<span style="color: #ff0000;">&quot;4096&quot;</span> <span style="color: #000066;">maxNameTableCharCount</span>=<span style="color: #ff0000;">&quot;16384&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;security</span> <span style="color: #000066;">mode</span>=<span style="color: #ff0000;">&quot;None&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;transport</span> <span style="color: #000066;">clientCredentialType</span>=<span style="color: #ff0000;">&quot;None&quot;</span> <span style="color: #000066;">proxyCredentialType</span>=<span style="color: #ff0000;">&quot;None&quot;</span></span>
<span style="color: #009900;">                            <span style="color: #000066;">realm</span>=<span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;message</span> <span style="color: #000066;">clientCredentialType</span>=<span style="color: #ff0000;">&quot;UserName&quot;</span> <span style="color: #000066;">algorithmSuite</span>=<span style="color: #ff0000;">&quot;Default&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/security<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/binding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/basicHttpBinding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

</blockquote>
<p>De tal forma que ahora en el código fuente podemos instanciarlo de 3 formas distintas:</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">            ExampleClient clientDefault <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ExampleClient<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;WSHttpBinding_IExample&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> namesDefault <span style="color: #008000;">=</span> clientDefault.<span style="color: #0000FF;">GetNames</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            ExampleClient clientSOAP11 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ExampleClient<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;BasicHttpBinding_IExample&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> namesSOAP11 <span style="color: #008000;">=</span> clientSOAP11.<span style="color: #0000FF;">GetNames</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            ExampleClient clientSOAP12 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ExampleClient<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;WSHttpBinding_IExample1&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> namesSOAP12 <span style="color: #008000;">=</span> clientSOAP12.<span style="color: #0000FF;">GetNames</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.oxxigeno.com/blog/2009/05/nuevas-tecnologias-windows-comunication-foundation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C#, .NET 2.0 y procesamiento de CSV</title>
		<link>http://www.oxxigeno.com/blog/2009/04/generalizando-procesamiento-csv/</link>
		<comments>http://www.oxxigeno.com/blog/2009/04/generalizando-procesamiento-csv/#comments</comments>
		<pubDate>Mon, 13 Apr 2009 14:06:41 +0000</pubDate>
		<dc:creator>ivinuales</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Desarrollo]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[ascx]]></category>
		<category><![CDATA[aspx]]></category>
		<category><![CDATA[microsoft]]></category>

		<guid isPermaLink="false">http://www.oxxigeno.com/blog/?p=483</guid>
		<description><![CDATA[Los ficheros csv son ficheros de texto que contienen valores separados por alguna caracter (en nuestro caso punto y coma) y agrupados en líneas. Es decir, cada línea podemos considerarla un conjunto de valores completo y podemos identificar a este conjunto de valores como una entidad.

Lo primero que debemos hacer es identificar esa entidad y [...]]]></description>
			<content:encoded><![CDATA[<p>Los ficheros csv son ficheros de texto que contienen valores separados por alguna caracter (en nuestro caso punto y coma) y agrupados en líneas. Es decir, cada línea podemos considerarla un conjunto de valores completo y podemos identificar a este conjunto de valores como una entidad.</p>
<p><span id="more-483"></span><br />
Lo primero que debemos hacer es <strong>identificar esa entidad y crear una clase para guardar esos datos</strong>. Después debemos tener algun mecanismo para rellenar esta clase con los valores de cada una de las filas (cada fila del fichero supondrá un objeto de ésta clase).</p>
<p>Pongamos un ejemplo.  Supongamos un fichero csv como el siguiente:</p>
<blockquote>
<pre>Edad; Nombre; Apellido
15; Pepito; Perez
57; Ernesto; Román
RowCount = 2</pre>
</blockquote>
<p>En ocasiones los csv traen una cabecera y/o un pie de fichero y en otras no, por lo que no podemos tratar de procesarlas siempre.</p>
<p>Vamos a empezar creando una clase entidad como la siguiente:</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Edades
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">int</span> _edad<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> _nombre<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> _apellido<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Edad
    <span style="color: #000000;">&#123;</span>
        get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _edad<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        set <span style="color: #000000;">&#123;</span> _edad <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Nombre
    <span style="color: #000000;">&#123;</span>
        get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _nombre<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        set <span style="color: #000000;">&#123;</span> _nombre <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Apellido
    <span style="color: #000000;">&#123;</span>
        get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _apellido<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        set <span style="color: #000000;">&#123;</span> _apellido <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</blockquote>
<p>Como vemos, se trata de una clase tonta con 3 atributos y sus 3 correspondientes propiedades publicas. En este ejemplo sería muy sencillo por cada fila crearnos un objeto y asignar a Edad el primer valor de la fila, a Nombre la segunda y a Apellido la tercera, serían 3 simples líneas. Sin embargo, a medida que el csv (y la clase) contengan más campos esto se haría más complejo, y sería muy complejo de modificar si algún día deciden introducir un campo nuevo en mitad del csv.</p>
<p>Claro este punto, debemos tener alguna forma de <strong>relacionar cada propiedad de la clase con la posicion que ocupa ese valor en el csv</strong>. Para esto tenemos dos opciones:</p>
<ul>
<li>Si la posición de cada campo es susceptible de ser modificada periódicamente deberíamos determinar esta relación en el web.config (si es una aplicación web) o en app.config (si es una aplicación de escritorio)</li>
<li>Si la posición de cada campo no es susceptible de ser modificada, es preferible agregar atributos a la clase que indique donde debemos buscar el valor de cada propiedad.</li>
</ul>
<p>Voy a centrarme en este segundo supuesto, el primero posiblemente lo trataremos en otro artículo.  Vamos a generar una clase que herede de Attribute para poder indicar en la clase que acabamos de crear donde buscaremos cada valor.</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> CSVFieldAttribute <span style="color: #008000;">:</span> Attribute
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">int</span> _order<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Order
    <span style="color: #000000;">&#123;</span>
        get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _order<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        set <span style="color: #000000;">&#123;</span> _order <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> CSVFieldAttribute<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> order<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        _order <span style="color: #008000;">=</span> order<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</blockquote>
<p>Ahora modificamos la clase de la entidad, añadiéndole a cada propiedad un atributo indicando su posición en el csv. La clase quedaría como sigue:</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Edades
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">int</span> _edad<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> _nombre<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> _apellido<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #000000;">&#91;</span>CSVField<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Edad
    <span style="color: #000000;">&#123;</span>
        get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _edad<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        set <span style="color: #000000;">&#123;</span> _edad <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#91;</span>CSVField<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Nombre
    <span style="color: #000000;">&#123;</span>
        get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _nombre<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        set <span style="color: #000000;">&#123;</span> _nombre <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#91;</span>CSVField<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Apellido
    <span style="color: #000000;">&#123;</span>
        get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _apellido<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        set <span style="color: #000000;">&#123;</span> _apellido <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</blockquote>
<p>En este punto podemos elegir partir de una base en 0 o una base en 1. Yo he preferido partir de base 0 porque es la utilizada en los arrays por C#.<br />
Ya tenemos relacionada la clase de entidad con los valores del csv, ahora falta buscarnos la vida para rellenar un objeto a partir de una fila del csv. Propongo utilizar Reflexion y las Templated classes para generalizar este proceso, creando una clase que trasforme un array de valores en un objeto del tipo que le indiquemos. A las propiedades de ese objeto les asignaremos el valor (mediante reflexion) que haya en el array de valores en la posición indicada. Vayamos al tema:</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> CSVConverter<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> T Convert<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> values<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        Type type <span style="color: #008000;">=</span> <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>T<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        T obj <span style="color: #008000;">=</span> Activator.<span style="color: #0000FF;">CreateInstance</span><span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">try</span>
        <span style="color: #000000;">&#123;</span>
            CSVFieldAttribute att <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
            <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> attributes <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>PropertyInfo prop <span style="color: #0600FF;">in</span> type.<span style="color: #0000FF;">GetProperties</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                attributes <span style="color: #008000;">=</span> prop.<span style="color: #0000FF;">GetCustomAttributes</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>CSVFieldAttribute<span style="color: #000000;">&#41;</span>, <span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>attributes.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    att <span style="color: #008000;">=</span> attributes<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span> <span style="color: #0600FF;">as</span> CSVFieldAttribute<span style="color: #008000;">;</span>
                    <span style="color: #008080; font-style: italic;">/* Si hemos optado por usar base 1 debemos poner:
                        values[att.Order - 1]
                    */</span>
                    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #000000;">&#40;</span>values<span style="color: #000000;">&#91;</span>att.<span style="color: #0000FF;">Order</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Trim</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Equals</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                    <span style="color: #000000;">&#123;</span>
                        prop.<span style="color: #0000FF;">SetValue</span><span style="color: #000000;">&#40;</span>obj,
                           Convert.<span style="color: #0000FF;">ChangeType</span><span style="color: #000000;">&#40;</span>values<span style="color: #000000;">&#91;</span>att.<span style="color: #0000FF;">Order</span><span style="color: #000000;">&#93;</span>, prop.<span style="color: #0000FF;">PropertyType</span><span style="color: #000000;">&#41;</span>,
                           <span style="color: #0600FF;">null</span>
                        <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    <span style="color: #000000;">&#125;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception ex<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Error al intentar hacer la conversión de tipos.<span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span>&quot;</span> <span style="color: #008000;">+</span> ex.<span style="color: #0000FF;">Message</span>, ex<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">return</span> obj<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</blockquote>
<p>Ya solo queda <strong>procesar el fichero para ir creando los objetos</strong> y hacer lo que tengamos que hacer con ellos. Para ello, nos crearemos una clase base a partir de la cual heredaremos las clases de negocio que creamos oportunas para procesar distintos modelos de csv.</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ProcessCSV<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">protected</span> StreamReader _streamReader<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">protected</span> <span style="color: #FF0000;">string</span> _line<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Process<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> filename<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">try</span>
        <span style="color: #000000;">&#123;</span>
            _streamReader <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StreamReader<span style="color: #000000;">&#40;</span>filename<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">ProcessHeader</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">ProcessBody</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">ProcessFooter</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception ex<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">throw</span> ex<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">finally</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_streamReader <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                _streamReader.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">virtual</span> <span style="color: #0600FF;">void</span> ProcessHeader<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;No implementado&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">virtual</span> <span style="color: #0600FF;">void</span> ProcessFooter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;No implementado&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">virtual</span> <span style="color: #0600FF;">void</span> ProcessOneObject<span style="color: #000000;">&#40;</span>T obj<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;No implementado&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> ProcessBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        T obj <span style="color: #008000;">=</span> Activator.<span style="color: #0000FF;">CreateInstance</span><span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        _line <span style="color: #008000;">=</span> _streamReader.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>_streamReader.<span style="color: #0000FF;">EndOfStream</span> <span style="color: #008000;">&amp;&amp;</span> _line <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span> <span style="color: #008000;">&amp;&amp;</span> _line.<span style="color: #0000FF;">Trim</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">try</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> values <span style="color: #008000;">=</span> _line.<span style="color: #0000FF;">Split</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">';'</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                CSVConverter<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> converter <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CSVConverter<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                obj <span style="color: #008000;">=</span> converter.<span style="color: #0000FF;">Convert</span><span style="color: #000000;">&#40;</span>values<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">ProcessOneObject</span><span style="color: #000000;">&#40;</span>obj<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception ex<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">// Aquí escribiríamos algo en el log indicando qué fila ha fallado</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</blockquote>
<p>Ya tenemos nuestra clase genérica. Pero claro, con esto no hacemos nada, si intentas parsear algo llamando a esta clase te van a saltar excepciones porque no está implementados algunos métodos necesarios. Generemos la clase que realmente usaremos.</p>
<p>Partimos de varios supuestos: el csv es el que he puesto arriba (con cabecera y pie), no vamos a usar para nada ni la cabecera ni el pie y queremos agregar a una lista para luego procesarlos (esto no lo voy a contemplar en el articulo).<br />
Al tema:</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> EdadesBL <span style="color: #008000;">:</span> ProcessCSV<span style="color: #008000;">&lt;</span>Edades<span style="color: #008000;">&gt;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> List<span style="color: #008000;">&lt;</span>Edades<span style="color: #008000;">&gt;</span> _list<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">public</span> EdadesBL<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        _list <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>Edades<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> ProcessHeader<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// Solo leemos porque no queremos conservar la cabecera</span>
        _streamReader.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> ProcessFooter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// Solo leemos porque no queremos conservar la cabecera</span>
        _streamReader.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> ProcessOneObject<span style="color: #000000;">&#40;</span>Edades obj<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// Solo queremos agregarlo a la lista.</span>
        _list.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>obj<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> List<span style="color: #008000;">&lt;</span>Edades<span style="color: #008000;">&gt;</span> Edades
    <span style="color: #000000;">&#123;</span>
        get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _list<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</blockquote>
<p>Ahora solo tenemos que <strong>procesar el fichero con dos simples líneas</strong> (en nuestro pequeño ejemplo 3 para recoger tambien la lista de Edades):</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">EdadesBL business <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> EdadesBL<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
business.<span style="color: #0000FF;">Process</span><span style="color: #000000;">&#40;</span>filename<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
List todos <span style="color: #008000;">=</span> business.<span style="color: #0000FF;">Edades</span><span style="color: #008000;">;</span></pre></div></div>

</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.oxxigeno.com/blog/2009/04/generalizando-procesamiento-csv/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Buenas prácticas: aspx sin código de servidor</title>
		<link>http://www.oxxigeno.com/blog/2009/02/buenas-practicas-aspx-sin-codigo-de-servidor/</link>
		<comments>http://www.oxxigeno.com/blog/2009/02/buenas-practicas-aspx-sin-codigo-de-servidor/#comments</comments>
		<pubDate>Fri, 13 Feb 2009 13:45:35 +0000</pubDate>
		<dc:creator>ivinuales</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Desarrollo]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[ascx]]></category>
		<category><![CDATA[aspx]]></category>
		<category><![CDATA[microsoft]]></category>

		<guid isPermaLink="false">http://www.oxxigeno.com/blog/?p=293</guid>
		<description><![CDATA[La capa de presentación de un sitio web es una parte muy sensible del mismo, pues debe proporcionar al usuario toda la información que solicita, de forma que la carga de proceso en la misma sea mínima.
Los sitios web desarrollados bajo el .NET framework son publicados con una precompilación del código fuente, sin embargo, las [...]]]></description>
			<content:encoded><![CDATA[<p>La capa de presentación de un sitio web es una parte muy sensible del mismo, pues debe proporcionar al usuario toda la información que solicita, de forma que la carga de proceso en la misma sea mínima.</p>
<p>Los sitios web desarrollados bajo el .NET framework son publicados con una <strong>precompilación del código fuente</strong>, sin embargo, las páginas <strong>aspx</strong> y los controles <strong>ascx</strong> son interpretados en tiempo de ejecución. Es decir: cada vez que un usuario solicita una página aspx ésta es interpretada por el servidor de aplicaciones y presentada al cliente.</p>
<p>Como puede suponerse, el hecho de que la página aspx contenga código fuente que deba ser compilado ralentizará la carga de la misma, puesto que se pasa de interpretar html y devolverlo a compilar código de servidor con más o menos lógica.</p>
<p>En muchas ocasiones es posible pensar que no nos es posible <strong>crear la página aspx tal y como queremos sin incluir código de servidor</strong> en la misma, esto es un error muy común, pero facilmente subsanable, siguiendo las siguientes recomendaciones.</p>
<p><span id="more-293"></span></p>
<ul>
<li>Solo está permitido <strong>utilizar código de servidor para cargar elementos dentro de controles de tipo DataBound </strong>(GridView, FormView, etc.). Aun así es poco recomendable porque sigue requiriendo compilación. Se recomienda la utilización del <strong>control Repeater</strong>.</li>
</ul>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">&nbsp;
                <span style="color: #ddbb00;">&amp;lt;</span>%# Eval(&quot;TipoDePagina.Nombre&quot;) %<span style="color: #ddbb00;">&amp;gt;</span>
&nbsp;
                Editar Contenidos</pre></div></div>

</blockquote>
<ul>
<li>Si necesitamos que <strong>un elemento HTML obtenga propiedades</strong> (style, href, title, etc.) desde el lado del servidor, porque utilice alguna lógica de negocio debemos identificar a éste elemento con <strong>runat=server</strong> y asignar dichas propiedades desde el <strong>code-behind</strong>, que va precompilado cuando se publica.</li>
</ul>
<p>aspx no recomendado:</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;global&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;fondo-menu-&amp;lt;%= ambitoCabecera %&amp;gt;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;cabecera&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;cabecera-&amp;lt;%= ambitoCabecera%&amp;gt;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;cabecera-logo&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">title</span>=<span style="color: #ff0000;">&quot;Alt&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;#&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;imgLogo&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;../images/&amp;lt;%= image%&amp;gt;&quot;</span> <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">&quot;Grupo Banco Popular&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;148&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;50&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;shortcuts&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ul</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;horizontal&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;#&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Atención al cliente<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> |<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;#&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Sucursales<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> |<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;#&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Tarifas<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> |<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;#&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Simuladores<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;buscador&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fieldset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fieldset<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

</blockquote>
<p>Código no recomendado:</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Imagen por omision para el logo</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> _image <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;default.jpg&quot;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// Ambito por omision para la cabecera</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> _ambitoCabecera <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;normal&quot;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> Page_Load<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #008080;">#region Dar valores a las variables</span>
    ...
    <span style="color: #008080;">#endregion</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></div></div>

</blockquote>
<p>aspx recomendado:</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;global&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;cabecera&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;cabecera-logo&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">title</span>=<span style="color: #ff0000;">&quot;Alt&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;#&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span> <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">&quot;Grupo Banco Popular&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;148&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;50&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;shortcuts&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ul</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;horizontal&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;#&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Atención al cliente<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> |<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;#&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Sucursales<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> |<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;#&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Tarifas<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> |<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;#&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Simuladores<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;buscador&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fieldset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fieldset<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

</blockquote>
<p>Código recomendado:</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> Page_Load<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// Imagen por omision para el logo</span>
    <span style="color: #FF0000;">string</span> image <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;default.jpg&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #008080; font-style: italic;">// Ambito por omision para la cabecera</span>
    <span style="color: #FF0000;">string</span> ambitoCabecera <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;normal&quot;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080;">#region Dar valores a las variables</span>
    ...
    <span style="color: #008080;">#endregion</span>
&nbsp;
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">global</span>.<span style="color: #0000FF;">Attributes</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;class&quot;</span>, <span style="color: #666666;">&quot;fondo-menu-&quot;</span> <span style="color: #008000;">+</span> ambitoCabecera<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">cabecera</span>.<span style="color: #0000FF;">Attributes</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;class&quot;</span>, <span style="color: #666666;">&quot;cabecera-&quot;</span> <span style="color: #008000;">+</span> ambitoCabecera<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">imgLogo</span>.<span style="color: #0000FF;">Src</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;../images/&quot;</span> <span style="color: #008000;">+</span> image<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></div></div>

</blockquote>
<ul>
<li>Si necesitamos que <strong>un elemento HTML reciba su identificador</strong>, propiedades sensibles o una maquetación compleja dependiente de una lógica de negocio, la solución optima es crear un <strong>custom web control</strong> para definirlo, éste tipo de controles heredan de la clase WebControl.</li>
</ul>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">namespace</span> Example.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">Controls</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #000000;">&#91;</span>DefaultProperty<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Text&quot;</span><span style="color: #000000;">&#41;</span>, ToolboxData<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;&amp;lt;{0}:OnlyText runat=server&amp;gt;&lt;!--{0}:OnlyText--&gt;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> OnlyText <span style="color: #008000;">:</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span></span>.<span style="color: #0000FF;">WebControl</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080;">#region Attributes</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> _text <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #008080;">#region Properties</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>Bindable<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;true&quot;</span><span style="color: #000000;">&#41;</span>, Category<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Data&quot;</span><span style="color: #000000;">&#41;</span>, DefaultValue<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Text
        <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _text<span style="color: #008000;">;</span><span style="color: #000000;">&#125;</span>
            set <span style="color: #000000;">&#123;</span> _text <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span><span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #008080;">#region Overrides Methods</span>
&nbsp;
        <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span>  Render<span style="color: #000000;">&#40;</span>HtmlTextWriter writer<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
             writer.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>Text<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080;">#endregion</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.oxxigeno.com/blog/2009/02/buenas-practicas-aspx-sin-codigo-de-servidor/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 1.553 seconds -->
