Reputation: 16209
in my project I implemented a UdpListener
. It is designed to receive udp packets for multiple IPs (multicast groups) at a specific port.
I'm wondering about whether to inject an object (event collection with multicastGroupIPs) by using the constructor (public UdpListener(int port, EventList<IPAddress> multicastGroups)
) or provide two methods on the interface for JoinMulticastGroup
and RemoveMulticastGroup
.
I'm not sure which way is cleaner, so I'm interested in your opinion.
Edit:
I want to use Unity DI. So the port
is provided by the config file, but the multicastGroupIPs
object by code.
Upvotes: 0
Views: 188
Reputation: 399
It is sometimes a matter of taste whether to use constructor or setter/method call injection.
In this case you should consider if the multicast group collection is mutable. If you can be sure it will never change you can pass it to the constructor. Having immutable state is always good practice.
On the other hand the constructor should only require what is really needed to create a valid object of some type. Perhaps you can find some default value but I think those multicast group are needed for your listener to function.
So if the collection is immutable pass it to the constructor. But if it is common to all listeners implementing the interface choose the method calls as you can never be sure.
Upvotes: 3