public async Task <IComponent> GetComponentInterfaceAsync <T>(Component parentComponent, string clientRef) where T : class//, IComponent { var executionThread = parentComponent.executionThread; var executingComponent = executionThread.ExecutingComponents.FirstOrDefault( e => e.ParentExecutingID == parentComponent.TXID.xid && e.ClientRef == clientRef ); var childComponentWithClientRefNotInThread = (executingComponent == null); if (childComponentWithClientRefNotInThread) { var componentInterfaceFullname = typeof(T).FullName; var registeredComponent = await AppRegisterApiClient.GetComponentByInterfaceFullName(componentInterfaceFullname); if (registeredComponent == null) { throw new Exception($"Component interface '{componentInterfaceFullname}' is not registered."); } var parentExecutingComponent = executionThread.ExecutingComponents.FirstOrDefault(e => e.ExecutingID == parentComponent.TXID.xid); if (parentExecutingComponent == null) { throw new Exception($"Exececuting component with id'{parentComponent.TXID.xid}' not found in execution thread with id'{parentComponent.TXID.tid}."); } if (registeredComponent == null) { throw new Exception($"Component interface '{componentInterfaceFullname}' is not registered."); } var componentInterfaceName = componentInterfaceFullname.Split('.').LastOrDefault(); int nextExecutingID = executionThread.ExecutingComponents.Count(); // Zero based. var nextTXID = new TXID(executionThread.ID, nextExecutingID).ToString(); executingComponent = new ExecutingComponent() { ExecutingID = nextExecutingID, InterfaceFullname = componentInterfaceFullname, TypeFullname = typeof(T).FullName, URL = $"{registeredComponent.DomainName}/{registeredComponent.PrimaryAppRoute}/{nextTXID}", Breadcrumb = $"{parentExecutingComponent.Breadcrumb}/{componentInterfaceName}({nextExecutingID})", ClientRef = clientRef, ParentExecutingID = parentExecutingComponent.ExecutingID, State = null, Title = registeredComponent.Title }; executionThread.ExecutingComponents.Add(executingComponent); executionThread.ComponentExecutingID = nextExecutingID; executionThread.ExecutingComponentTitle = registeredComponent.Title; } T proxy; IComponent component; if (childComponentWithClientRefNotInThread) { proxy = CoreFunctions.CreateProxy <T>(typeof(IComponent)); component = proxy as IComponent; component.TXID = new TXID(executionThread.ID, executingComponent.ExecutingID); } else { var stateJson = JsonConvert.SerializeObject(executingComponent.State); proxy = CoreFunctions.CreateProxy <T>(stateJson, typeof(IComponent)); component = proxy as IComponent; } return(component); }
public async Task <T> GetComponentAsync <T>(Component parentComponent, string clientRef) where T : Component, new() { var t = new T(); var component = t as Component; if (component == null) { return(null); } var executionThread = parentComponent.executionThread; var executingComponent = executionThread.ExecutingComponents.FirstOrDefault( e => e.ParentExecutingID == parentComponent.TXID.xid && e.ClientRef == clientRef ); var componentInterfaceFullname = typeof(T).GetCustomAttribute <ComponentInterfaceAttribute>()?.Type.FullName; // TODO: Cater for unregistered components! var registeredComponent = await AppRegisterApiClient.GetComponentByInterfaceFullName(componentInterfaceFullname); if (registeredComponent == null) { throw new Exception($"Component interface '{componentInterfaceFullname}' is not registered."); } if (executingComponent == null) { var parentExecutingComponent = executionThread.ExecutingComponents.FirstOrDefault(e => e.ExecutingID == parentComponent.TXID.xid); if (parentExecutingComponent == null) { throw new Exception($"Exececuting component with id'{parentComponent.TXID.xid}' not found in execution thread with id'{parentComponent.TXID.tid}."); } if (registeredComponent == null) { throw new Exception($"Component interface '{componentInterfaceFullname}' is not registered."); } var componentInterfaceName = componentInterfaceFullname.Split('.').LastOrDefault(); int nextExecutingID = executionThread.ExecutingComponents.Count(); // Zero based. var nextTXID = new TXID(executionThread.ID, nextExecutingID).ToString(); executingComponent = new ExecutingComponent() { ExecutingID = nextExecutingID, InterfaceFullname = componentInterfaceFullname, TypeFullname = t.GetType().FullName, URL = $"{registeredComponent.DomainName}/{registeredComponent.PrimaryAppRoute}/{nextTXID}", Breadcrumb = $"{parentExecutingComponent.Breadcrumb}/{componentInterfaceName}({nextExecutingID})", ClientRef = clientRef, ParentExecutingID = parentExecutingComponent.ExecutingID, State = null, Title = registeredComponent.Title }; executionThread.ExecutingComponents.Add(executingComponent); executionThread.ComponentExecutingID = nextExecutingID; executionThread.ExecutingComponentTitle = registeredComponent.Title; } component.executionThread = executionThread; component.TXID = new TXID(executionThread.ID, executingComponent.ExecutingID); component.ClientRef = executingComponent.ClientRef; component.State = executingComponent.State; return(t); }