Beispiel #1
0
 /// <summary>
 /// Gets an observable sequence that retrieves terms from the storage.
 /// </summary>
 /// <param name="scheduler">A scheduler to invoke service operations on.</param>
 /// <returns>An observable sequence of portions of terms.</returns>
 public IObservable <IEnumerable <Term> > LoadTerms(IScheduler scheduler)
 {
     return(Observable
            .Return(Unit.Default, scheduler)
            .SelectMany(_ => this.LoadStorage().Descendants(TermXmlNames.TermElement))
            .Select(node => new Term(
                        node.Attribute(TermXmlNames.TermNameAttribute).Value,
                        node.Attribute(TermXmlNames.TermDefinitionAttribute).Value))
            .Do(term => XmlTermsService.ApplyTermRules(term, "term"))
            .Catch((Exception ex) => Observable.Throw <Term>(
                       XmlTermsService.WrapIntoInvalidStorageException(
                           ex,
                           String.Format(Resources.InvalidXmlStorageToLoad, this._fileName))))
            .ToArray());
 }
Beispiel #2
0
        /// <summary>
        /// Gets an observable that recreates storage with the specified terms.
        /// </summary>
        /// <param name="terms">A terms to add to a new storage.</param>
        /// <param name="scheduler">A scheduler to invoke service operations on.</param>
        /// <returns>An observable sequence that recreates storage.</returns>
        public IObservable <Unit> RecreateStorage(IEnumerable <Term> terms, IScheduler scheduler)
        {
            if (terms == null)
            {
                throw new ArgumentNullException("terms");
            }

            return(Observable
                   .Return(Unit.Default, scheduler)
                   .Do(_ =>
            {
                var doc = new XDocument(new XElement(TermXmlNames.TermsElement));

                foreach (var term in terms)
                {
                    XmlTermsService.ApplyTermRules(term, "terms");

                    var xTerm = new XElement(
                        TermXmlNames.TermElement,
                        new XAttribute(TermXmlNames.TermNameAttribute, term.Name));

                    if (!String.IsNullOrEmpty(term.Definition))
                    {
                        xTerm.Add(new XAttribute(TermXmlNames.TermDefinitionAttribute, term.Definition));
                    }

                    doc.Root.Add(xTerm);
                }

                this.SaveStorage(doc);
            })
                   .Catch((Exception ex) => Observable.Throw <Unit>(
                              XmlTermsService.WrapIntoInvalidStorageException(
                                  ex,
                                  String.Format(Resources.InvalidXmlStorageToSave, this._fileName)))));
        }