-5
\$\begingroup\$

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:

    dataRepository.Contacts.Import(contact);

    dataRepository.Save();

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

    Contact.Import(contact);

    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
    this.dataRepo.Add(contact);
    this.dataRepo.Save();
  }
}

Any thoughts?

\$\endgroup\$

closed as off-topic by t3chb0t, Heslacher, 200_success Aug 4 '16 at 7:50

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\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
4
\$\begingroup\$

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.

\$\endgroup\$

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