Is it possible to register a class with a parameter that is expected to be passed from the point of creation?
I know this can be done something like this:
GlobalContainer.RegisterType<TUserProcessor>.Implements<IUserUpgrader>. AsTransient.DelegateTo( function: TUserProcessor begin Result := TUserProcessor.Create(GetCurrentUser); end );
But there the parameters are tied to the execution context in which the container is registered, and not where the object is tricked.
Is something like this possible, for example?
GlobalContainer.Resolve<IMathService>([FCurrentUser]);
I know that some peoble supporters have very simple constructors, but there are times when the constructor parameter explicitly looks like this:
The created object requires that the parameter of the object work, so the link must be executed. The parameter also makes this restriction much more obvious when looking at a class.
You can assign a reference in a method or property, and also raise and exclude in every other method if you try to use an object without prior assignment. I don't like writing this type of code, it's just a waste of time, just use the constructor parameter and check there. The less code, the better IMO.
Also, the object is passed locally to the object, which creates a new object using the container (for example, the Transaction object) and has some state (this is not a new object that I can get with the container).
dependency-injection delphi delphi-xe2 spring4d
pragmatic_programmer
source share