Sorry, there are no translations available at the moment.

Sauvegarde et restauration

Description

Dans ce tutoriel, nous allons apprendre à sauvegarder et restaurer les modifications apportées aux différents objets. La sauvegarde applique les modifications à la base de données.

Solution de départ

La solution de départ est disponible ici

Démonstration

http://tjtechno.com/demo/sauvegarde_et_restauration/

Code source

Fichier SavingService.cs

 
#region using
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
using IdeaBlade.EntityModel;
using DomainModel;
 
#endregion
 
namespace WinForms01 {
  /// <summary>
  /// Service class to perform save
  /// </summary>
  /// <remarks>
  /// A primitive example of such a service.
  /// </remarks>
  public class SavingService {
 
    /// <summary>Ctor.</summary>
    /// <param name="entityManager">Provide Saving services for this <see cref="T:EntityManager"/>.</param>
    public SavingService(DomainModelEntityManager entityManager) {
      _entityManager = entityManager;
    }
 
    /// <summary>Save all entities with pending changes.</summary>
    public void SaveAll() {
      if (!_entityManager.HasChanges()) {
        DisplayNothingToSave();
        return;
      }
      while (SaveAllCore()) {
      }
    }
 
    private bool SaveAllCore() {
      try {
        _entityManager.Saving += SavingHandler;
        SaveResult result = _entityManager.SaveChanges();
        DisplaySaveSucceeded();
        return false;
      }
      catch (EntityManagerSaveException saveException) {
        MessageBox.Show("Exception thrown during save: " + saveException.Message);
        return false;
      }
      finally {
        _entityManager.Saving -= SavingHandler;
      }
    }
 
    #region Display Methods
 
    private static void DisplayNothingToSave() {
      MessageBox.Show("Nothing to save Succeeded", "Save cancelled");
    }
 
    private static void DisplaySaveSucceeded() {
      MessageBox.Show("Save Succeeded", "Saved");
    }
 
    [System.Diagnostics.DebuggerNonUserCode()]
    private static void DisplaySaveFailed(Exception e) {
      Exception anException = e.InnerException;
      if (anException == null) {
        anException = e; // just in case
      }
      string msgFmt =
        "Save failed due to exception of type '{0}':{1}{1}{2}";
      MessageBox.Show(
        String.Format(msgFmt, anException.GetType(), Environment.NewLine, anException.Message),
        "Save failed");
    }
    #endregion
 
    #region SavingHandler and related methods
    [System.Diagnostics.DebuggerNonUserCode()]
    protected virtual void SavingHandler(object sender, EntitySavingEventArgs e) {
      CheckIfSomethingToSave(e);
    }
 
    [System.Diagnostics.DebuggerNonUserCode()]
    private static void CheckIfSomethingToSave(EntitySavingEventArgs e) {
      if (e.Entities.Count == 0) {
        throw new NothingToSaveException();
      }
    }
 
    #endregion
 
    #region Exception classes
 
    /// <summary>Nothing-to-Save exception.</summary>
    public class NothingToSaveException : Exception {
      public NothingToSaveException()
        : base("Nothing to save.") {
      }
    }
 
    /// <summary>Entity validation exception.</summary>
    public class ValidationException : Exception {
      public ValidationException(string pMessage)
        : base(pMessage) {
      }
    }
    #endregion
 
    #region Private Fields
    private DomainModelEntityManager _entityManager;
    #endregion Private Fields
 
  }
}        
 

Solution finale

Disponible ici

Liens

Gestion des exceptions

Opérateurs C#