コード例 #1
0
 public Guid?CreateStudent(Student student)
 {
     if (student != null)
     {
         var incomingStudent = student;
         var students        = GetAllStudents();
         if (students.Count() >= _configureService.GetMaxLength())
         {
             if (students.Count() == _configureService.GetMaxLength())
             {
                 students.RemoveFirst();
                 incomingStudent.Id = Guid.NewGuid();
                 students.AddLast(incomingStudent);
             }
         }
         else
         {
             incomingStudent.Id = Guid.NewGuid();
             students.AddLast(incomingStudent);
         }
         _fileService.WriteToFile(students);
         return(incomingStudent.Id);
     }
     return(null);
 }
コード例 #2
0
 public LinkedList <Student> ReadFromFile()
 {
     if (File.Exists(GetFullPath()))
     {
         using (StreamReader streamReader = new StreamReader(GetFullPath()))
         {
             if (_configureService.GetDocumentType().Equals("json"))
             {
                 string file           = streamReader.ReadToEnd();
                 var    readedStudents = JsonConvert.DeserializeObject <LinkedList <Student> >(file);
                 var    result         = readedStudents ?? new LinkedList <Student>();
                 var    itemsToDelete  = result.Count - _configureService.GetMaxLength();
                 while (itemsToDelete-- > 0)
                 {
                     result.RemoveFirst();
                 }
                 return(result);
             }
             else
             {
                 var serializer = new XmlSerializer(typeof(List <Student>));
                 using (var xmlReader = new XmlTextReader(streamReader))
                 {
                     if (!serializer.CanDeserialize(xmlReader))
                     {
                         return(new LinkedList <Student>());
                     }
                     var readedStudents  = serializer.Deserialize(xmlReader) as List <Student>;
                     var temporaryResult = readedStudents ?? new List <Student>();
                     var result          = new LinkedList <Student>(temporaryResult);
                     var itemsToDelete   = result.Count - _configureService.GetMaxLength();
                     while (itemsToDelete-- > 0)
                     {
                         result.RemoveFirst();
                     }
                     return(result);
                 }
             }
         }
     }
     else
     {
         if (_configureService.GetDocumentType().Equals("json"))
         {
             using (FileStream fstream = File.Create(GetFullPath()))
             {
             }
         }
         if (_configureService.GetDocumentType().Equals("xml"))
         {
             new XDocument(new XElement("root")).Save(GetFullPath());
         }
         return(new LinkedList <Student>());
     }
 }