Ejemplo n.º 1
0
        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));
            }
        }
Ejemplo n.º 2
0
        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 RegisterBaseType()
        {
            ///创建注册器
            IDependencyRegister register = container.CreateRegister();
            ///注册实现特定接口或基类的类型
            var assembly = register.RegisterAssembly <IDependency>(Assembly.GetExecutingAssembly());

            using (IDependencyResolver resolver = container.CreateResolver())
            {
                var results = resolver.ResolveAll <IDependency>();
                foreach (var value in results)
                {
                    Assert.IsNotNull(value);
                    Assert.IsInstanceOfType(value, typeof(IDependency));
                }
            }
        }
Ejemplo n.º 4
0
        public void Transient()
        {
            ///创建注册器
            IDependencyRegister register = container.CreateRegister();

            ///注册瞬时生命周期的类型
            register.RegisterType <IUserRepository, UserRepository>().AsTransientLifetime();
            ///创建解析器
            using (IDependencyResolver resolver = container.CreateResolver())
            {
                IUserRepository userRepository1 = resolver.Resolve <IUserRepository>();
                IUserRepository userRepository2 = resolver.Resolve <IUserRepository>();

                Assert.AreNotEqual <IUserRepository>(userRepository1, userRepository2);
                ///创建作用域
                using (IScopedResolver scoped = resolver.CreateScopedResolver())
                {
                    IUserRepository userRepository3 = scoped.Resolve <IUserRepository>();

                    Assert.AreNotEqual <IUserRepository>(userRepository1, userRepository3);
                    Assert.AreNotEqual <IUserRepository>(userRepository3, userRepository2);
                }
            }
        }
        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);
            }
        }
Ejemplo n.º 6
0
        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);
            }
        }