c# - How to replace factory pattern with dependency injection -
i have following situation: collection of objects have sent different third parties based on specific property of each object (the property defined enum). intend implement using factory pattern below.
can refactored use dependency injection instead?
public class servicea: ithirdparty { public void send(ticket objecttobesent) { // call third party made send ticket } } public class serviceb: ithirdparty { public void send(ticket objecttobesent) { // call third party made send ticket } } public interface ithirdparty { void send(ticket objecttobesent); } public static class thirdpartyfactory { public static void sendincident(ticket objecttobesent) { ithirdparty thirdpartyservice = getthirdpartyservice(objecttobesent.thirdpartyid); thirdpartyservice.send(objecttobesent); } private static ithirdparty getthirdpartyservice(thirdparty thirdparty) { switch (thirdparty) { case thirdparty.aaa: return new servicea(); default: return new serviceb(); } } }
yes can refactored - inject service sendincident, or class.
Comments
Post a Comment