Entity dependency list

Posts   
 
    
simmotech
User
Posts: 1024
Joined: 01-Feb-2006
# Posted on: 12-Jun-2012 14:24:02   

Is there a way of generating a list of entity types in order of their dependencies.

I have an empty LLBLGen database and an existing non-LLBLGen database and I am writing an importer.

I have two unit tests - one to test importing and one to delete entities (including reseeding identities etc) from the LLBGen database so that I can run the first again.

What would be fabulous is a way of listing all the entities in order of their dependencies so that I can import in the correct 'order' and conversely reset tables in the opposite order.

Has anything like that been done? I am sure all the information required is in the project somewhere.

Cheers Simon

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 12-Jun-2012 14:36:27   

Dependencies as in... relationships or inheritance dependencies? Or both?

It's not that hard. Use algorithmia simple_smile (already available to you in a .lpt template) -> Create a new directed graph (DirectedGraph is a type in algorithmia) and add all entities to it. Then traverse the entitymodel graph in the project and for each relationship found, add a directed edge from FK to PK to your new graph, or in case of an m:n relationship, just skip it (as an m:n relationship depends on 2 m:1 relationships, which are already in the list of relationships you're traversing). Then traverse the inheritanceinfo graph and add for each edge found an edge to the new graph from subtype to supertype, if the inheritance hierarchy is of type TPE. Otherwise skip it (as they're mapped onto the same target, as the heirarchy type is TPEH)

The project doesn't expose a method which expose dependencies combined (relationship+inheritance). And you want it combined, as otherwise you have to do it manually anyway.

Then use algorithmia's topological sorting algorithm (2 lines of code) to sort the directed graph topologically. This will give you the dependencies in the right order. The algorithm has parameters which allows you to configure what the order has to be and whether it has to fail when it detects a cycle. See Algorithmia's sourcecode and tests for details on this.

example:


[Test]
public void TopologicalSorterOnDirectedAcyclicGraphWhereDirectionMeansDependency()
{
    DirectedGraph<string, DirectedEdge<string>> graph = new DirectedGraph<string, DirectedEdge<string>>();
    graph.Add(new DirectedEdge<string>("A", "B"));  // A->B
    graph.Add(new DirectedEdge<string>("A", "C"));  // A->C
    graph.Add(new DirectedEdge<string>("B", "D"));  // B->D
    graph.Add(new DirectedEdge<string>("C", "D"));  // C->D
    graph.Add(new DirectedEdge<string>("D", "E"));  // D->E
    graph.Add(new DirectedEdge<string>("H", "G"));  // H->G
    graph.Add(new DirectedEdge<string>("G", "F"));  // G->F

    TopologicalSorter<string, DirectedEdge<string>> sorter = new TopologicalSorter<string, DirectedEdge<string>>(graph);
    sorter.Sort();
    List<string> expectedResults = new List<string>() { "E", "D", "B", "C", "A", "F", "G", "H" };
    for(int i =0;i<sorter.SortResults.Count;i++)
    {
        Assert.AreEqual(expectedResults[i], sorter.SortResults[i]);
    }
}

Frans Bouma | Lead developer LLBLGen Pro
simmotech
User
Posts: 1024
Joined: 01-Feb-2006
# Posted on: 22-Jun-2012 09:04:41   

Wow that sounds quite complicated (I'm not academically-oriented and talk of Nodes and Edges make my eye glaze over smile ). I'll have a look when I have some time.

In the meantime, I have another related question (about using existing metadata):

If I have some reference data, say a Category table, and it is used in (many) other tables. I want to be able to run a query to find out whether a given row is actually used or whether it can safely be deleted. Or better, count how many times it is referenced.

How can I build this query from the metadata?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 22-Jun-2012 11:15:32   

If I have some reference data, say a Category table, and it is used in (many) other tables. I want to be able to run a query to find out whether a given row is actually used or whether it can safely be deleted. Or better, count how many times it is referenced.

How can I build this query from the metadata?

Do you mean dynamically? as in you don't know which entities/tables are referencing the category table?

In general when it comes to Deletes, for performance sake I prefer to directly attempt the delete, and let the database return an error (FK reference) if the row was referenced. This in case the business requirements didn't ask to disable the delete functionality in case it was referenced.

simmotech
User
Posts: 1024
Joined: 01-Feb-2006
# Posted on: 26-Jun-2012 15:43:08   

Walaa wrote:

If I have some reference data, say a Category table, and it is used in (many) other tables. I want to be able to run a query to find out whether a given row is actually used or whether it can safely be deleted. Or better, count how many times it is referenced.

How can I build this query from the metadata?

Do you mean dynamically? as in you don't know which entities/tables are referencing the category table?

In general when it comes to Deletes, for performance sake I prefer to directly attempt the delete, and let the database return an error (FK reference) if the row was referenced. This in case the business requirements didn't ask to disable the delete functionality in case it was referenced.

Dynamically, exactly. I want to display the usage to the user.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 26-Jun-2012 23:33:54   

It will be costly, performance wise, to check for existence of related entities dynamically. What about my suggestion, you can also check the FK exception and find out which entity is referring to the one being deleted.

simmotech
User
Posts: 1024
Joined: 01-Feb-2006
# Posted on: 27-Jun-2012 08:39:30   

Walaa wrote:

It will be costly, performance wise, to check for existence of related entities dynamically. What about my suggestion, you can also check the FK exception and find out which entity is referring to the one being deleted.

Performance is not an issue in this case.

I want to be able to display a list of reference items and the number of times each is used (or at least a bool saying whether it used at all).

I need to do this for a lot of reference lists and need an automatic way of building a query from the metadata. Can you suggest a way or should I just work it out?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 27-Jun-2012 20:32:57   
simmotech
User
Posts: 1024
Joined: 01-Feb-2006
# Posted on: 28-Jun-2012 15:34:21   

That looks promising. Thanks!