这段代码是循环依赖的一个例子吗?
package expr;
import sheet.Sheet
public class AdressExpr implements Expr
{
private Address address;
private Sheet sheet;
public double value(Sheet sheet)
{
return sheet.value(address);
}
}
public interface Expr
{
public double value(Sheet sheet);
}
public class Adress
{
// omissions
}
package sheet;
import expr.Address;
import expr.Expr;
public class Sheet implements SuperSheet
{
private Map <Address, Expr> map;
public double value(Address address)
{
return map.get(Address).value(this);
}
}
public interface SuperSheet
{
public double value(Address address);
}
Run Code Online (Sandbox Code Playgroud)
我知道这个例子是错误的编程,但是由于value方法,接口是否禁止循环依赖?
我正在研究IronJS,我们的一个源文件变得越来越长.
现在,我正在努力让.NET互操作.我正在添加TryBinaryOperation方法,Undefined以便C#可以使用未定义值的JavaScript语义.
但是,这会引入对Operators类型的依赖,从而导致循环依赖.
Runtime.fs:
type BoxedValue() =
struct
// Contains IsUndefined and get_Undefined, referencing the Undefined class, below.
...
and type Undefined() =
inherit DynamicObject()
...
override x.TryBinaryOperation(binder:BinaryOperationBinder, arg:obj, result:obj byref) : bool =
// Here, we are referencing BoxedValue, above.
result <- Operators.add(Und, BoxedValue.Box(arg))
true
...
Run Code Online (Sandbox Code Playgroud)
Operators.fs:
type Operators =
...
// Here, we are referencing BoxedValue.
static member add(BoxedValue l, BoxedValue r)
...
Run Code Online (Sandbox Code Playgroud)
所以,我们有这组依赖:

