Tag Archive for development

Mercurial as a Git client

Days ago I just published a short blog post about using Mercurial as a SVN Client, I know, Subversion is the “now-legacy-vcs-system” and Git with Mercurial are the “cool kids” in the school, why not make them work together?

Well, in the same way we could make Subversion works with Hg (using extensions of course) we could make Git works with Mercurial. It requires a little more work in Windows but it stills easy to do. As usual we start with a TortoiseHg installation. I will try to show its installation in a short set of steps:

  • We need to activate the Mercurial Bookmark and rebase extensions (these extensions are included with standard Mercurial installation). Edit your mercurial.ini file and add the following to the [extensions] section
  • [extensions]
    rebase =
    hgext.bookmarks = 
  • HgGit needs a special python library to handle Git repositories, this library is named dulwich. You need to get it from its source repository, clone it in some directory

    hg clone http://bitbucket.org/abderrahim/dulwich
  • Now we need to get the HgGit source code from its repository, let’s clone it from repository

    hg clone http://bitbucket.org/durin42/hg-git
  • This is the hard part, we need to tell to HgGit the location where the dulwich library is installed. Remember, both HgGit and dalwich are Python libraries, so we’ll edit just a file to show where to get dalwich. Go to the place you cloned HgGit search and open the file __init__.py and add look for the following code and add the code highlighted
    import os
    
    # NOTE: Added for tortoisehg compatibility
    import sys
    sys.path.append('write here the directory where dulwich was cloned')
    
    from mercurial import commands, extensions, hg, util as hgutil
    from mercurial.i18n import _
  • Now let’s activate the extension, open again your mercurial.ini file and add the following line to your [extensions] section

    hggit = directory where hggit where clonedhggit

And that is all, just test your extension installation with hg help extensions And look for hggit

Now you can clone, rebase, push, pull and do whatever you do with mercurial but using remote git repositories, for example, now you can do something like

hg clone http://github.com/castleproject/Castle.Core.git castle

I hope this guide could help you to get more fun from Mercurial, see you later!

.NET y Configuraciones – Parte 3

Ok, hasta el momento ya vimos como crear nuestras propias secciones y valores de configuración, además de darnos una vuelta por las opciones de crear y agregar elementos a nuestras secciones personalizadas. Hoy continuaremos indagando un poco más en el mundo de las configuraciones personalizadas en .NET.

Grupos de Secciones

Imagínense que la configuración de nuestra aplicación es mucho más compleja de lo usual y que además de elementos personalizados necesitamos agregar “subsecciones” de configuración. Dentro de la .Net Framework es algo común en ASP.NET. Crear secciones de configuración no puede ser más fácil, basta con crear las secciones como lo hemos estado haciendo hasta el momento. Tomemos por ejemplo las siguientes dos secciones:

public class SampleSection : ConfigurationSection {
    [ConfigurationProperty("name", IsRequired = true)]
    public string Name {
        get { return (string) base["name"]; }
        set { base["name"] = value; }
    }

    [ConfigurationProperty("port", DefaultValue = 80)]
    public int Port {
        get { return (int) base["port"]; }
        set { base["port"] = value; }
    }
}

public class AnotherSection : ConfigurationSection {
    [ConfigurationProperty("times", IsRequired = true)]
    public int TimesPerDay {
        get { return (int) base["times"]; }
        set { base["times"] = value; }
    }
}

Basta ahora con crear un ConfigurationSectionGroup para obtener ambas secciones dentro de un mismo grupo

public class SampleSectionGroup : ConfigurationSectionGroup {
    [ConfigurationProperty("sample")]
    public SampleSection Sample {
        get { return (SampleSection) Sections["sample"]; }
    }

    [ConfigurationProperty("another")]
    public AnotherSection Another {
        get { return (AnotherSection) Sections["another"]; }
    }
}

Agregarla en el web.config es un poco más laborioso pero continúa siendo sencillo

