martedì 11 giugno 2013

E poi venne DYNAMIC

Lo so lo so sono un pazzo furioso, ammetto tutte le mie colpe ma credo davvero che mi sia spalancata la porta del sapere.
Questa mattina mi stavo arrovellando il cervello su come gestire una connettività fra classi in modo che si attivino come un Cromatoforo di un polpo.
Per chi non conoscesse l'argomento il cromatoforo, è un cellula della pelle di un polpo o di una seppia, che consente se stimolata di modificare il suo stato. La "vera fissa" di questa entità e che lavora in gruppo.
Di contro se questo fosse ralizzabile in ambito "software" sarebbe davvero qualcosa di unico.. immaginate classi di calcolo che lavorano all'unisono solo se sono sollecitate o classi di calcolo che sanno che sollecitare per lavorare... Mi vengono in mente mille soluzioni, e mille ipotesi. Ma per il momento mi godo questo momento e non penso ad altro.
(non è vero sto renderizzando ... )

Dynamic è qualcosa di "astratto" ma concreto non saprei dare una definizione, non è generic, benchè sia generico ha il vantaggio di non dover ( farti ) implementare invocatori di metodi con reflection.

Ma non attendo altro .. questo è il mio esempio.. VS2010 framework 4

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dynamic
{
    class Program
    {
        static void Main(string[] args)
        {

            DynamicDataProvider d0 = new DynamicDataProvider(0);
            entities et0 = d0.GetEntities();

            Console.WriteLine("Real Data provider");
            for (int i = 0; i < et0.Count(); i++)
            {
                Console.WriteLine(et0[i].id + "\t" + et0[i].name);
            }

            DynamicDataProvider d1 = new DynamicDataProvider(1);
            entities et1 = d1.GetEntities();

            Console.WriteLine("Mock Data provider");
            for (int i = 0; i < et1.Count(); i++)
            {
                Console.WriteLine(et1[i].id + "\t" + et1[i].name);
            }


            Console.WriteLine("Dynamc Real Data provider");
            dynamic dd = new DynamicDataProvider(0);
            var etn = dd.GetEntities();
            
            foreach (dynamic e in etn)
            {
                Console.WriteLine(e.id + "\t" + e.name);
            }


            Console.WriteLine("Dynamc fake Data provider");
            dynamic dd1 = new DynamicDataProvider(3);
            var etn1 = dd1.GetEntities();

            foreach (dynamic e in etn1)
            {
                Console.WriteLine(e.id + "\t" + e.name);
            }


            Console.ReadLine();

        }
    }

    public class entity
    {
        public int id { get; set; }
        public string name { get; set; }
        public string description { get; set; }
    }


    public class entityBis
    {
        public int id { get; set; }
        public string name { get; set; }
        public string value { get; set; }
    }

    public class entities : List<entity>
    {     
    }


    public class entitiesBis : List<entityBis>
    {
    }


    public class DynamicDataProvider
    {
        int retType = 0;

        public DynamicDataProvider(int cType)
        {
            retType = cType;
        }

        public dynamic GetEntities()
        {
            if (retType == 0)
            {
                return new realDataProvider().fillEntites();
            }
            if ( retType == 1)
            {
                return new mockDataProvider().fillEntites();
            }

            return new fakeDataProvider().fillEntites();
        }

    }

    public class realDataProvider
    {
        public entities fillEntites()
        {
            entities entx = new entities();
            entx.Add(new entity() { id = 1, 
                                    name = "default", 
                                    description = "" });
            entx.Add(new entity() { id = 2, 
                                    name = "one", 
                                    description = "" });
            entx.Add(new entity() { id = 3, 
                                    name = "many", 
                                    description = "" });
            entx.Add(new entity() { id = 4, 
                                    name = "nome", 
                                    description = "" });


            return entx;
        }
    }

    public class fakeDataProvider
    {
        public entitiesBis fillEntites()
        {
            entitiesBis entx = new entitiesBis();
            entx.Add(new entityBis() { id = 1, 
                                       name = "default Bis", 
                                       value = "1" });
            entx.Add(new entityBis() { id = 2, 
                                       name = "one Bis", 
                                       value = "2" });
            entx.Add(new entityBis() { id = 3, 
                                       name = "many Bis", 
                                       value = "3" });
            entx.Add(new entityBis() { id = 4, 
                                       name = "nome Bis", 
                                       value = "4" });


            return entx;
        }
    }


    public class mockDataProvider
    {
        public entities fillEntites()
        {
            entities entx = new entities();
            entx.Add(new entity() { id = 1, 
                                    name = "mock", 
                                    description = "" });

            return entx;
        }
    }
}


L'unica è prendere questo codice e provarlo in una console application.. realDataProvider e fakeDataProvider propongono lo stesso metodo ma non lo stesso tipo e questo mi gasa particolarmente.
Non basta... le ultime due porzioni di codice del Main sono davvero "potentissime" stesso codice differente risultato su due tipi diversi.
Riesco a immaginare librerie che caricano dinamicamente i plugin certo bisogna stare attenti a come si scrive il codice ma le potenzialità sono davvero tante.

Nessun commento: