Quantcast
Channel: C1 CMS Foundation - Open Source on .NET
Viewing all articles
Browse latest Browse all 2540

Commented Unassigned: Composite c1 - How to display data reference data in Razor functions [1845]

$
0
0
Hi,

I have been having problems figuring out the best way to display referenced data within Composite c1. Here is the scenario.....

I have a data type called Candidate. This data type has various fields and some fields that are references to other datatypes in this case I have a list of Industries and also a list of Countries.


using (var dataConnection = new DataConnection())
{
candidate = dataConnection.Get<JobAdder.ConnectedCube.Candidate>().Where(x => x.Reference == reference).FirstOrDefault();
}


I don't have a problem getting the candidate object but its time consuming getting each referenced data (Industry, Country) - I think I am doing it the long way around - I simply use the guid that is stored and do another data lookup (alot of code which I am hoping is unnecessary)

The problem is that the following code will just display the GUID of the Industry and Country reference. What is the best way to display the data here. I just assumed you would be able to do something like this @candidate.Industry.Name but it doesn't look like this is the case.

#<div class="table-content-row">
# <div class="float-left column1">
# <strong>Industry:</strong> @candidate.Industry
# </div>
# <div class="float-left column2">
# <strong>Country:</strong> @candidate.Country
# </div>
#</div>


Thanks

David
Comments: ** Comment from web user: napernik **

For razor function parameters - define the parameter as DataReference<YourType> or as NullableDataReference<YourType>

Example:


```
@inherits RazorFunction

@functions {
[FunctionParameter(Label="Shopping Item")]
public Composite.Data.DataReference<Shop.ShoppingItem> ShoppingItem { get; set; }
}

....

```

then you can use the Data property to access the fields:


```
@ShoppingItem.Data.Name;
```

Note that the Data property value isn't cached, so it you use it on multiple occasions, it is better to evaluate it to a local variable:


```
@{
var shoppingItem = ShoppingItem.Data;
}

....


@shoppingItem.Name
@shoppingItem.Price
....

```

When you have a reference you can create a new instance of [Nullable]DataReference<T>()

var candidate = new DataReference<Candidate>(reference).Data;

As for caching, you can either:
1) If running on Sql, mark the data type as cachable, this way the table with be stored in memory and all queries will be executed without SQL round trip.
2) You can create a those queries and use it to access data


```
public class Products
{
private static readonly QueryCache<Product, Guid> _productById = new QueryCache<Product, Guid>("Product by id", p => p.Id, 10000);

public static Product GetProductById(Guid productId)
{
return _productById[productId];
}
}
```

http://docs.composite.net/downloads/codeplex/napernik/querycache.cs.txt
// Old class, change namespaces Composite.Data.* -> Composite.Core.Data.* and everything should work

You can read more on performance here:

http://docs.composite.net/Configuration/Optimize-for-Speed


Viewing all articles
Browse latest Browse all 2540

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>