标签: circular-dependency

Python:如何摆脱涉及装饰器的循环依赖?

我遇到了以下循环导入的情况(这里进行了严重简化):

array2image.py转换模块:

import tuti

@tuti.log_exec_time # can't do that, evaluated at definition time
def convert(arr):
    '''Convert array to image.'''
    return image.fromarray(arr)
Run Code Online (Sandbox Code Playgroud)

tuti.py测试实用程序模块:

import array2image

def log_exec_time(f):
    '''A small decorator not using array2image'''

def debug_image(arr):
    image = array2image.convert(arr)
    image = write('somewhere')
Run Code Online (Sandbox Code Playgroud)

由于名称错误而失败。这对我来说看起来不太正确,因为那里实际上没有循环依赖。我一直在寻找一种巧妙的方法来避免这种情况或解释......在写这个问题的一半时我发现了它。

import将装饰器移至下方tuti.py可解决 NameError:

def log_exec_time(f):
    '''A small decorator not using array2image'''

import array2image

def debug_image(arr):
    image = array2image.convert(arr)
    image = write('somewhere')
Run Code Online (Sandbox Code Playgroud)

python circular-dependency decorator

3
推荐指数
1
解决办法
2453
查看次数

依赖注入可以防止循环依赖吗?

项目#1有一些项目#2引用的接口和类.

现在我想在Project#1中使用Project#2的实现,但vs.net抱怨循环依赖.

如果我在Project#1中使用依赖注入并绑定到Project#2中的实现(因为它遵守接口契约),这是否可行或者我仍然会在运行时获得循环依赖性错误消息?

c# assemblies dependency-injection circular-dependency

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

来自Guice的“请等到注入完成后才能使用此对象”错误

我们in(Scopes.SINGLETON)在 Guice 中有两个单例对象(通过 声明),每个对象在其构造函数中使用另一个。Guice 实现这一点的方法是使用代理——它首先使用另一个对象的代理来初始化对象,并且只有在需要该对象时才会解析它。

从多个线程运行此代码时,我们得到以下异常:

java.lang.IllegalStateException: This is a proxy used to support
circular references involving constructors. The object we're proxying is not
constructed yet. Please wait until after injection has completed to use this
object.
    at
com.google.inject.internal.ConstructionContext$DelegatingInvocationHandler.invoke(ConstructionContext.java:100)
Run Code Online (Sandbox Code Playgroud)

我认为这是 Guice 中的一个错误,因为我们没有做任何不寻常的事情。我们发现的一种解决方法是使用 尽早初始化单例.asEagerSingleton(),但这不是很方便,例如用于测试。

这是一个已知的问题?报告了Google Guice问题,通过独立测试重现。

还有其他建议/解决方法吗?

java singleton dependency-injection circular-dependency guice

3
推荐指数
1
解决办法
2430
查看次数

模板循环继承

在这段代码中,编译器抱怨undefined MyClassB,这是可以理解的:

class MyClassA;
class MyClassB;

template <class T> class BaseClass : public T {
};

class MyClassA : public BaseClass<MyClassB> {
};

class MyClassB : public BaseClass<MyClassA> {
};
Run Code Online (Sandbox Code Playgroud)

但在这段代码,编译是成功的,并没有抱怨有关MyClassB:

class MyClassA;
class MyClassB;

template <class T> class BaseClass : public T {
};

class MyClassA : public BaseClass<std::vector<MyClassB>> {
};

class MyClassB : public BaseClass<std::vector<MyClassA>> {
};
Run Code Online (Sandbox Code Playgroud)

为什么第二个代码编译,因为MyClassB在构造时尚未定义std::vector<MyClassB>

c++ inheritance templates circular-dependency incomplete-type

3
推荐指数
1
解决办法
546
查看次数

Java循环参考 - 无法编译

文件:A.java

class A
{
    B b;
    public A() {
        b = new B();
    }
}
Run Code Online (Sandbox Code Playgroud)

档案:B.java

class B
{
    public B() {}
    public foo(A a) {...}
}
Run Code Online (Sandbox Code Playgroud)

上面的代码无法编译,因为A需要B才能编译,B需要A才能编译.也不要在另一个之前编译.怎么办?

这个例子很简单.我可以删除foo(A a){...}这样的B.java会编译.然后编译A.java.恢复B.java然后编译它.但我正在尝试从源代码构建RXTX,它的依赖性是一个曲折的小短语的迷宫.

我原本希望能编译成非工作类代码.然后将定义的所有类和方法再次编译成工作代码.

有灵丹妙药吗?

java circular-dependency

3
推荐指数
1
解决办法
2044
查看次数

AngularJs拦截器的循环依赖

我有以下循环依赖:

            $http
            /   \
           /     \
          /       \
         /         \
