CSLA.NET 3.5: Indexed properties

CSLA.NET 3.5: Indexed properties

Old forum URL: forums.lhotka.net/forums/t/5375.aspx


Patrick posted on Tuesday, September 09, 2008

Hi,

is there any way to get to an indexed property in a business list without using Linq?

class Customer{
[Indexed]
public string Id;
}

class BusinessList<Customer> {
   public Customer GetCustomerById(string id){
       // How to get the Customer using the index without using Linq
   }
}

Thanks a lot,
Patrick

PS: The reason that I'm asking that I call a similar method ~ 5000 times in a recursive loop and LINQ has quite  an overhead due to compiling the query (which looks for a different value every time).

ajj3085 replied on Wednesday, September 10, 2008

You'll probably have to keep a hashtable in your BLB for this.

SonOfPirate replied on Wednesday, September 10, 2008

Sounds to me like you have a specific use case that would require a specific business object to satisfy.  Rather than performing 'x' number of GetCustomerById calls, you should have a business object designed specifically for the function you are performing.

For example, if you load your data into a Dictionary<string, Customer> keyed on the id value, then the lookups can be performed much quicker using the built-in capabilities of the dictionary.

HTH

 

Patrick replied on Wednesday, September 10, 2008

In CSLA.NET 3.5 BusinessListBase has a member

    private Linq.IIndexSet<C> _indexSet;

so when you add the [Indexable] attribute to a property it's indexed automatically.

You can search the index via this interface which BusinessListBase implements:
  internal interface IIndexSearchable<T>
  {
    IEnumerable<T> SearchByExpression(Expression<Func<T, bool>> expr);
  }

I was just wondering if I could access the index directly without going via the SearchByExpression method. The reason for this is that the expression has to be compiled each time which introduces an overhead in itself.

Thanks,
Patrick

AaronErickson replied on Thursday, September 11, 2008

That is a very interesting question.

I would probably use the IndexingProvider property, and write a class that supports IIndexSet that works in a manner closer to what you describe.

Alternatively, I could see if Rocky wants to open up the _index to be a protected field and perhaps make the IndexSet concrete class not internal anymore.  But frankly, to keep things simple, you probably want to provide your own similar, but slightly different indexing implementation.

Patrick replied on Thursday, September 11, 2008

AaronErickson:

Alternatively, I could see if Rocky wants to open up the _index to be a protected field and perhaps make the IndexSet concrete class not internal anymore. 

I'm quite happy to do this in my version of CSLA.NET Smile [:)]

Would you have a sample of how I could access an item in the list directly if I were to do the above?

Thanks,
Patrick

AaronErickson replied on Friday, September 12, 2008

In SearchByExpression, which has the general case, we do this:

foreach (C item in _indexSet.Search(expr, property))
yield return item;

Basically, call the search method with the exact property you want to search on.

Copyright (c) Marimer LLC