• C#学习——序列化和反序列化练习的重构


    目的

           代码的重构!我上一次写的随笔“C#学习——序列化和反序列化”对接口的理解似乎有误,接口基本上没进行什么设计,之后虽然也重新设计了一下,

    但是写了两个接口IDeserialize和ISerialize每个接口里面只有一方法,分别为反序列化和序列化,有点感觉过度设计,所以现在写了一篇

    C#学习——序列化和反序列化练习的重构,来改正这一错误,这一例子觉得有一些地方写的不妥,希望大家觉得哪里不顺眼,踊跃发言哈!

     

    示例:

     

           1、新建解决方案Serializer

     

          2、新建类库Jack.Gao.Domain,并在该类库下添加Person类,Person类的代码如下:

     

    View Code
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Runtime.Serialization.Formatters;
     6 
     7 namespace Jack.Gao.Domain
     8 {
     9     [Serializable]
    10     public class Person
    11     {
    12         //如果不想实例化某一个字段,可以使用特性NonSerialized标记
    13         private int _id;
    14         public int Id
    15         {
    16             get { return _id; }
    17             set { _id = value; }
    18         }
    19 
    20         private string _name;
    21         public string Name
    22         {
    23             get { return _name; }
    24             set { _name = value; }
    25         }
    26 
    27         private string _sex;
    28         public string Sex
    29         {
    30             get { return _sex; }
    31             set { _sex = value; }
    32         }
    33     }
    34 }

     

          3、新建类库Jack.Gao.PersonFactory,在该库下新建Factory类,用于生成Person。代码如下:

     

    View Code
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 using Jack.Gao.Domain;
     7 
     8 namespace Jack.Gao.PersonFactory
     9 {
    10     public class Factory
    11     {
    12         #region Create Person
    13         public static Person CreatePerson()
    14         {
    15             return new Person();
    16         }
    17         #endregion       
    18     }
    19 }

     

              添加枚举类型SerializerType,代码如下:

     

    View Code
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace Jack.Gao.PersonFactory
     7 {
     8     public enum SerializerType
     9     {
    10         Xml,
    11         Binary,
    12         Soap
    13     }
    14 }

     

                再新建一个类SerializerTypeFactory,用于根据类型生成某一特点的序列化方式。不过这里写的代码感觉不想面向对象的,写的有点烂。添加如下代码:

     

    View Code
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.IO;
     6 using System.Runtime.Serialization.Formatters.Binary;
     7 using System.Xml.Serialization;
     8 using System.Diagnostics;
     9 using System.Runtime.Serialization.Formatters.Soap;
    10 using Jack.Gao.Domain;
    11 
    12 namespace Jack.Gao.PersonFactory
    13 {
    14     public static class SerializerTypeFactory
    15     {
    16         public static object CreateSerializerType(SerializerType serializerType)
    17         {
    18             object serializer=null;
    19 
    20             switch (serializerType)
    21             {
    22                 case SerializerType.Xml:
    23                      serializer = new XmlSerializer(typeof(Person));
    24                      break;
    25                 case SerializerType.Binary:
    26                      serializer = new BinaryFormatter();
    27                      break;
    28                 case SerializerType.Soap:
    29                      serializer = new SoapFormatter();
    30                      break;
    31                 default:
    32                     throw new Exception("Type is not correctly!");
    33             }
    34 
    35             return serializer;
    36         }
    37     }
    38 }

     

           4、新建类库Jack.Gao.Persistence,新建

    ISerialize接口、SerializeBase抽象类、BinarySerializePerson、SoapSerializePerson、XmlSerializePerson这三个类分别实现了ISerialize接口。代码如下:

    SerializeBase:

    View Code
     1 using System.IO;
     2 using Jack.Gao.Domain;
     3 using Jack.Gao.PersonFactory;
     4 
     5 namespace Jack.Gao.Persistence.Base
     6 {
     7     public abstract class SerializeBase
     8     {
     9         protected Person PersonBase { get; set; }
    10 
    11         protected virtual FileStream FileStreamBase { get; set; }
    12 
    13         public SerializeBase()
    14         {
    15             PersonBase = Factory.CreatePerson();
    16         }
    17 
    18         //public virtual void SerializePerson()
    19         //{
    20             
    21         //}
    22 
    23         //public virtual Person DeSerializePerson()
    24         //{
    25         //    return new Person();
    26         //}
    27     }
    28 }

    ISerialize:

    View Code
     1 using Jack.Gao.Domain;
     2 
     3 namespace Jack.Gao.Persistence.Interface
     4 {
     5     interface ISerialize
     6     {
     7         void SerializePerson();
     8         Person DeSerializePerson();
     9     }
    10 }

    BinarySerializePerson:

    View Code
     1 using System.IO;
     2 using System.Runtime.Serialization.Formatters.Binary;
     3 using Jack.Gao.Domain;
     4 using Jack.Gao.Persistence.Base;
     5 using Jack.Gao.Persistence.Interface;
     6 using Jack.Gao.PersonFactory;
     7 
     8 namespace Jack.Gao.Persistence.Implement
     9 {
    10     public class BinarySerializePerson : SerializeBase,ISerialize
    11     {
    12         public void SerializePerson()
    13         {
    14             PersonBase.Id = 2;
    15             PersonBase.Name = "Jim";
    16             PersonBase.Sex = "Man";
    17 
    18             using (FileStreamBase = new FileStream("Person.txt", FileMode.Create))
    19             {
    20                 BinaryFormatter binaryFormatter = (BinaryFormatter)SerializerTypeFactory.CreateSerializerType
    21                     (SerializerType.Binary);
    22                 binaryFormatter.Serialize(FileStreamBase, PersonBase);
    23             }
    24         }
    25 
    26         public Person DeSerializePerson()
    27         {
    28             using (FileStreamBase = new FileStream("Person.txt", FileMode.Open))
    29             {
    30                 BinaryFormatter binaryFormatter =
    31                     (BinaryFormatter)SerializerTypeFactory.CreateSerializerType(SerializerType.Binary);
    32                 PersonBase = (Person)binaryFormatter.Deserialize(FileStreamBase);
    33             }
    34 
    35             return PersonBase;
    36         }
    37     }
    38 }

    SoapSerializePerson:

    View Code
     1 using System.IO;
     2 using System.Runtime.Serialization.Formatters.Soap;
     3 using Jack.Gao.Domain;
     4 using Jack.Gao.Persistence.Base;
     5 using Jack.Gao.Persistence.Interface;
     6 using Jack.Gao.PersonFactory;
     7 
     8 namespace Jack.Gao.Persistence.Implement
     9 {
    10     public class SoapSerializePerson : SerializeBase,ISerialize
    11     {
    12         public void SerializePerson()
    13         {
    14             PersonBase.Id = 3;
    15             PersonBase.Name = "Green";
    16             PersonBase.Sex = "Woman";
    17 
    18             using (FileStreamBase = new FileStream("Person.xml", FileMode.Create))
    19             {
    20                 SoapFormatter soapFormatter = (SoapFormatter)SerializerTypeFactory.
    21                     CreateSerializerType(SerializerType.Soap);
    22                 soapFormatter.Serialize(FileStreamBase, PersonBase);
    23             }
    24         }
    25 
    26         public Person DeSerializePerson()
    27         {
    28             using (FileStreamBase = new FileStream("Person.xml", FileMode.Open))
    29             {
    30                 SoapFormatter soapFormatter = (SoapFormatter)SerializerTypeFactory.
    31                     CreateSerializerType(SerializerType.Soap);
    32                 PersonBase = (Person)soapFormatter.Deserialize(FileStreamBase);
    33             }
    34 
    35             return PersonBase;
    36         }
    37     }
    38 }

    XmlSerializePerson:

    View Code
     1 using System.IO;
     2 using System.Xml.Serialization;
     3 using Jack.Gao.Domain;
     4 using Jack.Gao.Persistence.Base;
     5 using Jack.Gao.Persistence.Interface;
     6 using Jack.Gao.PersonFactory;
     7 
     8 namespace Jack.Gao.Persistence.Implement
     9 {
    10     public class XmlSerializePerson : SerializeBase,ISerialize
    11     {
    12         public void SerializePerson()
    13         {
    14             using (FileStreamBase= new FileStream("Person.xml", FileMode.Create))
    15             {
    16                 PersonBase.Id = 1;
    17                 PersonBase.Name = "Jack Gao";
    18                 PersonBase.Sex = "Man";
    19 
    20                 XmlSerializer xmlSerializer = (XmlSerializer)SerializerTypeFactory.
    21                     CreateSerializerType(SerializerType.Xml);
    22                 xmlSerializer.Serialize(FileStreamBase, PersonBase);
    23             }
    24         }
    25 
    26         public Person DeSerializePerson()
    27         {
    28             using (FileStreamBase = new FileStream("Person.xml", FileMode.Open))
    29             {
    30                 XmlSerializer xmlSerializer = (XmlSerializer)SerializerTypeFactory.
    31                     CreateSerializerType(SerializerType.Xml);
    32                 PersonBase = (Person)xmlSerializer.Deserialize(FileStreamBase);
    33             }
    34 
    35             return PersonBase;
    36         }
    37     }
    38 }

          5、新建控制台输出程序Jack.Gao.AppSerializer,在Program.cs添加如下代码:

     

    View Code
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.IO;
     6 using System.Diagnostics;
     7 
     8 using Jack.Gao.Domain;
     9 using Jack.Gao.Persistence;
    10 using Jack.Gao.Persistence.Implement;
    11 
    12 namespace Jack.Gao.AppSerializer
    13 {
    14     class Program
    15     {
    16         private static Person _person=null;
    17 
    18         static void Main(string[] args)
    19         {
    20             //XmlSerializePerson xmlSerializePerson = new XmlSerializePerson();
    21             //xmlSerializePerson.SerializePerson();
    22             //ShowIsSuccess();
    23 
    24             //_person = xmlSerializePerson.DeSerializePerson();
    25             //Console.WriteLine(_person.Id);
    26             //Console.WriteLine(_person.Name);
    27             //Console.WriteLine(_person.Sex);
    28 
    29             BinarySerializePerson binarySerializePerson = new BinarySerializePerson();
    30             binarySerializePerson.SerializePerson();
    31             ShowIsSuccess();
    32 
    33             _person = binarySerializePerson.DeSerializePerson();
    34             Console.WriteLine(_person.Id);
    35             Console.WriteLine(_person.Name);
    36             Console.WriteLine(_person.Sex);
    37 
    38             //SoapSerializePerson soapSerializePerson = new SoapSerializePerson();
    39             //soapSerializePerson.SerializePerson();
    40             //ShowIsSuccess();
    41 
    42             //_person = soapSerializePerson.DeSerializePerson();
    43             //Console.WriteLine(_person.Id);
    44             //Console.WriteLine(_person.Name);
    45             //Console.WriteLine(_person.Sex);
    46 
    47             Console.ReadKey();
    48         }
    49 
    50         private static void ShowIsSuccess()
    51         {
    52             if (IsExitFile())
    53             {
    54                 Console.WriteLine("Create File sucessfull.");
    55             }
    56             else
    57             {
    58                 Console.WriteLine("Create File error.");
    59             }
    60         }
    61 
    62         private static bool IsExitFile()
    63         {
    64             bool exitXmlFile = false;
    65 
    66             string currentApplicationPath = Process.GetCurrentProcess().MainModule.FileName;
    67 
    68             if (!string.IsNullOrEmpty(currentApplicationPath))
    69             {
    70                 exitXmlFile = true;
    71             }
    72 
    73             return exitXmlFile;
    74         }
    75     }
    76 }

     

               运行该程序,可以看到在输出目录下多了一个Person.xml的文件,可以查看里面的内容,以下内容使用soap序列化后生成的,你也可以查看其它的两种方式生成的内容。

     

     

              

     

  • 相关阅读:
    Python爬虫学习遇到的问题
    爬取网页内容后写入文件报错UnicodeEncodeError: 'gbk' codec can't encode的问题解决方案
    第14.6节 使用Python urllib.request模拟浏览器访问网页的实现代码
    Mina源码阅读笔记(一)-整体解读
    使用异步 I/O 大大提高应用程序的性能
    同步和异步,阻塞和非阻塞
    位运算详解
    php引用传值详解
    PHP获取指定地区的天气
    新闻网站开发-手机端-基于Wordpress
  • 原文地址:https://www.cnblogs.com/GaoHuhu/p/2621708.html
Copyright © 2020-2023  润新知