LoginManager------Interceptor
 (service)         (factory)
Run Code Online (Sandbox Code Playgroud)

这个循环依赖只在我添加了Interceptor的代码后才出现.

InterceptorLoginManager如果某个response被截获,将调用注销功能.

从我看到的,唯一的解决方案是将拦截器代码移动到LoginManager服务中anonymous factory

有没有更好的方法?

circular-dependency angularjs angular-http-interceptors

3
推荐指数
1
解决办法
1114
查看次数

Java:在同一个包中具有循环依赖是不好的做法吗?

在同一个 Java 包中的类之间存在循环依赖是不好的做法吗?

如果没有,我希望在某处有信誉良好的参考指南。

java oop circular-dependency

3
推荐指数
1
解决办法
4022
查看次数

Flask 循环依赖

我正在开发 Flask 应用程序。它仍然相对较小。我只有一个 app.py 文件,但因为我需要进行数据库迁移,我使用本指南将其分为 3 个:

https://realpython.com/blog/python/flask-by-example-part-2-postgres-sqlalchemy-and-alembic/

但是,我现在无法运行我的应用程序,因为应用程序和模型之间存在循环依赖关系。

应用程序.py:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask import render_template, request, redirect, url_for
import os

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DB_URL']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

app.debug = True

db = SQLAlchemy(app)

from models import User

... routes ...    

if __name__ == "__main__":
  app.run()
Run Code Online (Sandbox Code Playgroud)

模型.py:

from app import db
class User(db.Model):
  id = db.Column(db.Integer, primary_key=True)
  username = db.Column(db.String(80), unique=True)
  email = db.Column(db.String(120), unique=True)

  def __init__(self, username, email):
    self.username = username …
Run Code Online (Sandbox Code Playgroud)

python circular-dependency flask

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

Angular 7 库 - 检测到循环依赖(指令、服务、模块)

我创建了一个带有库的新 Angular 7 项目。该库包含一个指令、一个服务和一个模块(指令 get 是注入的服务,而服务是在模块中导出的 injectionToken)。我在编译时收到此警告:

检测到循环依赖项中的警告:projects\auth\src\lib\auth.module.ts -> projects\auth\src\lib\login-form-ref.directive.ts -> projects\auth\src\lib\auth。 service.ts -> projects\auth\src\lib\auth.module.ts

检测到循环依赖项中的警告:projects\auth\src\lib\auth.service.ts -> projects\auth\src\lib\auth.module.ts -> projects\auth\src\lib\login-form-ref。指令.ts -> 项目\auth\src\lib\auth.service.ts

检测到循环依赖项中的警告:projects\auth\src\lib\login-form-ref.directive.ts -> projects\auth\src\lib\auth.service.ts -> projects\auth\src\lib\auth。 module.ts -> projects\auth\src\lib\login-form-ref.directive.ts

auth.service.ts

import { Injectable, Inject } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Token } from './models/token';
import { OAuthClient } from './models/oAuthClient';
import { OAUTH_CONFIGURATION } from './auth.module';


@Injectable({
  providedIn: 'root' …
Run Code Online (Sandbox Code Playgroud)

dependency-injection circular-dependency angular-directive angular

3
推荐指数
1
解决办法
1969
查看次数

PartialView Action正在自我调用

我有MVC应用程序,该应用程序用于从主视图(ProductMaster)中将ProductAreaGrid的列表显示为PartialView,并且它将在Partialview中具有CreateProductArea作为PartialView。我的Gridview局部动作反复调用,我不确定为什么反复调用它。该代码中是否有任何循环参考?

我研究了谷歌,并得到下面的链接,但也没有用。

为什么PartialView不断调用自己?

下面是我的代码MVC代码。

ProductAreaGrid.cshml

@model IEnumerable<Brain.DAL.Entities.ProductArea>
@{
    ViewBag.Title = "Product Area";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<p>
    <a href="#" class="btn btn-success" data-target="#CreateProductArea" data-toggle="modal">
        Add New
        <i class="fa fa-plus"></i>
    </a>
</p>
@Html.Partial("Partials/PA/CreateProductArea", null, new ViewDataDictionary() {})
<div class="table-responsive">
    <table class="table table-bordered table-hover dataTable gray-table">
        <thead>
            <tr>
                <th>Action</th>
                <th>Name</th>
                <th>Is Active</th>
            </tr>
        </thead>
        <tbody>
            @if (!Model.Any())
            {
                <tr>
                    <td colspan="3">There are no required support entries.</td>
                </tr>
            }
            else
            {
                foreach (var item in Model)
                {
                    <tr>
                        <td>
                            <a href="#" class="btn btn-xs btn-success" …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc circular-dependency

3
推荐指数
1
解决办法
271
查看次数