Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, April 27, 2016 9:47 AM
consider below table :
FkName SchemaName Table Column RefTable RefColumn
FK_Factory_Person dbo Factory PersonId Person Id
FK_Car_Person dbo Car PersonId Person Id
FK_Factory_Car dbo Factory CarId Car Id
I want to create Dependency class for saving the dependent data
so I wrote a self-reference class that any dependency can have dependencies.
public class Dependency
{
public string TableName { get; set; } // RefTable
public string ColumnName { get; set; } //RefColumn
public List<Dependency> Dependencies { get; set; } // Table
}
but I dont know how fill class :
- TableName=Person , ColumnName=Id , Dependencies=Factory,Car
--TableName=Car , ColumnName=Id , Dependencies=Factory
Factory
'
' Person
' Car
Car
'
' Person
Can anyone help me for filing Dependency class recursively ?
All replies (5)
Thursday, April 28, 2016 3:45 AM âś…Answered
You can do that but it depends upon the situation as to whether it is correct or not. I guess you could set up a virtual directory structure like that. As far as the language is concerned you can have a property of the same type as the declaring type. Without supporting that we couldn't do something simple like a linked list.
Wednesday, April 27, 2016 4:16 PM
consider below table :
FkName SchemaName Table Column RefTable RefColumn FK_Factory_Person dbo Factory PersonId Person Id FK_Car_Person dbo Car PersonId Person Id FK_Factory_Car dbo Factory CarId Car Id
I might be missing something here, but this table definition makes no sesne for me:
Why would stuff that has Foreign Key Constraints on different tables be in the same table?
It looks like you failed to properly resolve a whole bunch of N:M relationships, so you ended up squeezing them into the same table?
I was not even aware something like this could be done in any Relational Database with default constraints in the first place.
I know of self referencing Foreign Key constraints pointing to the Primary Keys (what you have to do for any decent Heirarchical design), but nothing on this level. What type are thsoe coluns even at DB, Query and application level?
Wednesday, April 27, 2016 5:45 PM
Christopher84
Please dont think to database ! I want to know How Can I have self reference in OOP.
for Example :
public class Directory
{
public string Path {get;set;}
public List<Directory> Directories {get;set;}
}
I want to know all directory with sub directories based on above class and recursively.
more I need algorithm and C# code for solve these problems recursively.
but about above post I need to explain that I want to know which table is base and have foreign key into another tables for update & delete cascading based on C# code
I want to know these dependencies based on a poco recursively
Wednesday, April 27, 2016 6:30 PM
There is no solution to this problem nor are there any algorithms. It completely depends upon the type of relationship (parent/child, master/detail, etc), the ownership of the objects, etc. There is not a single solution that solves all these problems. You've thrown out 2 completely different examples so a solution would not cover both.
You are also using terminology that is ambiguous. Self-referencing could mean an object that references another object of the same type (such as the employee/supervisor) relationship. You could also mean a list that ultimately includes the original starting point (i.e. a graph).
As for the DB example you are reinventing a complex wheel. ORMs already handle this for you. Each table is a separate type. Each row is an object of that type. Each FK on the table is represented as a child property of the appropriate type. Since each row can have 0 or 1 FK value in a column a single property for each FK is fine. But to go the other way would require that you join the 2 tables together to get the list of all "child" rows that reference the "parent" rows PK.
For your specific example you are trying to go backwards. As such you'd need to group the table by RefTable and RefColumn. This gives you the rows that contain the same values. You can then use LINQ to generate the child dependencies.
static void Main ( string[] args )
{
var dependencies = from d in Data.GetData()
group d by new { Table = d.RefTable, Column = d.RefColumn } into g
select new Dependency() { TableName = g.Key.Table, ColumnName = g.Key.Column,
Dependencies = g.Select(x => new Dependency() { ColumnName = x.Column, TableName = x.Table }).ToList()
};
}
class Dependency
{
public string TableName { get; set; }
public string ColumnName { get; set; }
public List<Dependency> Dependencies { get; set; }
}
class Data
{
public string FKName { get; set; }
public string SchemaName { get; set; }
public string Table { get; set; }
public string Column { get; set; }
public string RefTable { get; set; }
public string RefColumn { get; set; }
public static IEnumerable<Data> GetData ()
{
return new[]
{
new Data() { FKName = "FK_Factory_Person", SchemaName = "dbo", Table = "Factory", Column = "PersonId", RefTable = "Person", RefColumn = "Id" },
new Data() { FKName = "FK_Car_Person", SchemaName = "dbo", Table = "Car", Column = "PersonId", RefTable = "Person", RefColumn = "Id" },
new Data() { FKName = "FK_Factory_Car", SchemaName = "dbo", Table = "Factory", Column = "CarId", RefTable = "Car", RefColumn = "Id" }
};
}
}
Michael Taylor
http://www.michaeltaylorp3.net
Thursday, April 28, 2016 2:26 AM
There is no solution to this problem nor are there any algorithms. It completely depends upon the type of relationship (parent/child, master/detail, etc), the ownership of the objects, etc. There is not a single solution that solves all these problems. You've thrown out 2 completely different examples so a solution would not cover both.
You are also using terminology that is ambiguous. Self-referencing could mean an object that references another object of the same type (such as the employee/supervisor) relationship. You could also mean a list that ultimately includes the original starting point (i.e. a graph).
As for the DB example you are reinventing a complex wheel. ORMs already handle this for you. Each table is a separate type. Each row is an object of that type. Each FK on the table is represented as a child property of the appropriate type. Since each row can have 0 or 1 FK value in a column a single property for each FK is fine. But to go the other way would require that you join the 2 tables together to get the list of all "child" rows that reference the "parent" rows PK.
For your specific example you are trying to go backwards. As such you'd need to group the table by RefTable and RefColumn. This gives you the rows that contain the same values. You can then use LINQ to generate the child dependencies.
static void Main ( string[] args ) { var dependencies = from d in Data.GetData() group d by new { Table = d.RefTable, Column = d.RefColumn } into g select new Dependency() { TableName = g.Key.Table, ColumnName = g.Key.Column, Dependencies = g.Select(x => new Dependency() { ColumnName = x.Column, TableName = x.Table }).ToList() }; } class Dependency { public string TableName { get; set; } public string ColumnName { get; set; } public List<Dependency> Dependencies { get; set; } } class Data { public string FKName { get; set; } public string SchemaName { get; set; } public string Table { get; set; } public string Column { get; set; } public string RefTable { get; set; } public string RefColumn { get; set; } public static IEnumerable<Data> GetData () { return new[] { new Data() { FKName = "FK_Factory_Person", SchemaName = "dbo", Table = "Factory", Column = "PersonId", RefTable = "Person", RefColumn = "Id" }, new Data() { FKName = "FK_Car_Person", SchemaName = "dbo", Table = "Car", Column = "PersonId", RefTable = "Person", RefColumn = "Id" }, new Data() { FKName = "FK_Factory_Car", SchemaName = "dbo", Table = "Factory", Column = "CarId", RefTable = "Car", RefColumn = "Id" } }; } }
Michael Taylor
http://www.michaeltaylorp3.net
Thanks Micheal for your clarification
Can I ask you about your solution for my second example ? (Get Directories recursively)
Do you confirm my statement ?
=> When every body want to create a self reference calss model in oop
they should create something like this
public class X
{
// Some properties
public List<X> OtherX // for self relation
}
Do this pattern is correct ?