none
Resharper: Delegate implementation of IExampleInterface to new field. RRS feed

  • Question

  • Hi everyone, i just wanna ask how to use the generated code by Resharper. Aside from Implementing the missing members, Resharper is suggesting me to Delegate implementation of my interface to a new field as well. After trying to delegate it, it generated some code on my behalf like this.

    public interface IProductCreate
                        {
                            void Create(Model.Products products);
                        }
    
                        public class Test : IProductCreate
                        {
                            private IProductCreate _productCreateImplementation;
                            public void Create(Products products)
                            {
                                _productCreateImplementation.Create(products);
                            }
                        }

    I don't have any idea how to use this class. 

    I would love to hear some explanation about this. 

    Regards,

    Tuesday, October 31, 2017 1:02 PM

Answers

  • ReSharper isn't recommending that you do that, it is just providing you suggestions of different ways to implement the interface. The normal approach is to simply implement the interface yourself (option 1). But you could also create a separate class to implement the interface and then have your public class do that. I wouldn't generally recommend going that route as it adds complexity with little benefit. Only in the case of a single class that implements many different interfaces (which is a bad design) should you probably consider using an impl class. I'd remove that code and simply implement the interface directly.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Dikong42 Tuesday, October 31, 2017 2:56 PM
    Tuesday, October 31, 2017 2:06 PM
    Moderator
  • Just because, Resharper makes a suggestion on a possible coding change, it doesn't mean that you have to  implement it.

    However, you should be using class contactor injection to use the class, called dependency injection.

     public class TheTest
     {
       private IProductCreate _productCreateImplementation;
    
       public TheTest(IProductCreate prodCreate)
       {
           _productCreateImplementation = prodCreate;
       }
    
       public void Create(Products products)
       {
          _productCreateImplementation.Create(products);
       }
    }

    var obj = new theTest(new ProductCreate);

    obj.Create(Products);

    -----------------------------------------------------------

    You need another class called ProductCreate that implements IProductCreate;

     

    • Marked as answer by Dikong42 Tuesday, October 31, 2017 2:55 PM
    Tuesday, October 31, 2017 2:24 PM

All replies

  • ReSharper isn't recommending that you do that, it is just providing you suggestions of different ways to implement the interface. The normal approach is to simply implement the interface yourself (option 1). But you could also create a separate class to implement the interface and then have your public class do that. I wouldn't generally recommend going that route as it adds complexity with little benefit. Only in the case of a single class that implements many different interfaces (which is a bad design) should you probably consider using an impl class. I'd remove that code and simply implement the interface directly.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Dikong42 Tuesday, October 31, 2017 2:56 PM
    Tuesday, October 31, 2017 2:06 PM
    Moderator
  • Just because, Resharper makes a suggestion on a possible coding change, it doesn't mean that you have to  implement it.

    However, you should be using class contactor injection to use the class, called dependency injection.

     public class TheTest
     {
       private IProductCreate _productCreateImplementation;
    
       public TheTest(IProductCreate prodCreate)
       {
           _productCreateImplementation = prodCreate;
       }
    
       public void Create(Products products)
       {
          _productCreateImplementation.Create(products);
       }
    }

    var obj = new theTest(new ProductCreate);

    obj.Create(Products);

    -----------------------------------------------------------

    You need another class called ProductCreate that implements IProductCreate;

     

    • Marked as answer by Dikong42 Tuesday, October 31, 2017 2:55 PM
    Tuesday, October 31, 2017 2:24 PM
  • This is easier to  understand than the generated code. Thanks bud.
    Tuesday, October 31, 2017 2:55 PM