views:

1391

answers:

1

In a RIA Domain service I have added some utility functions. For instance we have...

public virtual CmsDealer GetCmsDealerById(string id)
{
    return this.Context.CmsDealerSet
     .Include("CmsItemState")
     .FirstOrDefault(p => p.Id == id);
}

Now that function has it's own issues if the id is nonexistant, but lets table that for now. The important thing is that function compiles and executes.

However a similar function...

public virtual void DeleteCmsDealerById(string id)
{
    var dealer = this.Context.CmsDealerSet
     .FirstOrDefault(d => d.Id == id);

    if (dealer != null)
    {
     DeleteCmsDealer(dealer);
    }
}

Tosses a compile time error.

*Parameter 'id' of domain method 'DeleteCmsDealerById' must be an entity type exposed by the DomainService, either directly via a query operation, or indirectly via an included association.*

The thing is, I can understand that the (string id) param is not accewptable to EF, but why is it OK in one case and not another?

Input welcome :)

+2  A: 

The convention is that delete methods have a signature that take an entity. A string is not an entity. A entity is a type that a) has a member with a [Key] and b) is a type returned by one of the query methods in the domain service.

Query methods on the other hands do not take entities as parameters. Hence string is an ok parameter for the get query method.

In your case you'll want DeleteCmsDealer to take in a CmdDealer. You can still look up the db inside your method and delete the instance you loaded, rather than attach/delete the instance passed in if that is required.

Hope that helps.

NikhilK
It does indeed... but I guess I am wondering how the Delete method is "known" as a delete method? In this case this is a new method that was not created by the wizard, and I did nothign special to flag it as a delete method.Is it the word "delete"? Or the presence (down at the bottom) of a delete call?The reason for all this is I am comparing a large number of Id's in a remote client, there is no reason to have to ship the actual object down the wire (or onw up) just to do a delete. I know the Id I want to get rid of, so I am trying to ship the least data possible.Does that make sense?
Soulhuntre
Yes, it's the name of the method. See: http://msdn.microsoft.com/en-us/magazine/dd695920.aspx
Craig Stuntz
Craig, that article is an absolute gem! Here is what I was missing..."The naming convention that can be used when defining persistence operations is to prefix your method names with the type of operation. For instance, because the method is called DeleteExpenseReport, the Delete prefix signifies that is it a delete operation by convention. The signature then defines what entity type it is associated with (ExpenseReport)."So had I named it "NukeProductById" all would have been well it seems :)Ken
Soulhuntre