Object RecusrivelyConstructStack(Type rootDecorated, Stack <DecTypeIocInfo> sStack, ResolveDelegate resolve, IocConstructDelegate create) { if (sStack.Count == 0) { return(create(rootDecorated)); } var cs = sStack.Pop(); Func <Object> nextLevel = () => RecusrivelyConstructStack(rootDecorated, sStack, resolve, create); return(cs.Construct(x => x == cs.serviceType ? nextLevel() : resolve(x))); }
// run extra registrations (that delegate to the container) before creating the container. public void AnylyzeAndRegisterDecorators(RegisterServiceDelegate registerService, ResolveDelegate resolve, IocConstructDelegate create) { foreach (var dstack in serviceStacks.Where(x => x.Value.Any(y => y.madeDecorator))) { if (dstack.Value.First().madeDecorator) { throw new InvalidOperationException("root cant be decorator"); } if (dstack.Value.Skip(1).Any(x => !x.madeDecorator)) { throw new InvalidOperationException("all following root must be decorator"); } // call do nit regiuster new var root = dstack.Value.First().service(true); var decorators = dstack.Value.Skip(1).Select(x => x.service(true)).ToArray(); // regenerate a stack each time.. (e.g. what if not a singleton?) Func <Stack <DecTypeIocInfo> > decStack = () => new Stack <DecTypeIocInfo>(decorators.Select(x => new DecTypeIocInfo(dstack.Key, x))); // registring construction like this registerService(dstack.Key, () => RecusrivelyConstructStack(root, decStack(), resolve, create)); } }