In a repository, what is the best and most logical approach?
Deleting using the Entity or Deleting by ID?
public void Delete(Entity item);
VS.
public void Delete(int Id);
I just wanted to know what is the best practice, should i delete by Entity (achieved by finding the object first before deleting) or delete by Id (search the object on the method then delete).
AFAK going with the second choice public void Delete(int Id);
is the best practice after all if your ID
is your primary Key
the Entity will be retrieved or updated or deleted based on this ID, furthermore if you are exposing your repository to WebService( e.g like WCF)
calling a service with an int will be fast and less tedious then calling with an Entity
Update
In addition you can add to your Lead that both of the worlds are disconnected and the entity defined on the Model will not be the same on the client side since you can add properties to your partial class on your prox, it's just SOAP envelope
that will be interpreted on the server side more the envelope is less in size more the communication is fast
Further Reading MSDN
Windows Communication Foundation (WCF) is an XML-based communications infrastructure. Because XML data is commonly encoded in the standard text format defined in the XML 1.0 specification, connected systems developers and architects are typically concerned about the wire footprint (or size) of messages sent across the network, and the text-based encoding of XML poses special challenges for the efficient transfer of binary data.
The problem with passing a domain object as the parameter is you don't always have an instance, so you are forced to create a mock / partial object with just the id, simply to satisfy the method, or you retrieve the object before deleting it. Both are inefficient and ugly.
The Delete method should typically need only the primary key identifier to delete the record.
In some cases I provide both Delete(T) and DeleteByPk(int id)
The downside to exposing the primay key in the signature is that the signatures vary, so trivial in-house code generators need to be smart enough to deal with it. Any 3rd part generators worth using are smart enough.
Delete by ID is your best bet.
The repository pattern is an abstraction of your database technology, so the more you can separate your repository API from your database technology the better.
For instance, if you wanted to switch to a document database such as MongoDB then you'd have to remove references to the Entities from your repository API.
Typically the less information you can send to a method the clearer, more expressive and easier to read it is. By passing the ID your method is quite clear that it's deleting based on the ID of the entity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With