标签: circular-dependency

Python:类型检查所需的循环导入

首先:我知道已经有很多关于循环进口主题的问题和答案.

答案或多或少是:"正确设计模块/类结构,您不需要循环导入".那是真实的.我努力为我当前的项目做一个合适的设计,我认为我是成功的.

但我的具体问题如下:我需要在模块中进行类型检查,该模块已由包含要检查的类的模块导入.但这会引发导入错误.

像这样:

foo.py:

from bar import Bar

class Foo(object):

    def __init__(self):
        self.__bar = Bar(self)
Run Code Online (Sandbox Code Playgroud)

bar.py:

from foo import Foo

class Bar(object):

    def __init__(self, arg_instance_of_foo):
        if not isinstance(arg_instance_of_foo, Foo):
            raise TypeError()
Run Code Online (Sandbox Code Playgroud)

解决方案1:如果我修改它以通过字符串比较检查类型,它将起作用.但我真的不喜欢这个解决方案(对于简单的类型检查,字符串比较相当昂贵,并且在重构时可能会遇到问题).

bar_modified.py:

from foo import Foo

class Bar(object):

    def __init__(self, arg_instance_of_foo):
        if not arg_instance_of_foo.__class__.__name__ == "Foo":
            raise TypeError()
Run Code Online (Sandbox Code Playgroud)

解决方案2:我也可以将这两个类打包成一个模块.但我的项目有许多不同的类,如"Bar"示例,我想将它们分成不同的模块文件.

在我自己的2个解决方案对我来说没有选择之前:有没有人为这个问题找到更好的解决方案?

python import circular-dependency

8
推荐指数
2
解决办法
2009
查看次数

寻找spring bean之间循环依赖关系的良好实践

我有这个例外:

SEVERE: Context initialization failedorg.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'myService': Bean with name 'myService' has been injected into other beans [otherService] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.
Run Code Online (Sandbox Code Playgroud)

任何人都可以建议找到循环依赖来源的好策略吗?

我目前正在查看上下文定义,但正如您可能想象的那样,在某个成熟度的项目中,这需要相当长的时间.

所以我一般都在寻找快速找到循环bean依赖关系的想法.

java spring circular-dependency

8
推荐指数
1
解决办法
8823
查看次数

MVC - 循环依赖

我需要视图来保存对控制器的引用,因为它需要将控制器注册为事件监听器.我需要控制器来保存对视图的引用,因为在按钮单击时,我需要能够在列表中获取所选文件.(我有一个文件列表和一个"添加群集"按钮,所以当点击按钮时我需要获取所选文件)

所以总之我有:

Controller controller(view);
View view(controller);
Run Code Online (Sandbox Code Playgroud)

我确定这里有一些不好的设计,我只是想不通如何避免它..

java model-view-controller circular-dependency

8
推荐指数
2
解决办法
1672
查看次数

Typedef循环依赖

如何解决以下循环依赖?

typedef boost::variant<int, bool, double, std::string, Structure> Value;
typedef std::list<std::pair<std::string, ValueContainer>> Structure;
typedef std::vector<Value> ValueContainer;
Run Code Online (Sandbox Code Playgroud)

我试图用更多C++形式表示来自C api数据库库的对象.此数据库允许存储值或值数组,以及具有Structures的表示,如下所示:

typedef struct ApiStructureMember 
{
    char* name;
    struct ApiValueContainer value;
    struct ApiStructureMember_T* next;
} ApiStructureMember_T;
Run Code Online (Sandbox Code Playgroud)

最后,union用于表示值,如下所示:

typedef struct ApiValue 
{
    union 
    {
        int i;
        const char*   s;
        ...
        struct ApiStructureMember_T* firstStructureMember;
    } value;
} ApiValue_T; 
Run Code Online (Sandbox Code Playgroud)

c++ circular-dependency forward-declaration

8
推荐指数
1
解决办法
1544
查看次数

为什么C#中的类没有循环布局问题?

public struct Unit
{
    Unit u;
}
Run Code Online (Sandbox Code Playgroud)

原因:

类型为"单位"的结构成员"Unit.u"会在结构布局中生成一个循环.

public class Unit
{
    Unit u;
}
Run Code Online (Sandbox Code Playgroud)

编译.我理解我认为的问题.引用Unit对象时将形成无限循环,因为它必须初始化另一个成员Unit,依此类推.但是为什么编译器只是为了限制问题structs呢?这个问题也不存在class吗?我错过了什么吗?

c# struct class circular-dependency cyclic-reference

8
推荐指数
1
解决办法
317
查看次数

Visual Studio 2012 - 有效地查找循环引用

目前,如果我想在我选择的解决方案中检查循环引用Architecture - Generate Dependency Graph - For Solution.然后从我打开的新选项卡中选择Layout - Analyzers - Circular References Analyzer.最后,如果我从单个程序集向下钻取并且有循环引用,我可以在图表上看到它们以红色突出显示,它们也在错误列表中显示为警告.

由于我打算在相同类的方法之间发现循环引用,因此在适度大的代码库上这非常容易出错且耗时.

我想知道是否有一种方法可以立即获取所有警告,而无需扩展节点或者可能打开父节点的突出显示,这样我就可以只查看肯定包含循环引用的汇编.

