HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Search on List<Tree-like structure>

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
searchlikestructurelisttree

Problem

I create a data collection system that has a tree-like structure built on the similarity to the pattern of the factory, and I have the difficulty in working with this structure, more stingrays lot of code to find the element opredelnie.

```
public interface ITag : IRegister
{
string Name { get; set; }
string SystemName { get; }
}

public interface ISignal : IRegister
{
}

public interface IRegister
{
Type GetType { get; }
}

public interface IGroup : IRegister
{
string Name { get; set; }
string SystemName { get; }
}

public interface IDevice : IRegister
{
string Name { get; set; }
string SystemName { get; }
}

public interface IServer : IRegister
{
}

public interface INode : IRegister
{
string Name { get; set; }
string SystemName { get; }
}

[Serializable]
public class Server : IServer
{
public string Name;
public string SystemName;
public List Nodes;

public Server(string name, List node)
{
Name = name;
Nodes = node;
SystemName = "Server";
}
public Type GetType
{
get { return typeof(Server); }
}
}

[Serializable]
public class TCP : INode
{
public string IPAddress;
public int Port;
public List Nodes;
public string Name { get; set; }
public string SystemName { get; }

public TCP(string name, string ip, int port, List devices)
{
Name = name;
IPAddress = ip;
Port = port;
Nodes = devices;
SystemName = "Node";
}

public TCP()
{
}

public Type GetType
{
get { return typeof(TCP); }
}
}

[Serializable]
public class RTU : INode
{
public string Name { get; set; }
public string SystemName { get; }
public string Port;
public int SpeedRate;
public int DataBits;
public int StopBits;
public Parity Parity;
public List Devices;

public RTU(string name, int sr, int db, int sb, Parity par, string port, List devices)
{
Name = name;

Solution

If you extend the INode interface to also has a property returning a List like so

public interface INode : IRegister
{
    string Name { get; set; }
    string SystemName { get; }
    List Devices { get; }
}


you could remove a lot of code duplication because you wouldn't need to distinguish between TCP and RTU.

By adding a method GetTags() like so

private IEnumerable GetTags(Server server, string tagName, Type groupType)
{
    foreach (INode node in server.Nodes)
    {
        foreach (IDevice device in node.Devices)
        {
            foreach (IGroup group in device.Groups.Where(g => g.GetType() == groupType))
            {
                foreach (ITag tag in group.Tags.Where(t => t.Name == tagName))
                {
                    yield return tag;
                }
            }
        }
    }
}


your former loop could look like so

Type groupType = typeof(Group);
String tagName = e.Node.Text;

foreach(Server server in List)
{
    foreach(ITag tag in GetTags(server, tagName, groupType))
    {
        tag.Name = newname;
    }  
}


you see I have used the GetType() method of the object class. The provided GetType property of the IRegister interface will raise a lot of warnings, because it hides the inherited member objectGetType().

Code Snippets

public interface INode : IRegister
{
    string Name { get; set; }
    string SystemName { get; }
    List<IDevice> Devices { get; }
}
private IEnumerable<ITag> GetTags(Server server, string tagName, Type groupType)
{
    foreach (INode node in server.Nodes)
    {
        foreach (IDevice device in node.Devices)
        {
            foreach (IGroup group in device.Groups.Where(g => g.GetType() == groupType))
            {
                foreach (ITag tag in group.Tags.Where(t => t.Name == tagName))
                {
                    yield return tag;
                }
            }
        }
    }
}
Type groupType = typeof(Group);
String tagName = e.Node.Text;

foreach(Server server in List)
{
    foreach(ITag tag in GetTags(server, tagName, groupType))
    {
        tag.Name = newname;
    }  
}

Context

StackExchange Code Review Q#106095, answer score: 4

Revisions (0)

No revisions yet.