I am using WCF RIA Services in combination with the Entity Framework 4.0.
To ensure that related objects make it safely to the client, I need to first include them using the ObjectQuery(Of T).Include or DbSet(Of TResult).Include methods within my DomainService. I also need to add the IncludeAttribute to the navigation properties within metadata class开发者_StackOverflowes.
I make a single decision to have a related object transferred to the client and am forced to make two changes to implement it which seems a bit redundant.
I am considering making modifications to the T4 templates so that all navigation properties are automatically decorated with the IncludeAttribute.
I was a little worried about entities that were left over from previous requests being unintentionally sent to the client but my understanding is that the DomainService is stateless which should mean that this will not be the case, right?
The point of the "DomainService" name is that it is the stateless object that represents a specific domain that is characteristic of my application.
Another approach that I have seen was to automatically apply Include attributes to any metadata files within the project. I would still need to create the metadata files though.
Are there any other dangers that I am not seeing? Is there a better way to achieve the same effect?
In this case, I think you've identified the potential problem space. By making IncludeAttributes the default, your service will return whatever object graph you put together in memory.
The best way to implement this solution would be to create a custom DomainServiceDescriptionProvider (here's a decent sample) that infers the IncludeAttribute for the correct property types without you having to keep your metadata files up-to-date.
I modified the EDMX T4 template to apply the IncludeAttribute automatically and it is working as expected.
One advantage of manually applying the attributes that I identified along the way was that you are able to control the volume of code that is generated on the client. Entities that are used only on the server can be excluded.
This was not an issue in my case.
精彩评论