using MultiTerm.Core.Common; using MultiTerm.Core.ViewModel; namespace MultiTerm.Core.Factories; /// /// Can create instances during runtime. /// public class TerminalViewModelFactory : ITerminalViewModelFactory { private readonly Func> factory; /// /// Constructor of . /// /// function that gets all registered services with Interface public TerminalViewModelFactory(Func> factory) { this.factory = factory; } /// /// Creates a new . /// /// of the new . Must be set at creation time for all /// of the new /// public ITerminalViewModel Create(TerminalViewType viewType, ProtocolType protocolType) { ITerminalViewModel output; // get all registered TerminalViewModels as IEnumerable var registeredViewModels = factory(); // search for correct type by ViewType which must be set in constructor of all TerminalViewModel Types try { output = registeredViewModels.Where(x => x.ViewType == viewType).First(); } catch (Exception ex) { throw new NotImplementedException($"'{nameof(TerminalViewModelFactory)}' cannot create Terminal with {nameof(TerminalViewType)} {viewType}", ex); } // immediately initialize protocol type output.ProtocolType = protocolType; // return the filled up instance return output; } }