Le réseau pour les développeurs > Forums - Accueil > Visual C# General > Unit Of Work over Service with XMLDocument
Poser une questionPoser une question
 

TraitéeUnit Of Work over Service with XMLDocument

  • samedi 7 novembre 2009 20:45MDMoura Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Hello,

    I have 4 services that deal with XML data.

    I would like to create a InitOfWork pattern with Commit and Rollback methods so I could use as follows:

          ISession session = new Context(String path);
          AssetService assetService = new AssetService(session);
          DocumentService documentService = new DocumentService(session);
          assetService.Create(asset)
          documentService.DeleteById(1)
          session.Commit();

    So my idea is when I use session.Commit() the XML files used by each service would be saved.
    If I would choose Roolback the files would be disposed with no changes.

    Does this make sense or is possible in this case?

    A service example is as follows:

      public class AssetService {

        private XDocument _assets;

        // AssetService
        public AssetService(String path) {

          if (String.IsNullOrEmpty(path))
            throw new ArgumentNullException("path", "Path cannot be
    null");
          _assets = XDocument.Load(String.Concat(path, "Assets.xml"),
    LoadOptions.SetBaseUri);

        } // AssetService

        public void Create(Models.Asset asset) {

          Int32? id = _assets.Root.Elements("Asset").Select(s =>
    Int32.Parse(s.Element("Id").Value)).Aggregate((Int32?)null, (a, i) => !
    a.HasValue || a.Value < i ? i : a);
          XElement _asset = new XElement("Asset",
            new XElement("Id", id == null ? "1" : (id + 1).ToString()),
            new XElement("Content", asset.Content),
            new XElement("Locked", asset.Locked.ToString()),
            new XElement("Name", asset.Name)
          );
          _assets.Root.Add(_asset);
          _assets.Save(new Uri(_assets.BaseUri).LocalPath);

        } // Create

        public Models.Asset GetById(Int32 id) {

          return _assets.Root.Elements("Asset").Select(s => new
    Models.Asset {
            Id = Int32.Parse(s.Element("Id").Value),
            Content = s.Element("Content").Value,
            Locked = Boolean.Parse(s.Element("Locked").Value),
            Name = s.Element("Name").Value
          }).FirstOrDefault(s => s.Id == id);

        } // GetById

     }


    Thanks,
    Miguel

Réponses

  • lundi 9 novembre 2009 21:47OmegaManMVP, ModeratorMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée
    Does this make sense or is possible in this case?

    The question is across the board; hence why no responses. Rolling back of data is a good idea, so asking if it makes sense is moot.

     

    Whatever design will facilitate that rollback is unique to your situation and commenting on the validity of code will bare no fruit. Either implement a rollback, which yes I believe can be done with the right programmatic safeguards , and be done with it or abandon the idea. You may need a fifth service to manage such transactions though...(IMHO)

    GL HTH


    William Wegerson (www.OmegaCoder.Com)

Toutes les réponses

  • lundi 9 novembre 2009 21:47OmegaManMVP, ModeratorMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée
    Does this make sense or is possible in this case?

    The question is across the board; hence why no responses. Rolling back of data is a good idea, so asking if it makes sense is moot.

     

    Whatever design will facilitate that rollback is unique to your situation and commenting on the validity of code will bare no fruit. Either implement a rollback, which yes I believe can be done with the right programmatic safeguards , and be done with it or abandon the idea. You may need a fifth service to manage such transactions though...(IMHO)

    GL HTH


    William Wegerson (www.OmegaCoder.Com)