public void EnregistrerDessin(Dessin p_dessin)
                 {
                        XmlWriterSettings settings  =  new XmlWriterSettings();
                        settings.Indent             =  true;
                        settings.IndentChars        =   "\t";

                        using (XmlWriter doc  =  XmlWriter.Create(this.m_nomFichier,  settings))
                         {
                                XmlSerializer xmlSerializer  =  new XmlSerializer(typeof(DessinFormatAttributsDTO),
                                                                                                      new Type[]
                                                                                                       {
                                            typeof(PolygoneFormatAttributsDTO),
                                            typeof(CercleFormatAttributsDTO),
                                        
                });

                                 // Transformation de Dession vers DessinDTO
                                 // Méthode 1 : Manuelle
                                    DessinFormatAttributsDTO dessinDTO  =  new DessinFormatAttributsDTO();

                                dessinDTO.Formes  =  new List <FormeFormatAttributsDTO>();
                                foreach (Forme forme in p_dessin.Formes)
                                 {
                                        FormeFormatAttributsDTO formeDTO  =  null;
                                        switch (forme)
                                         {
                        case Polygone p:
                                                    formeDTO  =  new PolygoneFormatAttributsDTO()
                                                                                             {
                                                            Sommets  =  p.Sommets.Select(point2D  =>
                                                                                                                         new Point2DFormatAttributsDTO()
                                                                                                                          {
                                                                    X  =  point2D.X,
                                                                    Y  =  point2D.Y
                                                                                                         
                            }).ToList()
                                                                                                   
                        };
                                                    break;
                        case Cercle c:
                                                    Point2D centre  =  c.Centre;
                                                    formeDTO        =  new CercleFormatAttributsDTO()
                                                                                                   {
                                                            Centre  =  new Point2DFormatAttributsDTO()
                                                                                                       {
                                                                    X  =  centre.X,
                                                                    Y  =  centre.Y
                                                                                                         
                            },
                                                            Rayon  =  c.Rayon
                                                                                                 
                        };
                                                    break;
                        default:
                                                    break;
                                            
                    }

                                        dessinDTO.Formes.Add(formeDTO);
                                    
                }

                                 // Méthode 2 : Bibliothèque automapper
                                 //IMapper mapper = CreerOutilConversionEntity2DTO();
                                 //DessinDTO dessinDTO = mapper.Map<DessinDTO>(p_dessin);

                                doc.WriteStartDocument();

                                xmlSerializer.Serialize(doc,  dessinDTO);
                                doc.Close();
                            
            }
                    
        }
Exemplo n.º 2
0
        public void EnregistrerDessin(Dessin p_dessin)
        {
            XmlWriterSettings settings = new XmlWriterSettings();

            settings.Indent      = true;
            settings.IndentChars = "\t";

            using (XmlWriter doc = XmlWriter.Create(this.m_nomFichier, settings))
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(DessinFormatAttributsDTO),
                                                                new Type[]
                {
                    typeof(PolygoneFormatAttributsDTO),
                    typeof(CercleFormatAttributsDTO),
                });

                // Transformation de Dession vers DessinDTO
                // Méthode 1 : Manuelle
                DessinFormatAttributsDTO dessinDTO = new DessinFormatAttributsDTO();
                dessinDTO.Formes = new List <FormeFormatAttributsDTO>();
                foreach (Forme forme in p_dessin.Formes)
                {
                    FormeFormatAttributsDTO formeDTO = null;
                    switch (forme)
                    {
                    case Polygone p:
                        formeDTO = new PolygoneFormatAttributsDTO()
                        {
                            Sommets = p.Sommets.Select(point2D =>
                                                       new Point2DFormatAttributsDTO()
                            {
                                X = point2D.X,
                                Y = point2D.Y
                            }).ToList()
                        };
                        break;

                    case Cercle c:
                        Point2D centre = c.Centre;
                        formeDTO = new CercleFormatAttributsDTO()
                        {
                            Centre = new Point2DFormatAttributsDTO()
                            {
                                X = centre.X,
                                Y = centre.Y
                            },
                            Rayon = c.Rayon
                        };
                        break;

                    default:
                        break;
                    }

                    dessinDTO.Formes.Add(formeDTO);
                }

                // Méthode 2 : Bibliothèque automapper
                //IMapper mapper = CreerOutilConversionEntity2DTO();
                //DessinDTO dessinDTO = mapper.Map<DessinDTO>(p_dessin);

                doc.WriteStartDocument();
                xmlSerializer.Serialize(doc, dessinDTO);
                doc.Close();
            }
        }