This project is read-only.

Querying against custom methods

Sep 1, 2010 at 11:31 PM

I think this is more of a design question than an nHydrate question but I want to see if there's an nHydrate solution before I start doing crazy things.

Let's say, for sake of example, that I have an Order class, and it has a custom boolean method called "IsOverdue" that implements some overly complicated business logic to determine if an Order is overdue.  I use this method to flag Orders in red when viewing in a datagrid.

Now let's say I have 10 million orders and I want to run a query to get all the overdue ones.  I already have my business logic in one place, and I'd like to keep it in one place.  I'm OK moving it, but I'd like to keep just one copy.

My first instinct is to do OrderCollection q = new OrderCollection.RunSelect(x => x.IsOverdue==true)

Of course this doesn't work, nor should it because SQL has no idea what I'm talking about.

 

What's my next move?  I've so far avoided writing any views or stored procedures, but I feel like now's the time to cave.

Sep 2, 2010 at 2:08 AM

This is a difficult issue. We do support calculated fields so if you can put it in a SQL calculated field formula....problem solved. However you probably already thought of that and you said, "overly complicated" so I assume you are looking for another solution. 

You can create a SP as you already know.

You can do something more complicated like (you have many rows right) narrow by date or some other criteria and bring all records to client and then run LINQ on the client with your new property. This is obviously an issue if you pull too many records like 20k or more to the client. However this is still an option if you are narrowing by other criteria. I actually use this approach sometimes for very complicate logic and I know there will never be millions of records. For example I have 30k-100k records in DB and I narrow by date so it is much less then, then I run a LINQ statement against it in memory to get the "real" records.

Sorry but complicated logic can be added to the DB or in C# and using client side (in memory) or both. But of course you already knew all this. I am sorry I do not have a better answer.