Forums

linq or shared class in cobra?

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

linq or shared class in cobra?

Postby mithos » Mon Oct 05, 2009 9:07 am

Hello,

I started cobra some months ago for work, and learned it bit by bits when I needed something new.

The major part in where we use it is for interfaces and active record mapping. (in a Castle project if this info can be usefull to understand)

The problem is that I now need a function in a cobra class that would send back all items in a database table that have a specific value (no relation between tables possible).

At first, I thought about a Linq request, but it seems that it is not possible to do so.
Then I thought about making the class a partial class and do the other part in c#, but there it seems to be some problems with the people that I work for (I'm just in charge of a little part of the project).

so I wanted to know if you knew about a way to find it, a little like Linq in term of returns if possible.

I'm sorry if what I ask seems not clear or too hard.
In case I need to explain more, please ask

I will use this opportunity to thank the ones in charge of the Cobra project, as it seems quite good from what I saw and saved me some hours until now.
mithos
 
Posts: 2

Re: linq or shared class in cobra?

Postby Charles » Mon Oct 05, 2009 6:02 pm

Thanks for your interest in Cobra. I'm not 100% sure what you need so we may need a few iterations in this discussion thread to fully answer your question(s). Let's start with this:
class Queries

shared

def customersWithOutstandingBalances as Customer*
for cust in /# active record query #/
yield cust

From C# you could then write:
Code: Select all
foreach (Customer c in Queries.CustomersWithOutstandingBalances) { ...

Note that the use of yield means the records are only produced as needed. Provided that the query does the same thing then the whole stream of customers is consumed only on demand. I emphasized this because you mentioned LINQ which often emphasizes this.

Also, the "stream type" of Customer* will become IEnumerable<of Customer> so in C# you can use LINQ on the stream like so:
Code: Select all
var names = from c in Queries.CustomersWithOutstandingBalances select c.name;

Also from C# you can use the numerous extension methods on IEnumerable<of T>.

You mentioned a specific value so that could be something like:
class Queries

shared

def ordersFromState(state as String) as Order*
for order in /# active record query using state #/
yield order

So you pass in the state or province that the order was placed from.

Of course, if you're asking what the active record query should look like then I'm not helping at all. In that case, maybe gauthier can speak up.

Also, I've been preferring non-shared/static methods because I find that I eventually need some state and then I have to manage that state over the life time of the application which can lead to subtle bugs. So I prefer objects which may or may not require initialization parameters. Something like:
class Queries

cue init(source as DataSource) # whatever that means
base.init
_source = source

get source as DataSource from var

def customersWithOutstandingBalances as Customer*
for cust in /# active record query #/
yield cust

The user of the above class can cache it if needed and can also throw it away whenever they like and create a new one. Because it's an object, you could even have multiple instances and put them in lists, sets, dictionaries, etc. You could serialize them, create them out of config information, etc. These flexibilities are not available with the shared/static approach.

From C# that would be something like:
Code: Select all
var q = new Queries(dataSource);
foreach (Customer cust in q.CustomersWithOutstandingBalances) { ...


Finally, you don't have to use streams if you really wanted a new list each time that the caller could manipulate (sort, remove items, etc.):
class Queries

...

def customersWithOutstandingBalances as List<of Customer>
...

Did that help at all? What else were you looking for?
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: linq or shared class in cobra?

Postby mithos » Mon Oct 05, 2009 11:37 pm

Thanks, your post explains me a lot of points.

The only thing I don't know is what to put in the place of '/# active record query #/'
What I mean is that the cobra classes are at the base of the application, so they only access to their interfaces and some external dll.

But maybe it is not related to cobra, so I'll double check with castle project Active record doc.

Anyway, thank you for your fast and complete answer, it save me a lot of time in resolving other problems too.
mithos
 
Posts: 2

Re: linq or shared class in cobra?

Postby Charles » Tue Oct 06, 2009 3:43 am

Maybe gauthier can provide some help here as he has been using active record on his project.
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 47 guests