martes, 23 de julio de 2013

Kaleidos Manifest



  1. In the beginning, it's the people
  2. Code should be beautiful
  3. Customers are not a necessary evil
  4. Love your work and your work will be loved
  5. Shelfware is wrong
  6. Let us surprise you
  7. 1.618033988749894848204586834
  8. In the end, it's the people

nice!!!!

jueves, 11 de julio de 2013

Playing with AutoMapper & DataTable in C#

Some time ago, I mapped every colum from database in a POCO. This task was very boring, so for the next project, I´ve found a solution is AutoMapper.
The simple, but real example.

We have a POCO or a DTO User:
using System;

public class User
{
    public int ID { get; private set; }
    public string Name { get; private set; }
    public string LastName { get; private set; }
    public int Age { get; private set; }

    public User WithID(int id)
    {
        ID = id;
        return this;
    }
    public User WithName(string name) 
    {
        Name = name;
        return this;
    }
    public User WithLastName(string lastName)
    {
        LastName = lastName;
        return this;
    }
    public User WithAge(int age)
    {
        Age = age;
        return this;
    }

    public User()
    {
        ID = 0;
        Name = string.Empty;
        LastName = string.Empty;
        Age = 0;
    }
}

We have the mapper:
using System;
using AutoMapper;
using System.Data;
using System.Collections.Generic;

public class UserMapper
{
    public IEnumerable Map(DataTable data)
    {
        AutoMapper.Mapper.Reset();
        AutoMapper.Mapper.CreateMap()
                        .ForMember(t => t.ID, f => f.MapFrom(r => r["IdUser"]))
                        .ForMember(t => t.LastName, f => f.MapFrom(r => r["Last_Name"]));
        return AutoMapper.Mapper.Map>(data.CreateDataReader());
    }
}

The Test (I use fluent Assertions):
using System;
using System.Linq;
using System.Collections.Generic;
using System.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PruebaAutoMapper;
using System.Collections;
using FluentAssertions;

private DataTable CreateDataTable()
{
    var result = new DataTable();
    result.Columns.Add(new DataColumn("IdUser", typeof(int)));
    result.Columns.Add(new DataColumn("Name", typeof(string)));
    result.Columns.Add(new DataColumn("Last_Name", typeof(string)));
    result.Columns.Add(new DataColumn("Age", typeof(int)));

    result.Rows.Add(1, "NameI", "Last_NameI", 1);
    result.Rows.Add(2, "NameII", "Last_NameII", 2);
    result.Rows.Add(3, "NameIII", "Last_NameIII", 3);
    result.Rows.Add(4, "NameIV", "Last_NameIV", 4);

    return result;
}

private IEnumerable CreateList()
{
    var result = new List();

    result.Add(new User().WithID(1).WithName("NameI").WithLastName("Last_NameI").WithAge(1));
    result.Add(new User().WithID(2).WithName("NameII").WithLastName("Last_NameII").WithAge(2));
    result.Add(new User().WithID(3).WithName("NameIII").WithLastName("Last_NameIII").WithAge(3));
    result.Add(new User().WithID(4).WithName("NameIV").WithLastName("Last_NameIV").WithAge(4));

    return result;
}

[TestMethod()]
public void MapTest()
{
    UserMapper mapper = new UserMapper();
    var data = CreateDataTable();
    var expected = CreateList();

    var actual = mapper.Map(data);

    expected.ToList().ShouldAllBeEquivalentTo(actual.ToList());
}
}

Creating Javascript Fluent Api

First of all the tests, using Jasmine (making TDD):

describe("Test for Fluent Car", function(){

    it("Create simple Object Car", function () {
        var car = new Car();

        expect(car).not.toBeNull();
    })

    it("initialize Model", function () {
        var car = new Car();
        var model = 'Model';
        
        car.withModel(model);
        
        expect(car.Model()).toEqual(model);
    })

    it("initialize Model & Gas", function () {
        var car = new Car();
        var model = 'Model';
        var gas =  120;
        
        car.withModel(model)
           .withGas(gas);

        expect(car.Model()).toEqual(model);
        expect(car.Gas()).toEqual(gas);
    })

    it("initialize Make & Model & Gas", function () {
        var car = new Car();
        var make = 'make';
        var model = 'Mmdel';
        var gas =  120;

        car.withModel(model)
           .withMake(make)
           .withGas(gas);
        
        expect(car.Model()).toEqual(model);
        expect(car.Make()).toEqual(make);
        expect(car.Gas()).toEqual(gas);
    })

    it("initialize Height & Width", function () {
        var car = new Car();
        var height = 147;
        var width = 259;

        car.withHeight(height)
           .withWidth(width);
        
        expect(car.Height()).toEqual(height);
        expect(car.Width()).toEqual(width);
    })
});

The code:
function Car(){
 this._make = '';
 this._model = '';
 this_gas = 0;
 this._height = 0;
 this._width = 0;
 
 function Make(){
  return this._make;
 }

 function withMake(make){
  this._make = make;
  return this;
 }

 function Model(){
  return this._model;
 }

 function withModel(model){
  this._model =  model;
  return this;
 }

 function Gas(){
  return this._gas;
 }

 function withGas(gas){
  this._gas = gas;
  return this;
 }

 function Height(){
  return this._height;
 }

 function withHeight(height){
  this._height = height;
  return this;
 }

 function Width(){
  return this._width;
 }

 function withWidth(width){
  this._width = width;
  return this;
 }

 return{
  Make:Make,
  withMake:withMake,
  Model:Model,
  withModel:withModel,  
  Gas:Gas,
  withGas:withGas,
  Width:Width,
  withWidth:withWidth,
  Height:Height,
  withHeight:withHeight
 }
}




Source