Navigation Properties

Table Of Contents

What Are Navigation Properties?

Navigation Properties(or Nav Props) are C# representations of Foreign Keys between tables in a database.

They represent parent child relationships between the entities.

For example a Customer might have a Nav Prop called Orders. That represent the orders belonging to the customer

When The Sharp Factory App generates the C# code in your Visual Studio Solution; it will automatically generate Nav Props in your entities.

These Nav Props are actual strongly types properties. They can be a single object or a list of objects depending on the relationship of the entities.

When The Sharp Factory App generates the C# code in your solution, amongst other things, it generates a collection of enumerations that represent all the navigation properties of each entity.

The naming convention is EntityName + NavProperty. For example an Entity called Customer will have a corresponding enum called CustomerNavProperty.

To craft your Query to populate navigation properties of your entities.

Start by creating an instance of the Query class passing the Property Enum as the first generic type and then pass the Nav Property Enum as the second generic type parameter.

The following code illustrates how to pull a list of customers along with their orders

C#
// create an instance of the Repository Container

// remember this can be a singleton if you

// declare it early on and reuse it for the

// lifetime of your application

var repo = new RepositoryContainer();

// create an instance of Query and 

// specify the entity's PropertyEnum

// specify the entity's NavPropertyEnum to enable the BeginNavProps method

var query = new Query<CustomerProperty, CustomerNavProperty>()
    .BeginNavProps()
        .Append(CustomerNavProperty.Orders)
    .EndNavProps();                       

// pass the query to one of the repository methods

// in this case the ToList on the Customer repository

var result = repo.AppDb.Sales.Customer.ToList(query);

// iterate over the customers

foreach (var customer in result)
{
    // get the orders from the navigation property

    var orders = customer.Orders;

    // do something with the orders here

}

C#
// create an instance of the Repository Container

// remember this can be a singleton if you

// declare it early on and reuse it for the

// lifetime of your application

var repo = new RepositoryContainer();

// create an instance of Query and 

// specify the entity's PropertyEnum

// specify the entity's NavPropertyEnum to enable the BeginNavProps method

var query = new Query<CustomerProperty, CustomerNavProperty>()
    .Builder(builder =>
    {
        builder
        .NavProps((navProps) =>
        {
            navProps
            .Append(CustomerNavProperty.Orders);
        });
    });                       

// pass the query to one of the repository methods

// in this case the ToList on the Customer repository

var result = repo.AppDb.Sales.Customer.ToList(query);

// iterate over the customers

foreach (var customer in result)
{
    // get the orders from the navigation property

    var orders = customer.Orders;

    // do something with the orders here

}

The previous examples are equivalent. The only difference is the syntax.