标签: circular-dependency

数据库设计:循环依赖

想象一下以下数据库:

表'公司'有字段ID,名称和flagship_product_id.表'产品'包含字段id,name和company_id.

公司必须拥有旗舰产品(1:1关系),所有产品都有一家公司(1:N关系).

当使用MyISM等存储引擎时,上述情况应该没有任何问题,但是当使用InnoDB之类的引擎时,在插入新数据时会出现问题.

除了允许初始INSERT的NULL关系外,有什么好的解决方案?

总而言之,A公司必须拥有一个旗舰产品.

database-design circular-dependency

9
推荐指数
2
解决办法
2941
查看次数

StructureMap中的循环依赖 - 它们可以通过属性注入来打破吗?

我在结构图中有最简单的循环依赖 - 类A在其构造函数中依赖于类B,而类B在其构造函数中依赖于类A. 为了打破依赖,我让B类把A类作为属性,而不是构造函数参数,但是structmap仍然抱怨.

我已经看到在其他DI框架中使用此方法破坏了循环依赖 - 这是Structuremap的问题还是我做错了什么?

编辑: 我应该提到类B的属性是一个A类实例的数组,这样连线:

x.For<IB>().Singleton().Use<B>().Setter(y => y.ArrayOfA).IsTheDefault();
Run Code Online (Sandbox Code Playgroud)

为了澄清,我希望发生以下事件序列:

  • 构造B的实例,"b"
  • 构造一个A的实例,"a",在其构造函数中注入"b"
  • 将"b.ArrayOfA"设置为["a"]

我希望所有这一切都发生在使用自动装配,如果可能的话......

编辑2:这是一个使用显式连线的简化示例:

interface ILoader { }
interface ILoaderManager { }

class Loader : ILoader
{
    public Loader(ILoaderManager lm) { }
}
class LoaderManager : ILoaderManager
{
    public ILoader Loader { get; set; } // Was an array, but same circular dependency appears here
}

ObjectFactory.Configure
(
    x =>
    {
        x.For<ILoader>.Singleton().Use<Loader>();
        x.For<ILoaderManager>().Singleton().Use<LoaderManager>().OnCreation((c, a) => a.Loader = c.GetInstance<ILoader>());
    }
);
Run Code Online (Sandbox Code Playgroud)

验证配置会导致"使用RequestedType检测到双向依赖性问题:IocTest2.ILoader ..."

c# structuremap dependency-injection circular-dependency

9
推荐指数
2
解决办法
9659
查看次数

Python中"import lib.foo"和"import lib.foo as f"之间的区别

我对Python中循环导入的处理方式感到困惑.我试图提炼一个最小的问题,我不认为这个确切的变体之前被问过.基本上,我看到了两者之间的区别

import lib.foo
Run Code Online (Sandbox Code Playgroud)

import lib.foo as f
Run Code Online (Sandbox Code Playgroud)

当我lib.foo和之间有一个循环依赖lib.bar.我曾预料到两者都会起作用:(可能是半初始化的)模块可以在sys.modules本地命名空间中找到并放入本地命名空间.(从测试中我注意到它import lib.foo确实放入lib了本地命名空间 - 好吧,lib.foo.something无论如何我会用这种语法.)

但是,如果lib.foo已经存在sys.modules,则import lib.foo as f尝试foo作为属性访问lib并引发AttributeError.为什么行为(看似)依赖于存在sys.modules

此外,这种行为记录在哪里?我不觉得Python import语句引用解释了这种行为,或者至少我无法解压缩它:-)

总而言之,我正在尝试更改代码库以使用经常导入模块的方式,而不是模块中的符号:

from project.package import moduleA
from project.package import moduleB
Run Code Online (Sandbox Code Playgroud)

但是当两个模块之间存在循环导入时,这会失败.我曾期望它能够工作,只要两个模块中的顶级定义不相互依赖(例如,没有moduleB基类的子类moduleA).

测试脚本:

#!/bin/sh
rm -r lib; mkdir lib

touch lib/__init__.py

cat > lib/foo.py <<EOF
# lib.foo module
print '{ foo' …
Run Code Online (Sandbox Code Playgroud)

python module circular-dependency python-import

9
推荐指数
1
解决办法
577
查看次数

Rails 4控制器中的运行时错误:自动加载常量时检测到循环依赖性

如果我遗漏了任何东西,请告诉我.我无法弄清楚为什么我的views/references /文件夹无法访问.既不可用new.html.erb也不index.html.erb可用.当我转到localhost:3000/references我的错误是:

