using Autofac; public interface IService { void DoSomething(); } public class Service : IService { public void DoSomething() { Console.WriteLine("Doing something..."); } } // 注册服务 var builder = new ContainerBuilder(); builder.RegisterType<Service>().As<IService>(); var container = builder.Build(); // 解析依赖 var service = container.Resolve<IService>(); // 使用服务 service.DoSomething();
using Microsoft.Practices.Unity; public interface IService { void DoSomething(); } public class Service : IService { public void DoSomething() { Console.WriteLine("Doing something..."); } } // 注册服务 var container = new UnityContainer(); container.RegisterType<IService, Service>(); // 解析依赖 var service = container.Resolve<IService>(); // 使用服务 service.DoSomething();
using Ninject; public interface IService { void DoSomething(); } public class Service : IService { public void DoSomething() { Console.WriteLine("Doing something..."); } } // 注册服务 var kernel = new StandardKernel(); kernel.Bind<IService>().To<Service>(); // 解析依赖 var service = kernel.Get<IService>(); // 使用服务 service.DoSomething();
using SimpleInjector; public interface IService { void DoSomething(); } public class Service : IService { public void DoSomething() { Console.WriteLine("Doing something..."); } } // 注册服务 var container = new Container(); container.Register<IService, Service>(); // 解析依赖 var service = container.GetInstance<IService>(); // 使用服务 service.DoSomething();
using Microsoft.Extensions.DependencyInjection; using System; public interface IService { void DoSomething(); } public class Service : IService { public void DoSomething() { Console.WriteLine("Doing something..."); } } class Program { static void Main(string[] args) { // 创建服务集合 var services = new ServiceCollection(); // 注册服务 services.AddTransient<IService, Service>(); // 构建服务提供程序 var serviceProvider = services.BuildServiceProvider(); // 解析依赖 var service = serviceProvider.GetRequiredService<IService>(); // 使用服务 service.DoSomething(); } }
标签:IService,依赖,service,DoSomething,DI,void,var,public,注入 From: https://www.cnblogs.com/LiveCoding/p/17871202.html