none
[asp net core][di] How to inject method/property RRS feed

  • Question

  • I have class

    class App<T>{

    Result<T> Retrieve() {}

    }


    method, and of course i can use it like

    app = new App<Test>;

    app.Retrieve().Add();


    but can i inject App class to use it like below

    class App<T> : Result<T>{

    Result<T> Retrieve() {}

    }


    app = new App<Test>;

    app.Add();

    I hope you understand me

    Saturday, August 4, 2018 5:34 PM

All replies

  • Maybe like this:

    abstract class AppBase<T>
    {
       protected abstract Result<T> Retrieve();
    
       public void Add()
       {
          Retrieve().Add();
       }
    }
    
    
    class App<T> : AppBase<T>
    {
       protected override Result<T> Retrieve()
       {
          . . .
       }
    }
    

    Saturday, August 4, 2018 6:13 PM
  • I agree, it is encapsulation princip. It is bad to expose instance of anything (typically dictionary instance). Somebody can change this instance as need. In result there is unexpected application behavior.
    Saturday, August 4, 2018 8:01 PM
  • You  should use a public Interface for the class. You then use the Interface and class implementing the Interface  in the ConfigureSerrvices() in Startup.cs.

    An example....

    services.AddTransient<IDaoProject, DaoProject>();

    ASP.NET Core can be discussed at the ASP.NET forums.

    http://forums.asp.net/


    • Edited by DA924x Saturday, August 4, 2018 8:53 PM
    Saturday, August 4, 2018 8:47 PM