private static IComponentRegistration CreateLazyRegistration <T, TMeta>(Service providedService, IComponentRegistration valueRegistration)
        {
            var metadataProvider = MetadataViewProvider.GetMetadataViewProvider <TMeta>();
            var metadata         = metadataProvider(valueRegistration.Target.Metadata);

            var rb = RegistrationBuilder.ForDelegate(
                (c, p) =>
            {
                var context      = c.Resolve <IComponentContext>();
                var lazyType     = ((IServiceWithType)providedService).ServiceType;
                var valueFactory = new Func <T>(() => (T)context.ResolveComponent(valueRegistration, p));
                return(Activator.CreateInstance(lazyType, valueFactory, metadata));
            })
                     .As(providedService)
                     .Targeting(valueRegistration);

            return(rb.CreateRegistration());
        }
Beispiel #2
0
        static ExportDescriptorPromise[] GetLazyDefinitions <TValue, TMetadata>(CompositionContract lazyContract, DependencyAccessor definitionAccessor)
        {
            var metadataProvider = MetadataViewProvider.GetMetadataViewProvider <TMetadata>();

            return(definitionAccessor.ResolveDependencies("value", lazyContract.ChangeType(typeof(TValue)), false)
                   .Select(d => new ExportDescriptorPromise(
                               lazyContract,
                               Formatters.Format(typeof(Lazy <TValue, TMetadata>)),
                               false,
                               () => new[] { d },
                               _ =>
            {
                var dsc = d.Target.GetDescriptor();
                var da = dsc.Activator;
                return ExportDescriptor.Create((c, o) => {
                    return new Lazy <TValue, TMetadata>(() => (TValue)CompositionOperation.Run(c, da), metadataProvider(dsc.Metadata));
                }, dsc.Metadata);
            }))
                   .ToArray());
        }
        private static ExportDescriptorPromise[] GetExportFactoryDescriptors <TProduct, TMetadata>(CompositionContract exportFactoryContract, DependencyAccessor definitionAccessor)
        {
            var productContract = exportFactoryContract.ChangeType(typeof(TProduct));
            var boundaries      = EmptyArray <string> .Value;

            IEnumerable <string> specifiedBoundaries;
            CompositionContract  unwrapped;

            if (exportFactoryContract.TryUnwrapMetadataConstraint(Constants.SharingBoundaryImportMetadataConstraintName, out specifiedBoundaries, out unwrapped))
            {
                productContract = unwrapped.ChangeType(typeof(TProduct));
                boundaries      = (specifiedBoundaries ?? EmptyArray <string> .Value).ToArray();
            }

            var metadataProvider = MetadataViewProvider.GetMetadataViewProvider <TMetadata>();

            return(definitionAccessor.ResolveDependencies("product", productContract, false)
                   .Select(d => new ExportDescriptorPromise(
                               exportFactoryContract,
                               typeof(ExportFactory <TProduct, TMetadata>).Name,
                               false,
                               () => new[] { d },
                               _ =>
            {
                var dsc = d.Target.GetDescriptor();
                return ExportDescriptor.Create((c, o) =>
                {
                    return new ExportFactory <TProduct, TMetadata>(() =>
                    {
                        var lifetimeContext = new LifetimeContext(c, boundaries);
                        return Tuple.Create <TProduct, Action>((TProduct)CompositionOperation.Run(lifetimeContext, dsc.Activator), lifetimeContext.Dispose);
                    },
                                                                   metadataProvider(dsc.Metadata));
                },
                                               dsc.Metadata);
            }))
                   .ToArray());
        }