When use related fields and when relations itself

Posts   
 
    
G.I.
User
Posts: 172
Joined: 09-Jun-2005
# Posted on: 04-Jul-2005 12:43:21   

Hi,

I have a question: When should you use fields on related fields and when should you use relations and load full entities.

The reason why I am asking this is as because I have a table called Task. This Task has many relations to other tables that are connected with this task. For example the tables Project, Customer, Contract, etc.

The problem is that I often have to show data from different tables, for example, projecttask data, and a customer name, or project task data and a contract_type or sometimes just only a little bit of the project task data. It differs at different places in the application.

I can add all these fields as fields on related fields, but then I have a lot of fields which I don't use all the time. I don't even need all the data from the table ProjectTask, so why should I get so much data when I don't need it?

So as I see it I have two options: Use fields on related fields to get this data, or use the relations and get other entities with lazy loading to get for example the customer entity. But to load the whole entity for just the CustomerName seems like a lot of work for just a little thing.

Am I overlooking something here? If not, what is the best practice between the two options that I see?

Gr.,

G.I.

davisg avatar
davisg
User
Posts: 113
Joined: 27-Feb-2005
# Posted on: 04-Jul-2005 15:45:49   

Hi,

I know I am not fully answering your question here but if I have read you right, why not use a dynamic list for this? This is perfect for getting read-only selected data from single table or multiple tables. Just use the alias property of the fields.DefineField to determine which table you are getting the data from. You can then save this back using entities if needed with a little more code.

Geoff.

Code snippet from the documentation:-

 // C#
ResultsetFields fields = new ResultsetFields(3);
fields.DefineField(EmployeeFieldIndex.FirstName, 0, "FirstNameManager", "Manager");
fields.DefineField(EmployeeFieldIndex.LastName, 1, "LastNameManager", "Manager");
fields.DefineField(EmployeeFieldIndex.LastName, 2, "AmountEmployees", "Employee", AggregateFunction.Count);
IRelationCollection relations = new RelationCollection();
relations.Add(EmployeeEntity.Relations.EmployeeEntityUsingEmployeeId, "Employee", "Manager", JoinHint.None);

IGroupByCollection groupByClause = new GroupByCollection();
groupByClause.Add(fields[0]);
groupByClause.Add(fields[1]);

DataTable dynamicList = new DataTable();
TypedListDAO dao = new TypedListDAO();
dao.GetMultiAsDataTable(fields, dynamicList, 0, null, null, relations, true, groupByClause, null, 0, 0);

erichar11
User
Posts: 268
Joined: 08-Dec-2003
# Posted on: 04-Jul-2005 15:56:13   

Hi G.I,

If I understand your question correctly, you want to get a subset of data from multiple entities. When I need to do this I usually create a dynamic typed list. You can also create a typed list or typed view depending on your needs from the designer. I use the adapter scenario, So I do something like the following:

DataTable dt = new DataTable(); RelationPredicateBucket bucket = new RelationPredicateBucket(); DataAccessAdapter adapter = new DataAccessAdapter(true);

        ResultsetFields fields = new ResultsetFields(5);
        fields.DefineField(DeliverableFieldIndex.Name, 0, "Name");
        fields.DefineField(UserFieldIndex.Name, 1, "Owner");
        fields.DefineField(StatusTypeFieldIndex.Name, 2, "StatusType");
        fields.DefineField(DeliverableDataFieldIndex.PlannedDate, 3, "PlannedDate");
        fields.DefineField(DeliverableDataFieldIndex.ActualDate, 4, "ActualDate");

        bucket.Relations.Add(MilestoneEntity.Relations.MilestoneDeliverableEntityUsingMilestoneId);
        bucket.Relations.Add(MilestoneDeliverableEntity.Relations.DeliverableEntityUsingDeliverableId);
        bucket.Relations.Add(DeliverableEntity.Relations.UserEntityUsingUserId);
        bucket.Relations.Add(DeliverableEntity.Relations.DeliverableDataEntityUsingDeliverableId);
        bucket.Relations.Add(DeliverableDataEntity.Relations.StatusTypeEntityUsingStatusTypeId);

        bucket.PredicateExpression.Add(PredicateFactory.CompareValue(MilestoneFieldIndex.MilestoneId, ComparisonOperator.Equal, milestone.MilestoneId));
        adapter.FetchTypedList(fields, dt, bucket);
        adapter.CloseConnection();

        return dt;

Basically, you just define the fields you need, and then create the relations between those fields and add them to a RelationPredicateBucket. Please note this is for read only purposes. I would check the docs for creating dynamic typed lists or something along those lines. Hope this helps.

Eric