NDepend应该能够提供帮助,但我更喜欢让事情变得尽可能简单,所以我总是对采用其他工具持谨慎态度.

.net circular-dependency circular-reference visual-studio visual-studio-2012

8
推荐指数
1
解决办法
6451
查看次数

删除python循环导入

user.py:

from story import Story

class User:
    ...
    def get_stories(self):
        story_ids = [select from database]
        return [Story.get_by_id(id) for id in story_ids]
Run Code Online (Sandbox Code Playgroud)

story.py

from user import User

class Story:
    ...
    def __init__(self, id, user_id, content):
        self.id = id
        self.user = User.get_by_id(user_id)
        self.content = content
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,这个程序中有一个循环导入,导致一个ImportError.我了解到我可以在方法定义中移动import语句以防止出现此错误.但是我仍然想知道,在这种情况下有没有办法去除循环导入,或者,是否有必要(对于一个好的设计)?

python import circular-dependency

8
推荐指数
1
解决办法
820
查看次数

为什么所有领先的开源Java库在它们的包中都有循环依赖?

在过去的几周里,我一直在研究Java包结构和依赖模式.关于该主题的着作中的一个共同点是包依赖应该形成有向无环图(DAG)的简单规则.作者罗伯特马丁甚至正式确定了非循环依赖原则(ADP),该原则指出

包之间的依赖关系结构必须是有向无环图(DAG).也就是说,依赖结构中必须没有循环.

一些Java库确实遵循这个简单的规则.即Spring Framework库(spring-core,spring-web等)和Google Guava.

但令我惊讶的是,大多数领先的开源Java项目都没有!

以下开源项目在包之间具有循环依赖关系:

  • Netflix Hystrix(每个包都是一个循环的一部分!)
  • AWS开发SDK
  • 下议院郎
  • 共享的集合
  • 匕首
  • 谷歌Gson
  • 谷歌Guice
  • Hibernate ORM
  • Hibernate Validator
  • 杰克逊核心
  • 乔达时间
  • 玩框架
  • Junit的
  • 的logback
  • 码头
  • AspectJ的
  • 网状
  • java.util中
  • java.lang中

我误解了软件工程原理吗?或者开发商是否对此包装组织技术打折?

参考文献:

java spring dependencies circular-dependency package

8
推荐指数
1
解决办法
501
查看次数

Angular 4:无法实例化循环依赖!InjectionToken_HTTP_INTERCEPTORS

我知道,这个问题可能听起来很复杂,我已经尝试了在stackover流程中发现的所有内容都无法解决这个问题,所以请耐心等待

为了让你能够重现错误,我为你提供了整个代码

Github Repo

问题

我收到以下错误:

提供者解析错误:↵无法实例化循环依赖!InjectionToken_HTTP_INTERCEPTORS("[ERROR - >]"):在./AppModule@-1:-1中的NgModule AppModule中

在此输入图像描述

有关场景的信息(注释)

注1 文件:response-interceptor.service.ts

路径: ./src/app/shared/interceptors/response-interceptor/

我正在拦截HTTPClient检查401错误的响应,当错误发生时我需要让用户重新登录.

要向用户显示重新登录提示,我已经创建了global-functions-services一个具有"relogin"功能的功能

注2 文件:global-function.service.ts

路径: ./src/app/shared/services/helper-services/global-function/

这是所有这一切开始发生的地方......我一注射了 PersonService

  constructor(
    public dialog: MatDialog,
    private _personService: PersonService
  ) { }
Run Code Online (Sandbox Code Playgroud)

我收到此错误,在PersonService中我找不到任何import可能导致此问题的错误.

PersonService:
./src/app/shared/services/api-services/person/person.service.ts

import { IRequest } from './../../../interfaces/I-request';
import { environment } from 'environments/environment';
import { Injectable } from '@angular/core';

// for service
import 'rxjs/add/operator/map'
import 'rxjs/add/operator/toPromise';

// models
import { Person …
Run Code Online (Sandbox Code Playgroud)

javascript circular-dependency angular

8
推荐指数
3
解决办法
2万
查看次数

当基类返回可导入模块中的子类实例时,避免循环导入

概括

TLDR:当基类在可导入模块中返回子类实例时,如何避免循环导入错误?

我从其他位置/问题收集了一些解决方案(参见下面的 AD),但恕我直言,没有一个是令人满意的。

初始点

基于这个这个问题,我有以下假设的工作示例作为起点:

# onefile.py

from abc import ABC, abstractmethod

class Animal(ABC):
    def __new__(cls, weight: float):
        if cls is Animal:
            # Try to return subclass instance instead.
            for subcls in [Dog, Cat]:
                try:
                    return subcls(weight)
                except ValueError:
                    pass
            raise NotImplementedError("No appropriate subclass found.")
        return super().__new__(cls)

    @property
    @abstractmethod
    def weight(self) -> float:
        """weight of the animal in kg."""
        ...


class Dog(Animal):
    def __init__(self, weight: float = 5):
        if not (1 < weight < …
Run Code Online (Sandbox Code Playgroud)

python inheritance circular-dependency

8
推荐指数
1
解决办法
1567
查看次数