我有以下示例。
有没有更简单的方法来传递已经为 Container 注册的接口变量来解决它?
或者,还有其他方法可以使用此功能吗?
那是我的界面
IString = interface
function Value: string;
end;
IUser = interface
function Name: IString;
end;
ILogin = interface
function User: IUser;
function Password: IString;
end;
类实现
TString = class(TInterfacedObject, IString)
private
FValue: String;
public
constructor Create(const AValue: String);
function Value: String;
end;
TUser = class(TInterfacedObject, IUser)
private
FName: IString;
public
constructor Create(const AName: IString);
function Name: IString;
end;
TLogin = class(TInterfacedObject, ILogin)
private
FUser: IUser;
FPassword: IString;
public
constructor Create(const AUser: IUser; const APassword: IString);
function User: IUser;
function Password: IString;
end;
集装箱登记
GlobalContainer.RegisterType<TString>.Implements<IString>;
GlobalContainer.RegisterType<TUser>.Implements<IUser>;
GlobalContainer.RegisterType<TLogin>.Implements<ILogin>;
调用
所以当必须创建这些接口时,我以这种方式调用,这是嵌套参数使代码复杂且难以阅读(并查看)
GlobalContainer.Resolve<ILogin>([
TValue.From(
GlobalContainer.Resolve<IUser>([
TValue.From(
GlobalContainer.Resolve<IString>(['UserName'])
)
])
),
TValue.From(
GlobalContainer.Resolve<IString>(['SuperSecretPassword'])
)
]);