RuntimeError in ReferencesController#index
Circular dependency detected while autoloading constant ReferencesController
Run Code Online (Sandbox Code Playgroud)

我相信这是设置,它不应该是一个Rails问题,因为我的其他控制器工作正常.

我的路线文件中有resources :references我的rake路线产生:

references GET    /references(.:format)          references#index
           POST   /references(.:format)          references#create
new_reference GET    /references/new(.:format)      references#new
edit_reference GET    /references/:id/edit(.:format) references#edit
reference GET    /references/:id(.:format)      references#show
           PATCH  /references/:id(.:format)      references#update
           PUT    /references/:id(.:format)      references#update
           DELETE /references/:id(.:format)      references#destroy
Run Code Online (Sandbox Code Playgroud)

所以我试着通过这个到达我的索引页面,这应该是正确的路径.

<%= link_to 'References', references_path, class: 'navbar-brand' %>
Run Code Online (Sandbox Code Playgroud)

我的模特:

class Reference < ActiveRecord::Base
  belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)

我的控制器:

class ReferencesControllers < ApplicationController
  def index
    @references = Reference.all
  end
Run Code Online (Sandbox Code Playgroud)

谷歌搜索后,似乎每个类似的问题都是从Rails 3升级到Rails …

ruby ruby-on-rails circular-dependency controllers ruby-on-rails-4

9
推荐指数
1
解决办法
2万
查看次数

循环引用和构造函数

我正在尝试构建一个验证某个类型实例的属性.

为了做到这一点,我必须将其ObjectInstance转换为该类型.

我需要在该类型的成员上设置属性.

所以我们需要求助于and循环定义的关键字.

但是在下面的例子中我得到了错误

自定义属性必须调用对象构造函数

在下面标出的行上.

namespace Test

open System
open System.ComponentModel.DataAnnotations

[<AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)>]
type MyAttribute() =
    class
    inherit ValidationAttribute ()

    override this.IsValid (value: Object, validationContext: ValidationContext) =
        match validationContext.ObjectInstance with
        | :? MyClass as item ->
            // TODO more validation
            ValidationResult.Success
        | _ ->
            new ValidationResult("No no no")
    end
and MyClass(someValue) =
    [<Required>]
    [<Range(1, 7)>]
  //vvvvvvvvvvvvvvv
    [<MyAttribute>]
  //^^^^^^^^^^^^^^^
    member this.SomeValue : int = someValue
Run Code Online (Sandbox Code Playgroud)

我试着手动调用构造函数,例如:

[<MyAttribute()>]
// or
[<new …
Run Code Online (Sandbox Code Playgroud)

f# circular-dependency circular-reference

9
推荐指数
2
解决办法
294
查看次数

警告检测到循环依赖-Angular Cli

https://github.com/angular/angular-cli/pull/6813添加了关于循环依赖项的警告,我知道我可以使用“ showCircularDependencies”关闭所有警告:false。但是我宁愿保持循环依赖警告。有没有一种模式可以让我修复下面的用例,还是有一种方法可以专门禁用特定文件上的循环依赖插件?

最简单的情况是,如果我有3个文件:

表格模型

import { CustomModel } from './custom.model';
import { CustomForm } from './custom.form';

export class Forms {
  items: CustomForm[] = [];
  public constructor(models?: CustomModel[]) {
    models.forEach(model => this.items.push(new CustomForm(model)));
  }
}
Run Code Online (Sandbox Code Playgroud)

custom.model.ts

export class CustomModel {
  nestedModels: CustomModel[];    
}
Run Code Online (Sandbox Code Playgroud)

custom.form.ts

import { Forms } from './forms.model';
import { CustomModel } from './custom.model';

export class CustomForm {
  nestedForms: Forms;

  constructor(model: CustomModel) {
    this.nestedForms = new Forms(model.nestedModels);
  }
}
Run Code Online (Sandbox Code Playgroud)

这将导致以下警告:

WARNING in Circular …
Run Code Online (Sandbox Code Playgroud)

warnings circular-dependency angular-cli angular

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

C++ 模块中的前向声明 (MSVC)

我最近一直在试验 MSVC 提供的模块实现,我遇到了一个有趣的场景。我有两个在它们的接口中相互依赖的类,这意味着我必须使用前向声明来编译它。以下代码显示了一个示例:

Module interface

export module FooBar;

export namespace FooBar {
    class Bar;

    class Foo {
    public:
        Bar createBar();
    };

    class Bar {
    public:
        Foo createFoo();
    };
}
Run Code Online (Sandbox Code Playgroud)

Module implementation

module FooBar;

namespace FooBar {
    Bar Foo::createBar() {
        return Bar();
    }

    Foo Bar::createFoo() {
        return Foo();
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我想将这两个类拆分为它们自己的名为Foo和的模块Bar。但是,每个模块都需要导入另一个模块,因为它们的接口相互依赖。根据目前的模块提案,不允许循环接口导入。这篇文章建议使用proclaimed ownership声明,但是在模块的MSVC实现中似乎还没有实现。

因此,我是否正确假设目前无法使用 MSVC 提供的当前实现解决这种情况?还是我缺少一些替代方案?在这种情况下,情况非常简单,但是我在模块化具有许多具有此类依赖关系的类的库时遇到了这个问题。我意识到循环依赖通常表明设计不佳,但在某些情况下,它们不可避免或难以重构。

c++ circular-dependency visual-c++ c++-modules

9
推荐指数
1
解决办法
642
查看次数

Angular Circular Module 导入

我有两个模块,它们的组件相互使用。所以我必须在“test”中导入“word”,在“word”中导入“test”-->抛出一个错误......我该怎么办?

模块“测试”:

    @NgModule({
      declarations: [
        AppTest1Component,
        AppTest2Component,
      ],
      imports: [
        AppWordModule,
      ],
      exports: [
        AppTest1Component,
        AppTest2Component,
      ],
    })
    export class AppTestModule {
    }
Run Code Online (Sandbox Code Playgroud)

模块“字”:

    @NgModule({
      declarations: [
        AppWordComponent,
      ],
      imports: [
        AppTestModule,
      ],
      exports: [
        AppWordComponent,
      ],
    })
    export class AppWordModule {
    }
Run Code Online (Sandbox Code Playgroud)

我因为模板相互导入。test1.component.ts的模板调用word.component.ts,word.component.ts的模板调用test1.component.ts。

测试1.html

    <div class="app-word"></div>
Run Code Online (Sandbox Code Playgroud)

字.html

    <div class="app-test1"></div>
Run Code Online (Sandbox Code Playgroud)

我尝试使用 SharedModule 但我没有实现...

import module circular-dependency angular

9
推荐指数
1
解决办法
2051
查看次数

单独文件中的 FastAPI / Pydantic 循环引用

我很想在 FastAPI 中使用类似于以下内容的架构:

from __future__ import annotations
from typing import List
from pydantic import BaseModel


class Project(BaseModel):
    members: List[User]


class User(BaseModel):
    projects: List[Project]


Project.update_forward_refs()
Run Code Online (Sandbox Code Playgroud)

但为了保持我的项目结构干净,我会关闭。喜欢在单独的文件中定义这些。我怎么能在不创建循环引用的情况下做到这一点?

使用上面的代码,FastAPI 中的模式生成工作正常,我只是不知道如何将它分成单独的文件。在后面的步骤中,我将不再使用属性,而是使用@propertys 在它们的子类中为这些对象定义 getter。但是对于 OpenAPI 文档生成,我需要将其结合起来 - 我认为。

python circular-dependency pydantic fastapi

9
推荐指数
3
解决办法
1769
查看次数

解决圆形模块加载的最佳方法

我试图让两个不同的对象相互引用,并在属性上使用类型检查。当我这样做时,我得到Circular module loading detected trying to precompile. 谷歌搜索让我https://docs.raku.org/language/faq#Can_I_have_circular_dependencies_between_modules?其中指出:

请注意,Raku 没有“1 个文件 = 1 个类”的限制,单个编译单元(例如,文件)内的循环依赖可以通过存根实现。因此,另一种可能的解决方案是将类移动到同一个编译单元中。

如果可以避免的话,我宁愿不将两个类都放在同一个单元中。由于没有示例,我不确定如何使用存根来完成此操作。以下是我正在尝试做的一个小例子:

牦牛

unit class Yak;

use YakStore;

has YakStore $.yak-store is rw;
Run Code Online (Sandbox Code Playgroud)

YakStore.rakumod

unit class YakStore;

use Yak;

has Yak $.yak is rw;
Run Code Online (Sandbox Code Playgroud)

yak.rakutest

use lib '.';

use Test;

use Yak;
use YakStore;

plan 2;

my $yak-store = YakStore.new;

my $yak = Yak.new(:$yak-store);

$yak-store.yak = $yak;

isa-ok $yak-store.yak, Yak;
isa-ok $yak.yak-store, YakStore;
Run Code Online (Sandbox Code Playgroud)

是的,我知道,测试很蹩脚,但我只是想说明问题。谢谢!

circular-dependency raku

9
推荐指数
1
解决办法
115
查看次数