<configuration>
  <configSections>
    <sectionGroup name="sampleGroup" type="WebApplication2.SampleSectionGroup">
      <section name="sample" type="WebApplication2.SampleSection"/>
      <section name="another" type="WebApplication2.AnotherSection"/>
    </sectionGroup>
  </configSections>
  <sampleGroup>
    <sample name="sample" port="31" />
    <another times="5" />
  </sampleGroup>
</configuration>

Creo que para estas alturas el usarla en el código debe ser algo sencillo y "straighforward"

.NET y Configuraciones – Parte 2

Anteriormente habíamos conversado acerca de como trabajar nuestras propias secciones “customizadas” de configuración usando las facilidades de configuración que nos da la .Net Framework. Como vemos es sumamente fácil crear nuestras secciones “personalizadas”. Conversaremos hoy un poco más de como sacarle mayor provecho y extender nuestras secciones de configuración y así sacar mucho mayor provecho.

Elementos personalizados anidados

Observemos el siguiente snippet de código

public class HostInfo : ConfigurationElement {
    [ConfigurationProperty("server", DefaultValue = "localhost")]
    public string Server {
        get { return (string) base["server"]; }
        set { base["server"] = value; }
    }

    [ConfigurationProperty("port", DefaultValue = 80)]
    public int Port {
        get { return (int) base["port"]; }
        set { base["port"] = value; }
    }

    [ConfigurationProperty("ssl", DefaultValue = false)]
    public bool IsSecure {
        get { return (bool) base["ssl"]; }
        set { base["ssl"] = value; }
    }
}

Aunque nos recuerda a la forma en que creamos una sección de configuración, realmente estamos creando un elemento de la configuración (ConfigurationElement). En otras palabras, podemos tener elementos anidados y de esa manera lograr que nuestros elementos de configuración obedezcan a una más clara semántica. Utilizarlo en la sección de configuración no puede ser más fácil

public class ServerConfigurationSection : ConfigurationSection {
    [ConfigurationProperty("name", IsRequired = true)]
    public string Name {
        get { return (string)base["name"]; }
        set { base["name"] = value; }
    }

    [ConfigurationProperty("host")]
    public HostInfo Host {
        get { return (HostInfo)base["host"]; }
        set { base["host"] = value; }
    }
}

Usarlo no puede ser más fácil que esto

<configuration>
  <configSections>
    <section name="server" type="WebApplication2.ServerConfigurationSection"/>
  </configSections>
  <server name="myconfiguration">
    <host server="192.168.1.1" port="81"/>
  </server>
</configuration>

.NET y configuraciones – Parte 1

Hace unos días 0Gis0 publicó un buen artículo sobre el archivo de configuración en .Net y como crear nuestras propias secciones personalizadas. Bien, he decidido ahondar un “poco” más en el asunto aprovechando el hecho que tengo prometido a un par de amigos desde hace ratos que publicaría acerca de configuración en .Net, es buena idea que antes le den una rápida leída al artículo que en cuestión.

Recapitulando lo que nos menciona nuestra compañera, es sencillo crear una sección personalizada de configuración en .Net, basta solamente de heredar de la clase ConfigurationSection y listo. Bueno, no tan rápido, hay un par de cosas de hacer notar, la sección personalizada de configuración debe exponer propiedades que se mapean a las opciones de configuración, estas propiedades deben estar “adornados” con los atributos ConfigurationProperty y obligatoriamente debemos indicar a que atributo de la sección de configuración a la cual se mapea. A partir de esto usamos el indexer de la clase base y la casteamos al tipo que necesitamos.

public class SimpleConfigurationSection : ConfigurationSection {

    [ConfigurationProperty("port")]
    public int Port {
       get {
           return (int) this["port"];
       }
       set {
           this["port"] = value;
       }
    }
}

Esto correspondería a algo así en el archivo de configuración

<configSections>
    <section name="smtp" type="SimpleConfigurationSection, MyAssembly" />
</configSections>
<smtp port="25">

Simple y sencillo, de igual manera podemos indicar si el elemento es requerido y también el valor por defecto de la propiedad con simplemente modificar un poco el atributo

public class SimpleConfigurationSection : ConfigurationSection {

