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

    Pertanyaan

  • 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

    Sabtu, 04 Agustus 2018 17.34

Semua Balasan

  • 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()
       {
          . . .
       }
    }
    

    Sabtu, 04 Agustus 2018 18.13
  • 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.
    Sabtu, 04 Agustus 2018 20.01
  • 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/


    • Diedit oleh DA924x Sabtu, 04 Agustus 2018 20.53
    Sabtu, 04 Agustus 2018 20.47