1

Previously I asked this question and on a answer I got this comment:

This works, however injecting the container to a part, as far as I know, is not a "normal" use-case of MEF.

In my web app I have a few repositories that, of course, retrieve entities from the DB. To make them as loosely coupled as possible I'm making them return interfaces (eg IUser, IBill, IBlaBlaBla...) and the repository project only references the library project (that contains the interfaces). I use MEF composition capabilities to tie it all up...

Since the repository must have a concrete object to fill with info it got from the DB and the only the Container is aware of which concrete class maps to a specific interface I think that the repository MUST have reference to the container so it can call the "Resolve()", get a new instance and do his job, but that apparently is a mistake.

Can anyone tell me why and what approach would be better?

PS: I don't know if it's relevant but I'm using DDD...

Community
  • 1
  • 1
Leonardo
  • 10,737
  • 10
  • 62
  • 155

1 Answers1

1

I think the flaw in your design that lead to this problem is the use of interfaces to hide entities behind. Since entities are your core concept in your domain, there should be no use in hiding them behind an abstraction. Let's put it differently: do you ever have a different implementation of IUser?

In other words, ditch the IUser, IBill, etc. interface and let your repositories and business commands depend directly on your aggregate entities.

Steven
  • 166,672
  • 24
  • 332
  • 435
  • well, there is the UserTest, BillTest, etcTest (for unit testing...). The thing is that if I replace the interface for the concrete class I create a bunch of tight coupled classes... and I think that's worst because now the only coupling is in the "container" class no? – Leonardo Nov 28 '13 at 18:25
  • 1
    No. Steven is correct. Domain entities should have no external dependencies. The are the core on which everything else should be dependent of.. Look for instance at Onion Architecture or Domain Driven Design. – jgauffin Nov 28 '13 at 18:46
  • @jgauffin in a way the Entities does not directly have external refs... they reference the repository via I*Repository interface (the concrete class of the repository is injected) and the repositories reference the container... Any better now? – Leonardo Nov 29 '13 at 12:40