I am working on EWS. A class is made to query mail box and read emails.
public class MailReader
{
private readonly ExchangeService _service;
private readonly PropertySet _propertySet;
private readonly IFolderSearch _folder;
private readonly IMailAttachmentAdapter _attachment;
private const int MaxEmail = 1000;
public MailReader(
ExchangeService service,
PropertySet propertySet,
IFolderSearch folder,
IMailAttachmentAdapter attachment)
{
_service = service;
_folder = folder;
_propertySet = propertySet;
_attachment = attachment;
}
what is the best way to inject ExchangeService
, PropertySet
classes are sealed and with no interface. Planning to use AutoFac for dependency management.
any pointers will be helpful.
-
1If you don't need to substitute these dependencies, then injecting the concrete classes like you do seem fine.JacquesB– JacquesB2018年02月15日 15:29:51 +00:00Commented Feb 15, 2018 at 15:29
-
2I would hit the vendor with a club, a big one. Who does distribute concrete classes with no interface in 2018? The 80s have called...Christian Sauer– Christian Sauer2018年02月16日 04:51:57 +00:00Commented Feb 16, 2018 at 4:51
-
1Create the interface yourself, has the added benefit of decoupling.S.D.– S.D.2018年02月16日 06:36:45 +00:00Commented Feb 16, 2018 at 6:36
1 Answer 1
One of the simplest, but long-winded, ways to solve this is to use the delegation pattern:
interface IDelegatedExchangeService
{
bool AcceptGzipEncoding { get; set; }
string ClientRequestId { get; set; }
...
}
class ConcreteExchangeService : IDelegatedExchangeService
{
private ExchangeService _exchangeService;
public bool AcceptGzipEncoding
{
get => _exchangeService.AcceptGzipEncoding;
set => _exchangeService.AcceptGzipEncoding = value;
}
...
}
and to then use IDelegatedExchangeService
throughout your code.
answered Feb 15, 2018 at 15:00
lang-cs