Ejemplo n.º 1
0
        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>
        ///    获取缓存
        /// </summary>
        public static Dependency GetCache(IScopedResolver key, Type serviceType)
        {
            var dependencyEntries = new DependencyCacheManager(key).GetValue();

            return(dependencyEntries.ContainsKey(serviceType)
                ? dependencyEntries[serviceType]
                : default(Dependency));
        }
 /// <summary>
 ///     删除缓存
 /// </summary>
 public static void RemoveCache(IScopedResolver key)
 {
     GetCache(key).ForEach(PropertyCacheManager.RemoveCache);
     new DependencyCacheManager(key).RemoveLock();
 }
 /// <summary>
 ///    获取缓存
 /// </summary>
 public static IEnumerable <Dependency> GetCache(IScopedResolver key)
 => new DependencyCacheManager(key).GetValue().Values;
        /// <summary>
        ///     设置缓存
        /// </summary>
        public static void SetCache(IScopedResolver key, IEnumerable <Dependency> dependencyEntries)
        {
            var cache = new DependencyCacheManager(key).GetValue();

            dependencyEntries.ForEach(dependency => cache.Add(dependency.ServiceType, dependency));
        }
 private DependencyCacheManager(IScopedResolver key)
     : base(key)
 {
 }
Ejemplo n.º 7
0
 private DependencyResolver(IScopedResolver root, IEnumerable <Dependency> dependencyEntries)
 {
     RootScoped = root;
     DependencyCacheManager.SetCache(this, dependencyEntries);
     InitializationCallSite();
 }
Ejemplo n.º 8
0
 public DependencyResolver(IEnumerable <Dependency> dependencyEntries)
 {
     RootScoped = this;
     DependencyCacheManager.SetCache(this, dependencyEntries);
     InitializationCallSite();
 }
 private DependencyResolver(IScopedResolver root, IEnumerable<Dependency> dependencyEntries)
 {
     RootScoped = root;
     DependencyCacheManager.SetCache(this, dependencyEntries);
     InitializationCallSite();
 }
Ejemplo n.º 10
0
 public DependencyResolver(IEnumerable<Dependency> dependencyEntries)
 {
     RootScoped = this;
     DependencyCacheManager.SetCache(this, dependencyEntries);
     InitializationCallSite();
 }