Ejemplo n.º 1
0
        protected PersonBase(Workspace workspace, Action <ElementType, string> onCreatedFromExistingElement)
            : base(workspace.Views.Configuration.Styles, onCreatedFromExistingElement)
        {
            Workspace = workspace;
            if (!TryGetMetadata(out string description, out Location location))
            {
                throw new System.Exception($"Missing PersonAttribute on Person {GetType().Name}");
            }

            StyleContainer = workspace.Views.Configuration.Styles;
            Styles         = new ElementStyleBase[0];

            var name   = NamedIdentity.GetNameFromType(GetType());
            var person = workspace.Model.GetPersonWithName(name);

            if (person == null)
            {
                person = workspace.Model.AddPerson(location, name, description);
            }
            else
            {
                onCreatedFromExistingElement(ElementType.Element, person.Id);
                person.Location    = location;
                person.Description = description;
            }
            Element = Me = person;
        }
        private static void ConnectToComponent(Connector connector, StaticStructureElement fromElement)
        {
            var connectedSystem = _workspace.Model.SoftwareSystems.SelectMany(x => x.Containers)
                                  .SelectMany(x => x.Components)
                                  .FirstOrDefault(x => x.Name == NamedIdentity.GetNameFromType(connector.ConnectTo));

            if (connectedSystem == null)
            {
                return;
            }

            var ctor = connector.RelationshipStyle.GetConstructor(new[] { typeof(Structurizr.Styles), typeof(Action <ElementType, string>) });

            if (ctor == null)
            {
                return;
            }
            Action <ElementType, string> callback = OnCreatedFromExistingElement;
            var connectorStyle = (RelationshipStyleBase)ctor.Invoke(new object[] { _workspace.Views.Configuration.Styles, callback });

            var relationship = fromElement.Relationships.FirstOrDefault(x => x.DestinationId == connectedSystem.Id)
                               ?? fromElement.Uses(connectedSystem, connector.Description, connector.Technology, connectorStyle.InteractionStyle);

            relationship.AddUniqueTag(connectorStyle.GetRelationshipStyle().Tag);
        }
        private static List <SoftwareSystemBase> ConnectToSoftwareSystem(Connector connector, StaticStructureElement fromElement)
        {
            var softwareSystemDependencies = new List <SoftwareSystemBase>();
            var connectedSystem            =
                _workspace.Model.GetSoftwareSystemWithName(NamedIdentity.GetNameFromType(connector.ConnectTo));

            if (connectedSystem == null)
            {
                //possibly from another library: for now only allow SoftwareSystem (loose containers and components are not allowed)
                if (!typeof(SoftwareSystemBase).IsAssignableFrom(connector.ConnectTo))
                {
                    return(softwareSystemDependencies);
                }

                var softwareSystem = CreateExternalSoftwareSystem(connector.ConnectTo);
                softwareSystemDependencies.Add(softwareSystem);
                connectedSystem = softwareSystem.Me;
            }

            var ctor = connector.RelationshipStyle.GetConstructor(new[] { typeof(Structurizr.Styles), typeof(Action <ElementType, string>) });

            if (ctor == null)
            {
                return(softwareSystemDependencies);
            }
            Action <ElementType, string> callback = OnCreatedFromExistingElement;
            var connectorStyle = (RelationshipStyleBase)ctor.Invoke(new object[] { _workspace.Views.Configuration.Styles, callback });

            var relationship = fromElement.Relationships.FirstOrDefault(x => x.DestinationId == connectedSystem.Id)
                               ?? fromElement.Uses(connectedSystem, connector.Description, connector.Technology, connectorStyle.InteractionStyle);

            relationship.AddUniqueTag(connectorStyle.GetRelationshipStyle().Tag);
            return(softwareSystemDependencies);
        }
Ejemplo n.º 4
0
        protected SoftwareSystemBase(Workspace workspace, Action <ElementType, string> onCreatedFromExistingElement)
            : base(workspace.Views.Configuration.Styles, onCreatedFromExistingElement)
        {
            Workspace = workspace;
            _onCreatedFromExistingElement = onCreatedFromExistingElement;

            Styles     = new ElementStyleBase[0];
            Containers = new ContainerBase[0];

            if (!TryGetMetadata(out string description, out Location location))
            {
                throw new System.Exception($"Missing SoftwareSystemAttribute on SoftwareSystem {GetType().Name}");
            }
            StyleContainer = workspace.Views.Configuration.Styles;
            var name           = NamedIdentity.GetNameFromType(GetType());
            var softwareSystem = workspace.Model.GetSoftwareSystemWithName(name);

            if (softwareSystem == null)
            {
                softwareSystem = workspace.Model.AddSoftwareSystem(location, name, description);
            }
            else
            {
                onCreatedFromExistingElement(ElementType.Element, softwareSystem.Id);
                softwareSystem.Location    = location;
                softwareSystem.Description = description;
            }
            Element = Me = softwareSystem;
        }
        protected ContainerBase(SoftwareSystem softwareSystem, Structurizr.Styles styleContainer, Action <ElementType, string> onCreatedFromExistingElement)
            : base(styleContainer, onCreatedFromExistingElement)
        {
            if (!TryGetMetadata(out string description, out string technology))
            {
                throw new System.Exception($"Missing ContainerAttribute on Container class {GetType().Name}");
            }

            SoftwareSystem = softwareSystem;
            StyleContainer = styleContainer;
            _onCreatedFromExistingElement = onCreatedFromExistingElement;

            Components = new ComponentBase[0];
            Styles     = new ElementStyleBase[0];

            var name      = NamedIdentity.GetNameFromType(GetType());
            var container = softwareSystem.GetContainerWithName(name);

            if (container == null)
            {
                container = softwareSystem.AddContainer(name, description, technology);
            }
            else
            {
                _onCreatedFromExistingElement(ElementType.Element, container.Id);
                container.Description = description;
                container.Technology  = technology;
            }
            Element = Me = container;
        }
        protected ComponentBase(Container container, Structurizr.Styles styleContainer, Action <ElementType, string> onCreatedFromExistingElement)
            : base(styleContainer, onCreatedFromExistingElement)
        {
            if (!TryGetMetadata(out string description, out string technology, out string type))
            {
                throw new System.Exception($"Missing ComponentAttribute on Component {GetType().Name}");
            }

            StyleContainer = styleContainer;
            Styles         = new ElementStyleBase[0];

            var name      = NamedIdentity.GetNameFromType(GetType());
            var component = container.GetComponentWithName(name);

            if (component == null)
            {
                if (type.Length > 0)
                {
                    try
                    {
                        component = container.AddComponent(name, description, technology, type);
                    }
                    catch (System.ArgumentOutOfRangeException)
                    {
                        throw new System.Exception($"The type parameter in the ComponentAttribute on {name} has an invalid value");
                    }
                }
                else
                {
                    component = container.AddComponent(name, description, technology);
                }
            }
            else
            {
                onCreatedFromExistingElement(ElementType.Element, component.Id);
                component.Description = description;
                component.Technology  = technology;
                component.Type        = type;
            }
            Element = Me = component;
        }