6
\$\begingroup\$

I came to work at a place not really understanding data binding. I have been working with it for about a year now, and I'd like to get some clarification on what is going on.

Particularly, I'd like to know if there any difference between the following two types of calls.

Given this simplistic example of the data:

public class TableType {
    public int ID;
}

public System.Data.Linq.Table<TableType> TableTypes
{
    get
    {
        return this.GetTable<TableType>();
    }
}

Version 1:

private BindingList<TableType> _tableTypeList;

public BindingList<TableType> TableTypesList1
{
    get
    {
        if (_tableTypeList == null)
        {
            var temp = TableTypes.OrderBy(t => t.ID);
            _tableTypeList = ((IListSource)temp).GetList() as BindingList<TableType>;
        }
        return _tableTypeList;
    }
}

The Senior Developer here wrote lots of database code using Version 1.

Looking at it, you would think that it would prevent redundant database calls, but that does not seem to be the case. A breakpoint on the code never gets hit again.

Version 2:

public BindingList<TableType> TableTypesList2
{
    get
    {
        var query = from t in TableTypes orderby t.ID select t;
        var result = new BindingList<TableType>(query.ToList());
        return result;
    }
}

The Senior Developer left about 6 months ago, so now I am writing the database calls.

I have been using Version 2, which looks much cleaner to me and appears to do the exact same thing.

Is one version any better than the other? Is there anything one version does that another does not? How do I get a better feel for what is going on?

Particularly, I was surprised to find that another call to TableTypesList1 did not call the getter.

\$\endgroup\$
5
\$\begingroup\$

Is one version any better than the other?

It depends on what you actually want.

  • V1 is a single time operation. The table is going to be filled only once and subsequent calls will use cached data.
  • V2 on the other hand will be executed on each acceess.

One cannot say which one is to chose. You must know what would work better for you.


Is there anything one version does that another does not?

Yes. V1 will get data only once and V2 everytime. This means that it might be acceptable to let the first property be a property because subsequent calls will be fast and only the first time initialization might take some time (depending on how much data it needs to get). The value it returns is stored in a backing field and it returns the same value everythime.

The second solution on the other head clearly behaves like a method because it can return different lists on each call thus I would make V2 a method.


How do I get a better feel for what is going on?

This if is the answer:

if (_tableTypeList == null)
{
    var temp = TableTypes.OrderBy(t => t.ID);
    _tableTypeList = ((IListSource)temp).GetList() as BindingList<TableType>;
}

I was surprised to find that another call to TableTypesList1 did not call the getter.

It probably did but when you put the breakpoint inside the if it's obvious that this block won't be called again.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.