Microsoft Trainer e Software Architect
Sviluppatore professionista dal 2001 con VB6, SQL 2000 e Access creando applicazioni gestionali di varia natura. Passando rapidamente a .NET, windows e web, ho poi espanso le mie esperienze su Compact Framework, SQL Server Mobile ed infine BizTalk Server.
Negli ultimi anni mi sono focalizzato sempre più sullo sviluppo distribuito ed enterprise conseguendo nel contempo le seguenti certificazioni:
Microsoft Certified Trainer (MCT)Microsoft MCPD .NET 3.5 Enterprise Applications DeveloperMicrosoft MCTS .NET 3.5 WCF Applications DevelopmentMicrosoft MCTS .NET 3.5 ASP.NET Applications DevelopmentMicrosoft MCTS .NET 3.5 ADO.NET Applications DevelopmentMicrosoft MCTS .NET 3.5 Windows Applications DevelopmentMicrosoft MCTS .NET 2.0 Distributed Applications DevelopmentMicrosoft MCP .NET
Ciao a tutti
gli operatori impliciti sono particolarmente comodi nella leggibilità del codice.
Come esempio, vediamo un semplicissimo DataMapper
In uno scenario realistico, questo codice sarebbe implementato all’interno di un container IoC
leggendo dall’inizio avremo le dichiarazioni delle variabili, le assegnazioni (che si leggono da destra a sinistra) e le classi per come sono implementate:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace DataMapperTest { class Program { static void Main(string[] args) { DataMapper<DataInvoice, BusinessInvoice> dataToBusiness; DataMapper<BusinessInvoice, DataInvoice> businessToData; DataInvoice newDataInvoice; DataInvoice dataInvoiceFromDb; BusinessInvoice businessInvoice;
dataInvoiceFromDb = new DataInvoice { Date = DateTime.Now.Date, Number = 15 }; newDataInvoice = businessToData = businessInvoice = dataToBusiness = dataInvoiceFromDb; } }
public class BusinessInvoice { public int Number { get; set; } public string Date { get; set; } }
public class DataInvoice { public int Number { get; set; } public DateTime Date { get; set; } }
public abstract class DataMapper<FROM, TO> { protected FROM FromArgument { get; set; } protected abstract TO Map();
public static implicit operator DataMapper<FROM, TO>(FROM arg) { var mapper = typeof(DataMapper<,>).Assembly.GetTypes().FirstOrDefault(x => x.BaseType.Equals(typeof(DataMapper<FROM, TO>)));
if (mapper == null) throw new ArgumentException("Unable to find a proper datamapper!");
var instance = (DataMapper<FROM, TO>)mapper.Assembly.CreateInstance(mapper.FullName); instance.FromArgument = arg; return instance; }
public static implicit operator TO(DataMapper<FROM, TO> arg) { return arg.Map(); } }
public class BusinessToDataInvoiceMapper : DataMapper<BusinessInvoice, DataInvoice> { protected override DataInvoice Map() { return new DataInvoice { Number = FromArgument.Number, Date = DateTime.Parse(FromArgument.Date), }; } }
public class DataToBusinessInvoiceMapper : DataMapper<DataInvoice, BusinessInvoice> { protected override BusinessInvoice Map() { return new BusinessInvoice { Number = FromArgument.Number, Date = FromArgument.Date.ToString(), }; } } }
come compreso, il datamapper ha 2 operatori impliciti: uno dal tipo in request a se stesso, conservando il riferimento all’oggetto originale
ed un secondo operatore verso la vera e propria conversione
se andassimo a creare un pool di classi come quelle realizzate per le conversioni mettendole in un comodo gruppo di assembly caricati per dependency injection, avremo un comodo sistema di datamapper completamente basato sugli operatori impliciti che ci consentono l’assegnazione del valore al posto dell’invocazione del metodo
a presto
Anto