Wednesday, June 7, 2017

Implementing Service Locator (To Resolve Dependency)

using System;

/// <summary>
/// Summary description for Class1
/// </summary>
public class serviceLocator
{
    public serviceLocator()
{
// TODO: Add constructor logic here
}


    //Interface
    public interface IService
    {
        void Service_Starting();
        void Service_Completed();
    }


    //Implement Interface in the Service Class
    public class Service : IService
    {
        public void Service_Starting()
        {
            Console.WriteLine("Service Starting Method is calling : Service_Starting.............");
            Console.ReadLine();
        }

        public void Service_Completed()
        {
            Console.WriteLine("Service completed Method is calling : Service_Completed Method...............");
            Console.ReadLine();
        }
    }


    //Service Locator Class
    public static class LocateService
    {
        public static IService _Service { get; set; }

        public static IService GetService()
        {
            if (_Service == null)
                _Service = new Service();

            return _Service;
        }
    }


    //Client Class
    public class Client
    {
        private IService _service;

        public Client()
        {
            this._service = LocateService.GetService();
        }

        public void Start()
        {
            Console.WriteLine("Client :Method is Calling.........\n");
            this._service.Service_Starting();
            this._service.Service_Completed();
            Console.ReadLine();
        }
    }


    //Main Programm
    class Program
    {
        static void Main(string[] args)
        {
            var client = new Client();
            client.Start();
            Console.ReadKey();
        }
    }
}

Implementing Service Locator (To Resolve Dependency)

using System; /// <summary> /// Summary description for Class1 /// </summary> public class serviceLocator {     public s...