理想情况下,我们希望将每个文件拆分为自己的文件.
F#中是否可能存在跨文件循环依赖关系?
我已经阅读过,以避免循环依赖,我可以@Autowired在setter而不是构造函数上使用.
如果是这样,为什么会失败?
@Component
private static class A {
@Autowired
public A(B b) {
}
}
@Component
private static class B {
private A a;
public B() {
}
@Autowired
public void setA(A a) {
this.a = a;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我A类似地定义类B,一切都还可以,但上面的那个应该已经有效了,为什么不呢?我处于无法摆脱A类的构造函数中的依赖的情况.在这种情况下我该怎么办?
编辑:我使用的是Spring 3.2.1.RELEASE
编辑2:我的例外:
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dependencyTest.A' defined in file [/home/adam/workspaces/testproject/target/classes/mypackage/test/DependencyTest$A.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [mypackage.test.DependencyTest$B]: : Error …Run Code Online (Sandbox Code Playgroud) 是否可以将Unity配置为检测循环引用或拦截类型解析器以显示某些调试信息?
这里有几个相互依赖的接口和类
public interface IThing1 { }
public class Thing1 : IThing1
{
private IThing2 _thing2;
public Thing1(IThing2 thing2)
{
_thing2 = thing2;
}
}
public interface IThing2 { }
public class Thing2 : IThing2
{
private IThing1 _thing1;
public Thing2(IThing1 thing1)
{
_thing1 = thing1;
}
}
Run Code Online (Sandbox Code Playgroud)
如果在Castle Windsor中配置了这两种类型,它将抛出异常并提供一些调试信息以查找循环引用:
Castle.MicroKernel.CircularDependencyException: Dependency cycle has been detected when trying to resolve component 'CircularIoC.Thing1'.
The resolution tree that resulted in the cycle is the following:
Component 'CircularIoC.Thing1' resolved as dependency …Run Code Online (Sandbox Code Playgroud) castle-windsor circular-dependency inversion-of-control unity-container
业务逻辑 - 一个类别可能有多个(1:M)属性,如类别"内存"可能具有属性速度,大小,类型等.
同时,一个Category可以按属性值排序(它存储在Category.sortByAttribute中 - 这是LookupCategoryAttributes表的外键.
尝试通过SQLAlchemy构建它,但检测到循环依赖.怎么了?
class Attribute(Base):
__tablename__ = "LookupCategoryAttributes"
types = ["date", "float", "integer", "select", "string", "text"]
# Properties
ID = Column(BigInteger, primary_key=True)
categoryID = Column(BigInteger, ForeignKey('LookupCategories.ID'), nullable=False )
attribute = Column(VARCHAR(255), nullable=False)
listValues = Column(VARCHAR(4000))
typeID = Column(VARCHAR(40), nullable=False)
isRequired = Column(SmallInteger, nullable=False, default=0)
displayInMenu = Column(SmallInteger, nullable=False, default=0)
displayInFilter = Column(SmallInteger, nullable=False, default=0)
class Category(Base):
__tablename__ = "LookupCategories"
# Properties
ID = Column(BigInteger, primary_key=True)
category = Column(VARCHAR(255), nullable=False)
description = Column(VARCHAR(1000), nullable=False)
parentCategoryID = Column(BigInteger, …Run Code Online (Sandbox Code Playgroud) 我在Golang有一个包含几个模块的项目.由于以下场景,我遇到循环导入问题:
模块游戏包含具有当前游戏状态的结构.另一个模块(Modifier)正在做一些游戏特定的东西和计算,因此修改了游戏状态.因此,Modifier将需要struct Game,但不需要来自Game的任何方法.从Game中调用修饰符,这里我们有循环导入.
游戏启动修改器
修饰符需要Game结构
在我看来,这是一个常见的场景,所以我想知道我应该如何以最好的方式解决它.我的解决方案是创建第三个模块"Structs",它只包含整个应用程序的所有结构.这是一个好的解决方案吗?
这是我经常遇到的一个问题,我希望找到正确的方法来处理它.
所以我有这样的设置:
parent.js:
export default {
x: 1
}
Run Code Online (Sandbox Code Playgroud)
a.js:
import parent from 'parent.js'
export default parent.extend(a, { title: 'a' })
Run Code Online (Sandbox Code Playgroud)
b.js:
import parent from 'parent.js'
export default parent.extend(b, { title: 'b' })
Run Code Online (Sandbox Code Playgroud)
很酷,现在我有了一些孩子.但我决定在parent.js中有一个函数来检查对象是否是a或b的实例.
所以我可能这样做:
parent.js:
import a from 'a'
import b from 'b'
export default {
x: 1,
checkType (obj) {
if (obj instanceof a) {
return 'a'
} else if (obj instanceof b) {
return 'b'
}
}
}
Run Code Online (Sandbox Code Playgroud)
那么现在这是一个循环依赖.有一种优雅的方式来处理这个问题吗?
我希望我的ApplicationContext构造函数具有UserManageras参数但是依赖注入有问题.
码:
public class ApplicationContext : IdentityDbContext<ApplicationUser>
{
private IHttpContextAccessor _contextAccessor { get; set; }
public ApplicationUser ApplicationUser { get; set; }
private UserManager<ApplicationUser> _userManager;
public ApplicationContext(DbContextOptions<ApplicationContext> options, IHttpContextAccessor contextAccessor, UserManager<ApplicationUser> userManager)
: base(options)
{
_contextAccessor = contextAccessor;
var user = _contextAccessor.HttpContext.User;
_userManager = userManager;
ApplicationUser = _userManager.Users.FirstOrDefault(u => u.Id == _userManager.GetUserId(user));
}
}
Run Code Online (Sandbox Code Playgroud)
在startup.cs中
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<ApplicationContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), b => b.MigrationsAssembly("RCI.App")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationContext>()
.AddDefaultTokenProviders();
services.AddAuthentication();
services.AddMvc(); …Run Code Online (Sandbox Code Playgroud) c# dependency-injection circular-dependency asp.net-core-mvc asp.net-core
我正在开发一个 Angular 7 应用程序,它允许管理实体,例如汽车和学生。
应用程序组件可以用以下树来描述:
在CreateCar对话框中创建 Car 时,用户应该能够使用CreateStudent对话框创建并指定一个新学生作为 Car 的所有者。
同样,在CreateStudent对话框中创建 Student 时,用户应该能够使用CreateCar对话框创建和分配一辆新车作为 Student 的属性。
编译时,Angular 显示:“检测到循环依赖项中的警告”,我知道这应该发生。
我试过寻找模式来解决这个问题,例如共享服务,但似乎没有人工作。
编辑:
两个对话框的构造函数的相关部分:
constructor(
private readonly matDialog: MatDialog
) {
}
Run Code Online (Sandbox Code Playgroud)
在 CreateStudent 对话框中,打开 CreateCar 对话框的方法:
createCar(): void {
this.matDialog
.open(CreateCarDialogComponent)
.afterClosed().subscribe((car: Car) => {
// Do something with car
});
}
Run Code Online (Sandbox Code Playgroud)
在 CreateCar 对话框中,打开 CreateStudent 对话框的方法:
createStudent(): void {
this.matDialog
.open(CreateStudentDialogComponent)
.afterClosed().subscribe((student: Student) => {
// Do something with student …Run Code Online (Sandbox Code Playgroud) 我已经使用了Dice PHP DI容器很长一段时间,它在注入依赖项的简单性方面似乎是最好的.
来自骰子文档:
class A {
public $b;
public function __construct(B $b) {
$this->b = $b;
}
}
class B {
}
$dice = new \Dice\Dice;
$a = $dice->create('A');
var_dump($a->b); //B object
Run Code Online (Sandbox Code Playgroud)
但是,当您必须使用直接相互依赖的对象时,由于无限循环,finall结果是服务器错误.
例:
class A {
public $b;
public function __construct(B $b) {
$this->b = $b;
}
}
class B {
public $a;
public function __construct(A $a) {
$this->a = $a;
}
}
Run Code Online (Sandbox Code Playgroud)
Dice的作者说没有办法从A或B类构造一个对象.如:
作者说,这个限制涉及所有 DI容器!
angular ×1
asp.net-core ×1
c# ×1
commonjs ×1
dialog ×1
ecmascript-6 ×1
f# ×1
go ×1
java ×1
javascript ×1
oop ×1
php ×1
python ×1
spring ×1
sqlalchemy ×1