Click here to Skip to main content
15,888,257 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have some config

C#
<Databases>
<Database Name= "A" DataSource="B">
<Tables>
<Table Schema="C" Table="D" />
<Table Schema="E" Table="F" />
</Tables>
</Database>
<Database Name= "E" DataSource="F">
<Tables>
<Table Schema="C" Table="D" />
<Table Schema="E" Table="F" />
</Tables>
</Database>
</Databases>

I want the Table class to be and to reference the Database class, so that it can reference the DataSource and other properties

What I have tried:

I have built the code, and it works on the first loop, but doesn't on the second. I have swapped the order of params in the config and it always goes wrong on the second pass

The code used is all managed so I cannot step through it, but when running the first loop the database object is passed through to each of the tables, but on the second loop it doesn't seem to create the database object and it has null passed through.


Ideally it would be great if someone is able to provide some code (c#) that allows the parent object to be visible in the child.

The above is a simplified view there are more properties and lots more tables and databases.
Posted
Updated 23-Jun-20 3:31am
Comments
Maciej Los 23-Jun-20 8:50am    
Why do you need to get parent object from child? Can't you use `DatabaseName` property of `Table` class to access parent object by name?
Why a'm asking. Because it might be the reason of future problems. See: What is Circular Dependency and How Do We Resolve It?[^]
If you want to create nested class, please see: Nested Types - C# Programming Guide | Microsoft Docs[^]

C#
public class Database
{
    public List<Table> Tables { get; set; }
}

public class Table
{
    public Database Database { get; set; }
}


Usage

C#
Database db = new Database();
Table t = new Table();
t.Database = db;
db.Tables.Add(t);
 
Share this answer
 
First of all, please read my comment to the question.

Second, Table class can NOT contain the member with the same name as its enclosing type. See: Compiler Error CS0542 | Microsoft Docs[^]

Third, you don't need a reference to parent object! As you are using XML data, you can get parent object from XML. You can even search for specific table trough the databases objects. See:

C#
void Main()
{
	List<Database> databases = new List<Database>
	{
		new Database(){Name="A", DataSource="B", Tables = new List<Table>
			{
				new Table(){Schema = "C", Name = "D"},
				new Table(){Schema = "E", Name = "F"}
			}},
		new Database(){Name="E", DataSource="F", Tables = new List<Table>
			{
				new Table(){Schema = "C", Name = "D"},
				new Table(){Schema = "E", Name = "F"}
			}}
	};
	
	//get parent of specific table
	Table a = new Table(){Schema = "C", Name="D"};
	List<Database> ParentsOfA = databases
		.Where(x=>x.Tables.Any(y=>y.Name == a.Name && y.Schema == a.Schema))
		.ToList();
	Console.WriteLine($"Table '{a.Name}' with schema of '{a.Schema}' belongs to:");
	foreach(Database db in ParentsOfA)
		Console.WriteLine($"'{db.Name}' database with datasource: '{db.DataSource}'");
}

// Define other methods and classes here
public class Database
{
	public string Name {get; set;}
	public string DataSource {get; set;}
	public List<Table> Tables {get; set;}
}

public class Table
{
	public string Schema {get; set;}
	public string Name {get; set;}
}


Fourth, you can add DatabaseName to Table class to get unique identifier of database object. This property should act as foreign key ;)

Final note: use XML serialization/deserialization to write/read xml data into/from xml. See:
Details of XML serialization | Microsoft Docs[^]
Examples of XML Serialization | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900