Partial classes & fields collection

Posts   
 
    
simon831
User
Posts: 152
Joined: 19-Jan-2006
# Posted on: 11-Feb-2008 16:30:13   

I have many custom properties in an entity partial class. What are my options for adding them to the fields collection so that I can use code like this:

foreach (IEntityField2 field in myEntity.Fields)

and avoid having to hardcode something specifically to deal with my custom properties.

goose avatar
goose
User
Posts: 392
Joined: 06-Aug-2007
# Posted on: 11-Feb-2008 17:50:31   

the problem here is that if you want to add those field to the collection you must implement a way to tell the entity how to fetch them and how to persist them. That would be too much work depending on what you are trying to do. This is what I'll do if I was in your case: I would treat the entity from the reflection point of view whenever I want to have that kind of access, that way I could inspect the properties of the object directly.

simon831
User
Posts: 152
Joined: 19-Jan-2006
# Posted on: 12-Feb-2008 10:55:26   

The code I am writing needs to handle all different types of entity. How would you use reflection?

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 12-Feb-2008 12:10:58   

The following is a long discution about **Adding custom EntityFields to an Entity at runtime **

http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=11806

simon831
User
Posts: 152
Joined: 19-Jan-2006
# Posted on: 12-Feb-2008 13:01:06   

Walaa wrote:

Adding custom EntityFields to an Entity at runtime

I don't think this is quite what I want to do. I already have all the EntityFields I need defined at design time - using partial classes.

I ideally want to add them to Entity.Fields - this does not have to be at runtime. I could then loop through all properties in an entity irrespective of if it was a mapped field or a partial class field using: foreach (IEntityField2 field in myEntity.Fields) As I need to make this code generic for all Entities in the solution I cannot use reflection based on the type.

goose avatar
goose
User
Posts: 392
Joined: 06-Aug-2007
simon831
User
Posts: 152
Joined: 19-Jan-2006
# Posted on: 18-Feb-2008 11:15:20   

I take it that its still not possible to add to the fieldsCollection.

I think this is what I need, and it seems there are other people. Could I request this be added to the llb TODO list?


protected override IEntityFields CreateFields()
{
    IEntityFields toReturn = base.CreateFields();
    toReturn.Expand(1);
    IEntityField IsPolicyValid = new EntityField("IsPolicyValid", SqlFunctionFactory.IsPolicyValid(SomeEntityFields.SomeEntityId));
    toReturn.DefineField(IsPolicyValid, (int)SomeEntityFieldIndexExt.IsPolicyValid);
    return toReturn;
}

In the mean time, could you explain this?

In the SqlServer DQE, you could modify the insert DQ creation routine by adding some code which tests if the field in question has an Expression set. If so, skip the field (inserts need this, for updates it should still work). Be aware that the index is used in both the fields and the persistence info and from that point on doesn't match anymore so you need 2 indexes.

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 18-Feb-2008 12:21:28   

In the SqlServer DQE, you could modify the insert DQ creation routine by adding some code which tests if the field in question has an Expression set. If so, skip the field (inserts need this, for updates it should still work). Be aware that the index is used in both the fields and the persistence info and from that point on doesn't match anymore so you need 2 indexes.

Adding a custom field that doesn't exist to the EntityFields collection will result in an entity which has fields more than defined in its persistence info, this will lead to errors when persisting to the database.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 19-Feb-2008 10:40:24   

Please implement dynamic fields added at runtime differently, i.e. via a meta-table. The mappings are cached and static, you can't alter them at runtime.

Frans Bouma | Lead developer LLBLGen Pro