Is this bad practice (am i doing this wrong)?
In a silverlight LOB application I'm populating a ItemsControl with the results of a RIA services (EF) request. Inside the items control are some custom controls, and in one of them I have a button which I use to delete the entity.
To remove this I attach it to the domain context in the control and then remove it. To be able to attach it I am doing this 开发者_StackOverflow社区in my view model when I receive the data:
foreach(var app in Apps.ToList())
{
_context.Apps.Detach(app);
}
Is this a hack? because it sure feels like one.
Patterns exist to serve the developer not the other way around. You've identified smelly code. Something that bothers you. Now there are two choices you have to make. Is the smell offensive enough to remove? And how should you go about removing it.
One suggestion. You don't have to use standard CRUD with RIA Services. You can create a Service Operation DeleteApp that takes the App's Id and perform the delete on the database. This would avoid the need to perform the detach and the like on the client side. This is an example of the Transaction Script pattern as I described in my article albeit a very rudimentary example. Don't forget to reload the client list after doing this so that you don't have stale data.
At the far extreme, you may want to look into command query separation. In this case you have two separate DomainServices one that is Read Only and the other that is Write only. This forces you to be more explicit with your actions and to think more about WHAT you want to do instead of HOW to do it.
精彩评论