Lambda expression in dlinq attribute

  • Thread starter Thread starter Andrus
  • Start date Start date
A

Andrus

I need to use lambda expressions or other way to
specify default query for dlinq entity type.
Code below causes compile error shown in comment.
How to fix ?
Maybe to use [QueryAttribute( "Supplier", "d=>
d.Discriminator==""Domestics"" ")] ?

Andrus.

using System;
using System.Linq;

// '<' unexpected : attributes cannot be generic
[QueryAttribute<Supplier>(d=> d.Discriminator=="Domestics")]
class DomesticsSupplier {
public string Discriminator { get; set; };
public string Name { get; set; };
}

class QueryAttribute<TEntity> : Attribute {
IQueryable<TEntity> Query;
internal QueryAttribute(IQueryable<TEntity> query) {
Query = query;
}
}
 
Well, the compiler message is clear enough; you would have to pass a
Type through, not a generic. Also, your lambda isn't an IQueryable<T>
- it is a Func<T, bool> or an Expression<Func<T, bool>> - but I doubt
you could use iether directly in an attribute.

I would suggest something like
[QueryAttrubute(Condition="SupplierDiscriminator",
Type=typeof(Supplier)]
....
public static Expression<Func<Supplier, bool>> SupplierDiscriminator {
get {return d=> d.Discriminator=="Domestics";}
}

(I would make Type optional, defaulting to the current)

Note that this usage is very different (as I understand it) to how
NHibernate uses QueryAttribute, and you'll need to code it up by
hand...

Marc
 
Actually, it looks like LINQ to SQL supports
InheritanceMappingAttribute, and IsDiscriminator on ColumnAttribute.
 
Marc,
Actually, it looks like LINQ to SQL supports
InheritanceMappingAttribute, and IsDiscriminator on ColumnAttribute.

I have 2 discriminator columns in my documents table:

CREATE TABLE Document (
DocumentType C(1), ---- Document main type: G=Incoice, U=Order etc.,
total 25 values.
AssetType N(1) ) -- Document subtype: only 3 values: 1,2,3
.... 50 other columns
);

I think I must create custom 3-level entity type hierarchy for this:

Document
AssetDokument1
AssetDokumentG1 // type represents subtype 1 invoice
AssetDokumentO1 // type represents subtype 1 order
...
AssetDokument2
AssetDokumentG2 // type represents subtype 2 invoice
AssetDokumentO2 // type represents subtype 2 order
...
AssetDokument3
....

AFAIK DbLinq, Linq-SQL and EF do not support two column discriminators.
So I need probably to implement this manually.

I also need to create DLinq queries to return paged entity lists for every
type.
I'm planning to create separate query factory class

public abstract class QueryFactory<TEntity> {

public IQueryable<TEntity> Query, OrderedQuery;

public QueryFactory(DataContext db) { ....
}
}

and mirror my class hierarchy by creating the same query factory class
hierarchy:

QueryFactory<TEntity>
DocumentFactory<T> where T: Document
AssetDokument1Factory<T> where T: AssetDokument1
AssetDokumentG1Factory<T> where T: AssetDokumentG1
.....

Is this best design ?

Andrus.
 
Well, since .NET has single inheritance, you can't have two dimensions
of discriminator... could you perhaps simply map the two columns into
a single discriminator column? This sound like something EF could
handle (perhaps), or maybe a UDF when reading... but to be honest I
think two discriminators is making life hard...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top