How To Define a Data Structure in RIA Services
If I have an entity in RIA services like this,
[EnableClientAccess]
public class ShapeEntity
{
[Key]
public int Id { get; set; }
[Association("Shapes", "Id", "Id")]
[Include()]
public IEnumerable<SingleShapeEntity> ShapeEntities { get; set; }
}
The problem with the above is that I 开发者_高级运维just want the ShapeEntities to hold information related to the ShapeEntity, but not be its own entity in its own right.
The problem with it being a separate entity is that I add or update something in the ShapeEntities list it calls the insert / update method for the SingleShapeEntity on the server.
I don't really want this. I would rather it be something like this,
[EnableClientAccess]
public class ShapeEntity
{
[Key]
public int Id { get; set; }
public IEnumerable<SingleShapeEntity> ShapeEntities { get; set; }
}
But what happens when I do this is the ShapeEntities doesn't end up being compiled into the client side entity.
Another point as to why I want to do this overall is that I need to be able to operate on the ShapeEntities list on the server as a whole, and at the moment the insert / update method gets called on the server with each SingleShapeEntity item one after the other.
You should be able to do this by just having the object implement the data contract attribute:
[DataContractAttribute]
public partial class ShapeEntity
{
[Key]
[DataMemberAttribute()]
public int Id { get; set; }
[DataMemberAttribute()]
public IEnumerable<SingleShapeEntity> ShapeEntities { get; set; }
}
Then in your domain service have a method for InsertShapeEntity(ShapeEntity entity). In that method you can do whatever you need to do with the list of entities.
I had to use a work around, I defined an insert method for the SingleShapeMethod on the server which did nothing, like this,
public void InsertShapeEntity(SingleShapeEntity shapeEntity)
{
}
This was called for each SingleShapeEntity that I added to the ShapeEntities enumerable. I didn't want to deal with them in this method because I need to deal with all of the info at once.
I then had to define the ShapeEntity like this,
[EnableClientAccess]
public class ShapeEntity
{
[Key]
public int Id { get; set; }
[Association("Shapes", "Id", "Id")]
[Include()]
public IEnumerable<SingleShapeEntity> ShapeEntities { get; set; }
public DateTime Updated { get; set; }
}
When I updated the ShapeEntities I had to set the Updated field to DateTime.Now so that this would trigger an update method to be called on the server,
public void UpdateShapeEntity(ShapeEntity entity)
{
// handle the new set of ShapeEnties that have been defined as a whole
}
Without having the Updated field RIA services doesn't consider the ShapeEntity to have updated and the server update method is not called. When you update the ShapeEntities RIA treats that as an update to the child entities only, not the parent.
Not the best solution, but it works.
精彩评论