public MongoDBWriteDataTest() { _dependencyRegister = new UnityDependencyRegister(); ApplicationConfig.SetDependencyResolver(_dependencyRegister.GetResolver()); _dependencyRegister.Register <IDBService, MongoDBService>(); _dependencyRegister.Register <IJSONValidator, JSONValidator>(); _dependencyRegister.RegisterInstance <IAppSettingService>(AppSettingService.Instance); _dependencyRegister.Register <IEncryption, EncryptionService>(); _dependencyRegister.RegisterInstance <IViewEngine>(RazorTemplateEngine.GetEngine()); _dependencyRegister.Register <IKeyValueStorage, FileKeyValueFileStorage>(); }
/// <summary> /// 注册 实例 /// </summary> public static IDependencyRegistration RegisterInstance(this IDependencyRegister dependencyRegister, Object instance) { if (instance == null) { throw new ArgumentNullException(nameof(instance)); } return(dependencyRegister.RegisterInstance(instance.GetType(), instance)); }
/// <summary> /// 注册 实例 /// </summary> public static IDependencyRegistration RegisterInstance <TService>(this IDependencyRegister dependencyRegister, Object instance) where TService : class { if (instance == null) { throw new ArgumentNullException(nameof(instance)); } return(dependencyRegister.RegisterInstance(typeof(TService), instance)); }
public void RegisterInstance() { ///创建注册器 IDependencyRegister register = container.CreateRegister(); ///注册类型的实例 UserRepository instance = new UserRepository(); register.RegisterInstance(typeof(IUserRepository), instance); ///创建解析器 using (IDependencyResolver resolver = container.CreateResolver()) { IUserRepository userRepository = resolver.Resolve <IUserRepository>(); Assert.IsNotNull(userRepository); Assert.IsInstanceOfType(userRepository, typeof(IUserRepository)); } }