lexeme
lexeme

Reputation: 2973

Injecting a factory with multiple constructor parameters

Initially I needed only one queue to be created by the MessageQueueFactory:

container.RegisterSingleton<IMessageQueueFactory>(() => {
    var uploadedWaybillsQueuePath = ConfigurationManager
        .AppSettings["msmq:UploadedDocumentsQueuePath"];
    return new MessageQueueFactory(uploadedWaybillsQueuePath);
});

Now that requirements have changed there's a need to support several queues.

The simplest thing I can do here is to add other paths (stored in app.config) to the factory's constructor and provide methods for each queue:

container.RegisterSingleton<IMessageQueueFactory>(() => {
    var uploadedDocsQueuePath = ConfigurationManager
        .AppSettings["msmq:UploadedDocumentsQueuePath"];
    var requestedDocsQueuePath = ConfigurationManager
        .AppSettings["msmq:RequestedDocumentsQueuePath"];

    return new MessageQueueFactory(
        uploadedWaybillsQueuePath,
        requestedDocsQueuePath
    );
});

interface IMessageQueueFactory {
    MessageQueue CreateUploadedDocsQueue();
    MessageQueue CreateRequestedDocsQueue();
}

Is it a poor design? How can it be refactored?

Upvotes: 0

Views: 101

Answers (1)

Andrei Dragotoniu
Andrei Dragotoniu

Reputation: 6335

I wouldn't consider this bad design. You need to provide the queue name and having it as an appSetting makes it easier to update them if you need to.

It also feels like the less friction path, which is always good, however I don't quite like it because every time you add a new name you have to change the interface and that's not that nice.

I found this post with some answers that might interest you :

IoC - Multiple implementations support for a single interface

Upvotes: 1

Related Questions