Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Startup Dependency Injection when class has muliple interfaces

$
0
0

I have a single class that supports multiple interfaces. I'd like to instantiate the class once on a request to inject the interface. A request to inject the other interfaces should draw on the existing instantiated class. Is this possible?   As it stands now, I believe that each time a different interface is called, a new class is instantiated. This would leave 3 instances in memory - wasteful!

Class Definition

  public class BaseApi : IInterface1, IInterface2, IInterface3
  {
        public BaseApi(string root)
        {
            Root = root;
        }
        public string Root { get; set; }

}

Startup.cs

        public void ConfigureServices(IServiceCollection services)
        {

            ...

            string root = "Test";
            //TODO: find out how to establish object once and use 3 different interfaces
            services.AddSingleton<IInterface1>(p => new BaseApi(root));
            services.AddSingleton<IInterface2>(p => new BaseApi(root));
            services.AddSingleton<IInterface3>(p => new BaseApi(root));

        }


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>