locked
Dependency in WCF RRS feed

  • Question

  • User1954082887 posted

    I have a wcf service class which calls my business manager & the manager eventually calls dataaccess layer.
    Now the Business Manager implement interface whose object I want to access in my Service implementation.

    Scenario at present

    public DemoService : IDemoService
    {
    	public int Add(int x, int y)
    	{
    		int result=0;
    		IDemoManager manager = new DemoManager();
    		result = manager.Add(x, y);
    		
    		return result;
    	}
    }
    public IDemoManager
    {
    	int Add(int x, int y);
    }
    
    public DemoManager : IDemoManager
    {
    	public int Add(int x, int y)
    	{
    		return x+y;
    	}
    }

    Now here I want to skip the use of concrete class DemoManager, otherwise there is no use of interface IDemoManager.

    Please help how can I achive the same.

    Monday, April 1, 2013 3:10 AM

Answers

  • User220959680 posted

    by above statement I meant parameterized constructor from client. So it is hard to provide Constructor Injection for manager from client.

    Below is provided by assuming that it is SOAP based WCF service.

    Again It is NOT required to create an instance of service in client side to access service methods. Service proxy class (Example: DemoServiceClient), which gets created when the Service reference is added to the project in client side. Serivce proxy class is the communication channel between the service and the client with all the meta data.

    In this scenario when the service reference is added in the client side proxy class gets created, usually service name appended with the Client i.e., DemoServiceClient (for DemoService). So no issue of parametrized constructor in client side.

    some reason don't want to switch to any IoC.

    IoC containers are widely used to implement DI pattern. In the second response in this thread a reference is provided to handle DI, which provides various alternatives to handle DI.

    This is the concluded response to this thread from me. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 2, 2013 8:12 AM

All replies

  • User220959680 posted

    IDemoManager manager = new DemoManager();

    want to skip the use of concrete class

    Above can be achieved by Depency Injection pattern by using Inversion of Control technique with number of alternative tools such as

    Structure map, Microsoft's Unity, castle window etc.

    Note that there are various ways of implementing DI:

    • Constructor way
    • Exposing setter and getter
    • Interface implementation
    • Service locator

    Refer http://www.codeproject.com/Articles/29271/Design-pattern-Inversion-of-control-and-Dependency

    Solution:

    //Service layer
    public DemoService : IDemoService
    {
            //IDemoManager from Business Layer
            private readonly IDemoManager demoManager;
           
            //Inject depency in constructor
            public DemoService(IDemoManager demoManager)
            {
              this.demoManager = demoManager; 
            } 
            
            public int Add(int x, int y)
            {
                    int result=0;
                    //Access method through the Interface
                    result = demoManager.Add(x, y);
                    
                    return result;
            }
    }
    
    //Global.asax or boot strapper class utilises IOC tools such as Unity, structure map etc to register
    //Interfaces to concrete implementation

    Refer http://prashantbrall.wordpress.com/2010/10/11/dependency-injection-in-wcf-service/ to utilise Castle Windsor to achive the above.

    Monday, April 1, 2013 7:01 PM
  • User1954082887 posted

    //Inject depency in constructor
            public DemoService(IDemoManager demoManager)
            {
              this.demoManager = demoManager; 
            } 

    I know about these dependency injection, but I don't think we call the constructor of wcf service while accessing at client.

    Tuesday, April 2, 2013 1:56 AM
  • User220959680 posted

    I don't think we call the constructor of wcf service while accessing at client

    Client side an instance of service proxy class is created to access methods. Not the service itself.

    DemoServiceClient client = new DemoServiceClient();
    
    try
    {
    client.Open();
    
    client.Add(1,2);
    
    client.Close();
    }
    catch(FaultException)
    {
      //
    }
    catch(communicationException)
    {
      //
    }

    Reference in initial response works as expected where depency is injected through constructor and Castle windsor container used to register Interface and concrete implementation.

    Below is another implementation using Structure map

    //Service layer
    public DemoService : IDemoService
    {
       //Declare an instance of DL interface
       IDemoManagerDL _demoManagerDL;
       
       //Declare an instance of BL interface
       IDemoManager _demoManager;
    
       
      #region
       
       // Default constructor to initialize boot strapper
       static DemoService()
       {
         Bootstrapper();
       }
    
       // Empty constructor for initializing the service
       public DemoService : this(ObjectFactory.GetInstance<IDemoManager>(), ObjectFactory.GetInstance<IDemoManager())
       { }
      
       //Overloaded constructor for initializing business layer object
       public DemoService(IDemoManager demoManager, IDemoManagerDL demoManagerDL)
       {
          _demoManager = demoManager;
          _demoManagerDL = demoManagerDL;
       }
      
       #endregion
    
       //Bootstrapper initalizes all object mappings
       private static void Bootstrapper()
       {
          // IoC initialization...
                ObjectFactory.Initialize(
                   x =>
                   {
                       x.For<IDemoManager>().Use<DemoManager>();
                       X.For<IDemoManagerDL().Use<DemoManagerDL();
                    });
       }
    
       #region Service methods
       public int Add(int x, int y)
            {
                    int result=0;
                    //Access method through the BL Interface
                    result = demoManager.Add(x, y);
                    
                    return result;
            }
       #endregion
    
    
    }
    
    



    Refer http://angadbhat.com/2011/11/8/ioc-with-structuremap-in-wcf-service.aspx for Structure map as Ioc container.

    Tuesday, April 2, 2013 6:52 AM
  • User1954082887 posted

    thanks sukumarraju

    I don't think we call the constructor of wcf service while accessing at client

    by above statement I meant parameterized constructor from client. So it is hard to provide Constructor Injection for manager from client.

    //Inject depency in constructor
            public DemoService(IDemoManager demoManager)
            {
              this.demoManager = demoManager; 
            } 
            

    And for some reason don't want to switch to any IoC. Unless there any other solution available thru dependency Injection etc..

    Tuesday, April 2, 2013 7:13 AM
  • User220959680 posted

    by above statement I meant parameterized constructor from client. So it is hard to provide Constructor Injection for manager from client.

    Below is provided by assuming that it is SOAP based WCF service.

    Again It is NOT required to create an instance of service in client side to access service methods. Service proxy class (Example: DemoServiceClient), which gets created when the Service reference is added to the project in client side. Serivce proxy class is the communication channel between the service and the client with all the meta data.

    In this scenario when the service reference is added in the client side proxy class gets created, usually service name appended with the Client i.e., DemoServiceClient (for DemoService). So no issue of parametrized constructor in client side.

    some reason don't want to switch to any IoC.

    IoC containers are widely used to implement DI pattern. In the second response in this thread a reference is provided to handle DI, which provides various alternatives to handle DI.

    This is the concluded response to this thread from me. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 2, 2013 8:12 AM