public void AutoPropertyInjection() { ///创建注册器 IDependencyRegister register = container.CreateRegister(); ///注册类型 register.RegisterType <IRepository <UserEntity>, UserRepository>(); register.RegisterType <IUserService, UserService>(); ///注册自动属性 register.RegisterType <ILogger, Logger>().AsPropertyInjection(); ///创建解析器 using (IDependencyResolver resolver = container.CreateResolver()) { IRepository <UserEntity> userRepository = resolver.Resolve <IRepository <UserEntity> >(); var logger = ((UserRepository)userRepository).Logger; Assert.IsInstanceOfType(logger, typeof(Logger)); Assert.AreNotEqual <ILogger>(logger, UnKownLogger.Instance); IUserService useService = resolver.Resolve <IUserService>(); Assert.AreEqual <ILogger>(((UserService)useService).Logger, UnKownLogger.Instance); IRepository <UserEntity> userRepository1 = resolver.Resolve <IRepository <UserEntity> >(); var logger1 = ((UserRepository)userRepository1).Logger; Assert.IsInstanceOfType(logger1, typeof(Logger)); Assert.AreNotEqual <ILogger>(logger1, UnKownLogger.Instance); } }
public void Scoped() { ///创建注册器 IDependencyRegister register = container.CreateRegister(); ///注册瞬时生命周期的类型 register.RegisterType <IUserRepository, UserRepository>().AsScopedLifetime(); ///创建解析器 using (IDependencyResolver resolver = container.CreateResolver()) { IUserRepository userRepository1 = resolver.Resolve <IUserRepository>(); ///创建作用域 using (IScopedResolver scoped = resolver.CreateScopedResolver()) { IUserRepository scopedRepository1 = scoped.Resolve <IUserRepository>(); IUserRepository scopedRepository2 = scoped.Resolve <IUserRepository>(); Assert.AreNotEqual <IUserRepository>(userRepository1, scopedRepository1); Assert.AreEqual <IUserRepository>(scopedRepository1, scopedRepository2); ///创建作用域 using (IScopedResolver scoped1 = scoped.CreateScopedResolver()) { IUserRepository scopedRepository3 = scoped1.Resolve <IUserRepository>(); IUserRepository scopedRepository4 = scoped1.Resolve <IUserRepository>(); Assert.AreNotEqual <IUserRepository>(userRepository1, scopedRepository3); Assert.AreNotEqual <IUserRepository>(scopedRepository1, scopedRepository3); Assert.AreEqual <IUserRepository>(scopedRepository3, scopedRepository4); } } } }
/// <summary> /// 注册 Type /// </summary> public static IDependencyRegistration RegisterType(this IDependencyRegister dependencyRegister, Type serviceType) { if (serviceType == null) { throw new ArgumentNullException(nameof(serviceType)); } return(dependencyRegister.RegisterType(serviceType, serviceType)); }
public void Constructor() { ///创建注册器 IDependencyRegister register = container.CreateRegister(); ///注册类型 register.RegisterType <IRepository <UserEntity>, UserRepository>(); register.RegisterType <IUserService, UserService>(); ///创建解析器 using (IDependencyResolver resolver = container.CreateResolver()) { IUserService service = resolver.Resolve <IUserService>(); UserEntity entity = service.GetById(1); Assert.IsNotNull(entity); Assert.AreEqual(entity.Id, 1); } }
public void ResolverCallSiteExtensions() { ///创建注册器 IDependencyRegister register = container.CreateRegister(); ///注册类型 register.RegisterType <IRepository <UserEntity>, UserRepository>(); register.RegisterType <IUserService, UserService>(); ///创建解析器 using (IDependencyResolver resolver = container.CreateResolver()) { ///清除默认解析器 resolver.RemoveAllCallSites(); ///添加自定义解析器 resolver.AddCallSite(new CustomResolverCallSite()); IUserService service = resolver.Resolve <IUserService>(); Assert.IsNotNull(service); } }
public void RegisterType() { ///创建注册器 IDependencyRegister register = container.CreateRegister(); ///注册类型 register.RegisterType <IUserRepository, UserRepository>(); ///创建解析器 using (IDependencyResolver resolver = container.CreateResolver()) { IUserRepository userRepository = resolver.Resolve <IUserRepository>(); Assert.IsNotNull(userRepository); Assert.IsInstanceOfType(userRepository, typeof(IUserRepository)); } }
/// <summary> /// 注册 Type /// </summary> public static IDependencyRegistration RegisterType <TService>(this IDependencyRegister dependencyRegister) where TService : class { return(dependencyRegister.RegisterType(typeof(TService))); }
/// <summary> /// 注册 Type /// </summary> public static IDependencyRegistration RegisterType <TService, TImplementation>(this IDependencyRegister dependencyRegister) where TService : class where TImplementation : class, TService { return(dependencyRegister.RegisterType(typeof(TService), typeof(TImplementation))); }
/// <summary> /// 注册类型为依赖服务 /// </summary> public static IDependencyRegistration RegisterType <TService>(this IDependencyRegister dependencyRegister) => dependencyRegister.RegisterType(typeof(TService));
/// <summary> /// 注册类型和实现类型为依赖服务 /// </summary> public static IDependencyRegistration RegisterType <TService, TImplementation>( this IDependencyRegister dependencyRegister) where TImplementation : TService => dependencyRegister.RegisterType(typeof(TService), typeof(TImplementation));