[Serializable]
[HierarchyRoot]
public class MyEntity : Entity
{
    [Field, Key]
    public int Id { get; private set; }

    [Field(Length = 100)]
    public string Name { get; set; }

    [Field]
    [Association(PairTo = "Owner",OnOwnerRemove = OnRemoveAction.Clear)]
    public EntitySet<InnerEntity> InnerItems { get; private set; }
}

[Serializable]
[HierarchyRoot]
public class InnerEntity : Entity
{
    [Field, Key]
    public int Id { get; private set; }

    [Association(OnOwnerRemove = OnRemoveAction.Clear)]
    [Field(Nullable = false)]
    public MyEntity Owner { get; set; }

//[VirtualField]
    public string OwnerName { get { return Owner.Name; } }

    [Field(Length = 100)]
    public string Text { get; set; }
}

//[VirtualField] public string OwnerName { get { return Owner.Name; } }

I would like to use this in Select/Where

var items = Query.All<InnerEntity>().Where(a => a.OwnerName.StartsWith("a"));

this should be equivalent to

var items = Query.All<InnerEntity>().Where(a => a.Owner.Name.StartsWith("a"));

But if I have dozen linked enities it will be ugly.

May be it could be expression parser to transform accessing to non persisted property to mapped one?

This thread was imported from our support forum. The original discussion may contain more detailed answer.

asked Mar 24 '10 at 09:46

pil0t's gravatar image

pil0t
207545763


One Answer:

Alex (Xtensive) wrote:

This is already possible. See:


psulek wrote:

[Serializable]
[HierarchyRoot]
public class MyEntity : Entity
{
    [Field, Key]
    public int Id { get; private set; }
    [Field(Length = 100)]
    public string Name { get; set; }
    [Field]
    [Association(PairTo = "Owner",OnOwnerRemove = OnRemoveAction.Clear)]
    public EntitySet<InnerEntity> InnerItems { get; private set; }
}
[Serializable]
[HierarchyRoot]
public class InnerEntity : Entity
{
    [Field, Key]
    public int Id { get; private set; }
    [Association(OnOwnerRemove = OnRemoveAction.Clear)]
    [Field(Nullable = false)]
    public MyEntity Owner { get; set; }
//[VirtualField]
    public string OwnerName { get { return Owner.Name; } }
    [Field(Length = 100)]
    public string Text { get; set; }
}

//[VirtualField] public string OwnerName { get { return Owner.Name; } } I would like to use this in Select/Where var items = Query.All<innerentity>().Where(a => a.OwnerName.StartsWith("a")); this should be equivalent to var items = Query.All<innerentity>().Where(a => a.Owner.Name.StartsWith("a")); But if I have dozen linked enities it will be ugly. May be it could be expression parser to transform accessing to non persisted property to mapped one?

And can you DO4 guys think about implement such feature with attribute [VirtualField] where it will convert method of this get_OwnerName into Func<expression> with postsharp in behind of build process. To be fully transparent for us, we just mark property like this OwnerName with attribute [VirtualField] and postsharp does magic for us. Can be this possible or not?


Alex (Xtensive) wrote:

This must be possible only if we know what kind of logic must be inside OwnerName property getter. But we don't - all we know is its name and type. The logic is hard-coded in IL, and it's impossible (w/o such tool as Reflector, and if there are many statements - even with it) to convert it C# Expression tree, that can be transformed to the one used by DO4 during LINQ query translation.


mahdness wrote:

I did something similar in DO 3.9 for fill descriptors (analog of Prefetch in this case).

OwnerName property would be marked with VirtualFieldAttribute("Owner.Name") (it would be nice if we could handle more complicated cases such as Person.Age being marked with VirtualField("DateTime.Now - DateOfBirth")]

Anyway, if you cached these during domain build process and created LINQ rewriters for them (as well as prefetch processors), that would be cool.

Just a thought

answered Mar 24 '10 at 11:43

Editor's gravatar image

Editor
46146156157

(Mar 24 '10 at 11:43) Alex Yakunin Alex%20Yakunin's gravatar image

If such an attribute has a property allowing to specify an expression as string, this is really possible.

So we'll take this into account and consider implementing this in future.

(Mar 24 '10 at 11:43) Alex Yakunin Alex%20Yakunin's gravatar image
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

Subscription:

Once you sign in you will be able to subscribe for any updates here

Tags:

×569
×52

Asked: Mar 24 '10 at 09:46

Seen: 2,174 times

Last updated: Mar 24 '10 at 09:46

powered by OSQA