S
shapper
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.
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
}
How can I create this UnitOfWork?
Thanks,
Miguel
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.
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
}
How can I create this UnitOfWork?
Thanks,
Miguel