IEntryDecoration
IEntryDecoration indicates that entry is a decoration of another entry.
/// <summary>Decorated <see cref="IEntry"/> that contains a value that is derived of other source.</summary>
public interface IEntryDecoration : IEntry
{
/// <summary>Determines dispose behaviour of <see cref="Source"/>. If set to true, <see cref="Source"/> is disposed along with decoration</summary>
bool DisposeUnderlying { get; set; }
/// <summary>The underlying source <see cref="IEntry"/> of this decoration is derived of.</summary>
IEntry? Source { get; }
}
IEntryDecorator decorates a entry in some specific way.
/// <summary>Decorates <see cref="IEntry"/>s in an unspecified way.</summary>
public interface IEntryDecorator
{
/// <summary>Decorate <paramref name="entry"/></summary>
/// <param name="entry"></param>
/// <returns>Decorated version of <paramref name="entry"/>. May or may not implement <see cref="IEntryDecoration"/>.</returns>
IEntry Decorate(IEntry entry);
}
Decorator applies a decoration to a entry.
// Create entry
IEntry entry = new Entry<string>();
// Create decorator
IEntryDecorator decorator = new LoggingDecorator(Console.Out);
// Decorate entry to log
entry = decorator.Decorate(entry);
// Assign value
entry.SetValue("Value");
public class LoggingDecorator : IEntryDecorator
{
static ConstructorT<IEntry, TextWriter, IEntry> ctor = new(typeof(LoggingDecoration<>));
TextWriter writer;
public LoggingDecorator(TextWriter writer) => this.writer = writer;
public IEntry Decorate(IEntry entry)
{
StructList2<Type> types = ((IEntryReadable)entry).ValueTypes<StructList2<Type>>();
IEntry decoration = ctor.Create(types[0], entry, writer);
return decoration;
}
}
public class LoggingDecoration<T> : EntryDecoration<T>
{
TextWriter writer;
public LoggingDecoration(IEntry source, TextWriter writer) : base(source, true)
{
this.source = source;
this.writer = writer;
}
public override bool TrySetValue(object? value)
{
writer.WriteLine(value);
return base.TrySetValue(value);
}
}
Full Example
Full example
using System;
using System.IO;
using Avalanche.Service;
using Avalanche.Utilities;
public class entry_decoration
{
public static void Run()
{
{
// <01>
// Create entry
IEntry entry = new Entry<string>();
// Create decorator
IEntryDecorator decorator = new LoggingDecorator(Console.Out);
// Decorate entry to log
entry = decorator.Decorate(entry);
// Assign value
entry.SetValue("Value");
// </01>
}
}
// <98>
public class LoggingDecorator : IEntryDecorator
{
static ConstructorT<IEntry, TextWriter, IEntry> ctor = new(typeof(LoggingDecoration<>));
TextWriter writer;
public LoggingDecorator(TextWriter writer) => this.writer = writer;
public IEntry Decorate(IEntry entry)
{
StructList2<Type> types = ((IEntryReadable)entry).ValueTypes<StructList2<Type>>();
IEntry decoration = ctor.Create(types[0], entry, writer);
return decoration;
}
}
// </98>
// <99>
public class LoggingDecoration<T> : EntryDecoration<T>
{
TextWriter writer;
public LoggingDecoration(IEntry source, TextWriter writer) : base(source, true)
{
this.source = source;
this.writer = writer;
}
public override bool TrySetValue(object? value)
{
writer.WriteLine(value);
return base.TrySetValue(value);
}
}
// </99>
}