Microsoft 开发人员网络 > 论坛主页 > Visual C# General > Unit Of Work over Service with XMLDocument
提出问题提出问题
 

已答复Unit Of Work over Service with XMLDocument

  • 2009年11月7日 20:45MDMoura 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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

答案

  • 2009年11月9日 21:47OmegaManMVP, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    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)

全部回复

  • 2009年11月9日 21:47OmegaManMVP, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    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)