1. Cors
Ayuda
Inicializar una nueva instancia de la estructura Guid.
var newGuid = Guid.NewGuid();
Se puede igualar a una propiedad double nullable de una Entidad.
(float)-3.6708850081608
Int32.MaxValue
5. AutoMapper
Mapear objeto simple con propiedades primitivas.
Mapper.CreateMap<DtoX,ViewModelX>(); var viewModel = Mapper.Map<DTOx,ViewModelX>
Mapear listas simples con propiedades primitivas.
var ListItemDTO = GetList(); //List of Item DTO Mapper.CreateMap<ItemDTOX,ItemViewModelX>(); var ListItemViewModel = ListItemDTO.Select(x=> { var unitViewModel = Mapper.Map<DTOx,ViewModelX>(x); return unitViewModel; }).ToList();
//ObjetoGlobalViewModel es el objeto con props primitivas (prop1, prop2) + propiedad coleccionVM (lista de ItemVM) //ObjetoGlobalDTO es el objeto con props primitivas (prop1, prop2) + coleccionDTO (lista de ItemDTO) var coleccionVM = ObjetoGlobalViewModel.ColeccionVM; //List of ItemVM Mapper.CreateMap<ItemVM,ItemDTO>(); var listaItemDTOAux = coleccionVM.Select(x=> { var unitdto = Mapper.Map<ItemVM,ItemDTO>(x); return unitdto; }).ToList(); var resultado = new ObjetoGlobalDTO { prop1 = ObjetoGlobalViewModel.prop1, prop2 = ObjetoGlobalViewModel.prop2, coleccionDTO = listaItemDTOAux }
En ocasiones necesitamos realizar operaciones con cadenas de texto que se repiten en varios zonas del codigo. Se muestran algunos ejemplos extendiendo la clase String y "usando" la propia clase para acceder al método:
public static class StringExtensions { /// <summary> /// Get substring of specified number of characters on the left. /// </summary> /// <param name="length"></param> /// <returns>string</returns> public static string Left(this string value, int length) { if (string.IsNullOrEmpty(value)) return value; length = Math.Abs(length); return (value.Length <= length ? value : value.Substring(0, length)); } /// <summary> /// Get substring of specified number of characters on the right. /// </summary> /// <param name="length"></param> /// <returns>string</returns> public static string Right(this string value, int length) { if (String.IsNullOrEmpty(value)) return string.Empty; return value.Length <= length ? value : value.Substring(value.Length - length); } /// <summary> /// Check if string could be parsed to double /// </summary> /// <param name="input"></param> /// <param name="numberStyle"></param> /// <returns>boolean</returns> public static Boolean IsNumeric(String input, NumberStyles numberStyle) { Double temp; Boolean result = Double.TryParse(input, numberStyle, CultureInfo.CurrentCulture, out temp); return result; } }