I am in the process of converting some of my code to MEF from a proprietary system that sort of does the same thing as MEF, and I have a question about how I would accomplish the following problem that I recently ran into.
I have a typical entity object that looks something like this:
public class Account {
[Import]
public IAccountServerService { get; set; }
}
And a service object that needs to be imported in to the above entity object:
public class AccountServerService : IAccountServerService {
[ImportingConstructor]
public AccountServerService (Account account) { ... }
}
To put this into words I need the account
parameter passed into the AccountServerService
constructor instance to be the object of the calling Account
object. So that it act like this:
public class Account {
public IAccountServerService { get { return new AccountServerService (this); } }
}
Please let me know if this scenario is possible or if I have to refactor my service interface in this instance.
So MEF does support circular dependencies but they must both be property imports, neither of them can be constructor imports. So the following should work from a MEF standpoint, of course I'm not sure if this approach is blocked by some other constraint you have.
public class AccountServerService : IAccountServerService {
[Import]
public Account Account { get; set; }
public AccountServerService () { ... }
}
public class Account {
[Import]
public IAccountServerService { get; set; }
}