Factory pattern along with DI using Unity container I am learning design patterns and this is my sample code where I have implemented Factory pattern and instance creation I have delegated to Unity container Framework(DI) to build the loosely coupled system.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity;

namespace DesignPatternsDemo
{
    class Program
    {
        static void Main(string[] args)
        {

            iPaymentGateway gateway = PaymentGatwayFactory.NewPaymentGatway(GateWay.SHIFT4);
            gateway.CreatePaymentGateway();
            gateway = PaymentGatwayFactory.NewPaymentGatway(GateWay.WELLSFARGO);
            gateway.CreatePaymentGateway();
            gateway = PaymentGatwayFactory.NewPaymentGatway(GateWay.PROTOBASE);
            gateway.CreatePaymentGateway();
            Console.ReadLine();
        }

        enum GateWay
        {
            PROTOBASE = 1,
            SHIFT4 = 2,
            WELLSFARGO = 3
        }

         //Factory class
        class PaymentGatwayFactory
        {
            public static iPaymentGateway NewPaymentGatway(GateWay GateWayType)
            {
                 //Dependency injection using unity container
                var container = new UnityContainer();
                iPaymentGateway result = null;
                switch (GateWayType)
                {
                    case GateWay.SHIFT4:
                        container.RegisterType&lt;iPaymentGateway, SHIFT4Gateway&gt;();
                        result = container.Resolve&lt;SHIFT4Gateway&gt;();
                        break;
                    case GateWay.WELLSFARGO:
                        container.RegisterType&lt;iPaymentGateway, WellsFargoGateway&gt;();
                        result = container.Resolve&lt;WellsFargoGateway&gt;();
                        break;
                    default:
                        container.RegisterType&lt;iPaymentGateway, ProtobaseGateway&gt;();
                        result = container.Resolve&lt;ProtobaseGateway&gt;();
                        break;
                }

                return result;
            }
        }

        class WellsFargoGateway : iPaymentGateway
        {
            public void CreatePaymentGateway()
            {
                Console.WriteLine("Implement Wells Fargo logic");
            }
        }

        class SHIFT4Gateway : iPaymentGateway
        {
            public void CreatePaymentGateway()
            {
                Console.WriteLine("Implement SHIFT4 logic");
            }
        }

        class ProtobaseGateway : iPaymentGateway
        {
            public void CreatePaymentGateway()
            {
                Console.WriteLine("Implement Protobase logic");
            }
        }

        interface iPaymentGateway
        {
            void CreatePaymentGateway();
        }
    }
}


Is this is the right way of implementing Factory pattern and using the Unity framework?
The following code should tell a story as it is reviewed. There are not many comments as the code has been simplified to be easy to understand as it speaks for itself. I suggest comparing it to what you had before and observe the difference in strategy taken to achieve the factory pattern with DI.

Apart from the composition root, note the separation of concerns and loose coupling of all members involved. The factory in the original post was tightly coupled to the UnityContainer and was also creating an instance every time the factory method was called. The container should only be created once in the composition root of the application.

public interface IPaymentGateway {
    void Invoke();
}

public class WellsFargoGateway : IPaymentGateway {
    public void Invoke() {
        Console.WriteLine("Implement Wells Fargo logic");
    }
}

public class SHIFT4Gateway : IPaymentGateway {
    public void Invoke() {
        Console.WriteLine("Implement SHIFT4 logic");
    }
}

public class ProtobaseGateway : IPaymentGateway {
    public void Invoke() {
        Console.WriteLine("Implement Protobase logic");
    }
}

internal class NullGateway : IPaymentGateway {

    static Lazy&lt;IPaymentGateway&gt; nullObject = new Lazy&lt;IPaymentGateway&gt;(() =&gt; new NullGateway());

    public static IPaymentGateway Empty {
        get { return nullObject.Value; }
    }

    private NullGateway() {

    }

    public void Invoke() {
        //No-op
    }
}


public enum GateWay {
    PROTOBASE = 1,
    SHIFT4 = 2,
    WELLSFARGO = 3
}

public interface IPaymentGatewayFactory {
    IPaymentGateway Create(GateWay GateWayType);
}

public class DefaultPaymentGatewayFactory : IPaymentGatewayFactory {

    readonly Func&lt;GateWay, IPaymentGateway&gt; factoryFactory;

    public DefaultPaymentGatewayFactory(Func&lt;GateWay, IPaymentGateway&gt; factoryFactory) {
        this.factoryFactory = factoryFactory;
    }

    public IPaymentGateway Create(GateWay GateWayType) {
        return factoryFactory(GateWayType);
    }
}

public static class CompositionRoot {
    public static IUnitContainer Configure() {
        //Dependency injection using unity container
        var container = new UnityContainer();
        //Register the gateways using named mappings
        container.RegisterType&lt;IPaymentGateway, WellsFargoGateway&gt;(GateWay.WELLSFARGO.ToString());
        container.RegisterType&lt;IPaymentGateway, SHIFT4Gateway&gt;(GateWay.SHIFT4.ToString());
        container.RegisterType&lt;IPaymentGateway, ProtobaseGateway&gt;(GateWay.PROTOBASE.ToString());
        //create the strategy
        Func&lt;GateWay, IPaymentGateway&gt; factoryFactory = (gatewayType) =&gt;
            container.Resolve&lt;IPaymentGateway&gt;(gatewayType.ToString()) ?? NullGateway.Empty;
        //register factory
        var factory = new DefaultPaymentGatewayFactory(factoryFactory);
        container.RegisterInstance&lt;IPaymentGatewayFactory&gt;(factory);

        return container;
    }

}

class Program {
    static void Main(string[] args) {            
        var container = CompositionRoot.Configure();
        var factory = container.Resolve&lt;IPaymentGatewayFactory&gt;();
        IPaymentGateway gateway = factory.Create(GateWay.SHIFT4);
        gateway.Invoke();
        gateway = factory.Create(GateWay.WELLSFARGO);
        gateway.Invoke();
        gateway = factory.Create(GateWay.PROTOBASE);
        gateway.Invoke();
        Console.ReadLine();
    }
}