    [ConfigurationProperty("port", DefaultValue = 80, IsRequired = true)]
    public int Port {
       get {
           return (int) this["port"];
       }
       set {
           this["port"] = value;
       }
    }
}

Simple y sencillo, como lo menciona nuestra amiga. Con esto como base podemos acceder la configuración de forma simple

var config = (SimpleConfigurationSection) ConfigurationManager.GetSection("smtp");
var port = config.Port;

Con esto como base estamos listos para profundizar un poco más en el asunto. En la próxima entrega veremos como definir jerarquías de configuración en nuestras secciones personalizadas.

Saludos!

UPDATE: otros post de la serie han sido publicados

FluentNhibernate y Sql-query

Hoy en la oficina uno de mis compañeros de trabajo me presenta un simple problema muy particular

Necesito llamar a un procedimiento almacenado desde NHibernate, pero estoy usando FluentNhibernate para el mapeo de entidades, ¿hay algo que puedo hacer?

No es algo así como un problema “clásico” el tener que llamar procedimientos almacenados en Nhibernate, pero creo que vale la pena explicarlo.

Digamos que el procedimiento almacenado sea algo como esto

CREATE PROCEDURE [dbo].[usp_GetItemCount](@parentId INT) AS
BEGIN
    SELECT COUNT(*) AS [Total]
    FROM [items]
    WHERE [parent_id] = @parentId
END

Bueno, nada complicado, pero digamos que necesitamos llamarlo desde algún lugar de nuestra aplicación. Digamos que debido a que es algo fuera de la entidad de negocio necesitamos hacerlo usando un servicio. Bueno, escribamos el servicio

public interface ICountService {
    int GetTotal(int parentId);
}

Como recordaran, en NHibernate existe algo llamado named queries utilizadas para ejecutar consultas de SQL arbitrarias en el motor de base de datos, es una herramienta sumamente poderosa, les recomiendo dar una vuelta por la documentación. Una named query se define en un archivo de mapeo HBM al igual que los archivos de mapeo de NHibernate, por lo tanto podemos definir esta named query en el archivo Queries.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <sql-query name="SampleQuery" cacheable="true"
             read-only="true" callable="true">
    <query-param name="parentId" type="Int32"/>
    <return-scalar type="Int32" column="Total"/>
    exec usp_GetItemCount @parentId = :parentId
  </sql-query>
</hibernate-mapping>

Ok señores, pero digamos que estamos usando Fluent NHibernate para el mapeo de nuestras entidades… Rayos… Bueno, no tanto, en realidad es bastante sencillo, en Fluent Nhibernate tenemos la opción de agregar mapeos de XML al mapeo "fluent"

SessionFactory =
 Fluently.Configure().Database(MsSqlConfiguration.MsSql2008
     .ConnectionString(
         c => c.Server("(local)").Database("Test")
             .TrustedConnection())
         .ShowSql().CurrentSessionContext("web")
         .ProxyFactoryFactory<ProxyFactoryFactory>())
     .Mappings(m => m.FluentMappings.AddFromAssemblyOf<ItemClassMap>())
     .Mappings(m => m.HbmMappings.AddFromAssemblyOf<ItemClassMap>())
     .BuildSessionFactory();

Digamos que el archivo de mapeo de la query se encuentra en el mismo lugar que el de las classmaps de fluent nhibernate. Ahora es sencillo definir el servicio que ejecuta el procedimiento almacenado.

public class CountServiceImpl : ICountService {
    readonly ISessionFactory _factory;
    protected ISession Session {
        get { return _factory.GetCurrentSession(); }
    }

    public CountServiceImpl(ISessionFactory factory) {
        _factory = factory;
    }

    public int GetTotal(int parentId) {
        var total = Session.GetNamedQuery("SampleQuery")
            .SetInt32("parentId", parentId)
            .UniqueResult<int>();
        return total;
    }
}

Simple, sencillo, llano, conciso…

Como siempre para comentarios o lo que quieran decir/compartir/discutir se encuentra la cajita de comentarios en este post… Hasta la próxima!