I have an EF-driven Repository pattern implementation. Roughly looks like this:

public class DataRepository : IDataRepository
  public IDbSet<Contacts> Contacts { get; set; }

I would like to implement a Contact import functionality, which has some custom logic for manipulating the entity which is being imported (i.e. it is different from Adding an entity to the repo).

I see two options right now:

  1. Implement an extension method on the IDbSet<Contacts> Contacts, which would allow me to do:



  2. Implement a static method on the Contact class, which will then allow me to do something like:


    but then I am not sure how I would attach the entity to the context, without breaking my abstraction...

I could also implement an 'Import Service' which wraps this functionality, but it seems very old-school. e.g.:

public class ContactsImportService
  public ContactsImportService(IDataRepository dataRepo)

  public void Import(Contact contact)
    // Manipulate contact here

Any thoughts?

  • \$\begingroup\$ Thanks for the downvotes without a single explanation why. Typical StackExchange. \$\endgroup\$ – hyankov Aug 4 '16 at 7:30
  • 3
    \$\begingroup\$ This is the wrong forum for this kind of questions. You need to present working code if you wish a review. I was just writing this comment to support my downvote but you were quicker ;-P \$\endgroup\$ – t3chb0t Aug 4 '16 at 7:32
  • \$\begingroup\$ I have presented three different versions of a working code. I won't write a complete implementation here of any of them, it's not necessary. And what is the correct forum? \$\endgroup\$ – hyankov Aug 4 '16 at 7:33
  • 6
    \$\begingroup\$ "Thanks for the downvotes without a single explanation why. Typical StackExchange" well I didn't downvote, but its typical for StackExchange that users won't read the help center of the site they are going to ask. \$\endgroup\$ – Heslacher Aug 4 '16 at 7:35
  • \$\begingroup\$ The on-topic and [how-to-ask](/help/how-to-ask] pages should give you a better idea of the kind of questions we're looking for, and how to formulate them well to make the most out of this site. \$\endgroup\$ – janos Aug 4 '16 at 9:31

Implementing an extension method will be a good approach.

However, since you already have a repository layer with IDataRepository & an implementation class DataRepository, the best approach will be to declare a method signature in IDataRepository interface & implement the method in DataRepository class itself.

This will ensure, we are not touching/extending the direct data access objects of entity framework that is IDbSet<Contact>.

Also its always good thing to group the methods working on object sets (aka Add, edit, delete) in a single place like DataRepository (Since you already have this in place).

The import operation as you have specified is specifically for a single entity & I hope its carrying all relevant information for the import within the Contact object itself.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.