Advertisement
fcamuso

Generics - 2

Feb 25th, 2021
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Collections;
  5.  
  6. namespace Generics1
  7. {
  8.   interface IDuplicabile
  9.   {
  10.     public IDuplicabile Clona();
  11.   }
  12.  
  13.   class Documento : IDuplicabile
  14.   {
  15.     public string Testo { get; set; }
  16.     public Documento(string testo) { Testo = testo; }
  17.     public IDuplicabile Clona() { return new Documento("il clone"); }
  18.  
  19.   }
  20.  
  21.   class FigliaDiDocumento : Documento
  22.   {
  23.     public FigliaDiDocumento(string testo) : base(testo) { }
  24.     public FigliaDiDocumento() : base("") { }
  25.   }
  26.  
  27.   class NipoteDiDocumento : FigliaDiDocumento
  28.   {
  29.     public NipoteDiDocumento(string testo) : base(testo) { }
  30.     public NipoteDiDocumento() : base("") { }
  31.   }
  32.  
  33.   class MyList<T, U> where T: IDuplicabile , new()
  34.                      where U : T
  35.   {
  36.     int capacita { get; set; } = 2;
  37.     T[] v = null;
  38.     int inseriti = 0;
  39.     public int count { get { return inseriti; } }
  40.  
  41.     public MyList() { v = new T[capacita];  }
  42.  
  43.     public void Add(T nuovo)
  44.     {
  45.       nuovo.Clona();
  46.       if (inseriti == capacita) Espandi();
  47.       v[inseriti] = nuovo; inseriti++;
  48.  
  49.     }
  50.  
  51.     public void Espandi(int di_quanto = 2)
  52.     {
  53.       T[] nuovo = new T[capacita + di_quanto];
  54.       v.CopyTo(nuovo, 0);
  55.       v = nuovo;
  56.       capacita += di_quanto;      
  57.     }
  58.  
  59.     public T this [int posizione]
  60.     {
  61.       get { return v[posizione];  }
  62.       set { v[posizione] = value; }
  63.     }
  64.   }
  65.  
  66.   //Credits: Giovanni De Rosa
  67.   public interface IEntity
  68.   {
  69.     int Id { get; set; }
  70.   }
  71.  
  72.  
  73.   public abstract class Entity : IEntity
  74.   {
  75.     public int Id { get; set; }
  76.   }
  77.  
  78.   public class Studente : Entity
  79.   {
  80.   }
  81.  
  82.   public class Repository<T> where T : IEntity, new()
  83.   {
  84.     private readonly IList<T> records = Array.Empty<T>();
  85.  
  86.     public T Get(int id)
  87.     {
  88.       // Possiamo accedere alla proprietà Id perchè T rispetta IEntity
  89.       return records.FirstOrDefault(e => e.Id == id);
  90.     }
  91.  
  92.     public void Remove(int id)
  93.     {
  94.       T entity = records.FirstOrDefault(e => e.Id == id);
  95.       records.Remove(entity);
  96.     }
  97.  
  98.     public void Add(T entity) { }
  99.     public void Update(int id, T entity) { }
  100.     public IEnumerable<T> List() => records;
  101.     public T Create() { return new T(); }
  102.   }
  103.  
  104.   //Doppio parametro generic per non essere vincolati al tipo int per la chiave primaria
  105.   public interface IEntity<T>
  106.   {
  107.     T Id { get; set; }
  108.   }
  109.  
  110.   public abstract class Entity2<T> : IEntity<T>
  111.   {
  112.     public T Id { get; set; }
  113.   }
  114.  
  115.   public class Studente2 : Entity2<string>
  116.   {
  117.   }
  118.  
  119.   public class Repository2<TId, TEntity> where TEntity : IEntity<TId>, new()  // DOPPIO SEGNAPOSTO
  120.   {
  121.     private readonly IList<TEntity> records = Array.Empty<TEntity>();
  122.     public TEntity Get(TId id)
  123.     {
  124.       // Possiamo accedere alla proprietà Id perchè T rispetta IEntity
  125.       return records.FirstOrDefault(e => e.Id.Equals(id));
  126.     }
  127.     public void Remove(TId id)
  128.     {
  129.       TEntity entity = records.FirstOrDefault(e => e.Id.Equals(id));
  130.       records.Remove(entity);
  131.     }
  132.     public void Add(TEntity entity) { }
  133.     public void Update(TId id, TEntity entiity) { }
  134.     public IEnumerable<TEntity> List() => records;
  135.     public TEntity Create() { return new TEntity(); }
  136.   }
  137.  
  138.  
  139.   class Program
  140.   {
  141.     static void Main(string[] args)
  142.     {
  143.       //MyList<int> listaInteri = new MyList<int>();
  144.  
  145.       //MyList<string> listaStringhe = new MyList<string>();
  146.       //listaStringhe.Add("prima");
  147.       //listaStringhe.Add("seconda");
  148.       //listaStringhe.Add("terza");
  149.  
  150.       //for (int i = 0; i < listaStringhe.count; i++)
  151.       //  Console.WriteLine(listaStringhe[i]);
  152.  
  153.  
  154.       MyList<FigliaDiDocumento, NipoteDiDocumento> listaDocumenti =
  155.                 new MyList<FigliaDiDocumento, NipoteDiDocumento>();
  156.  
  157.       Repository<Studente> studentiRepository = new Repository<Studente>();
  158.       Studente studente = studentiRepository.Create();
  159.       Console.ReadKey();
  160.  
  161.       Repository2<string, Studente2> studentiRepository2 = new Repository2<string, Studente2>();
  162.       Studente2 studente2 = studentiRepository2.Create();
  163.       Console.ReadKey();
  164.     }
  165.   }
  166. }
  167.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement