Example #1
0
        //删除
        private static void Method04()
        {
            OperationResult result = _container.GetExportedValueOrDefault <ContainerIn>().UserContract.DeleteEntity("65128044");

            Console.WriteLine(result.Message);
            Console.WriteLine();
            //有外键约束时不能删除
        }
        [ActiveIssue(25498, TestPlatforms.AnyUnix)] // System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
        public void TryToDiscoverExportWithGenericParameter()
        {
            var catalog   = new AssemblyCatalog(typeof(AssemblyCatalogTests).Assembly);
            var container = new CompositionContainer(catalog);

            // Should find a type that inherits from an export
            Assert.NotNull(container.GetExportedValueOrDefault <object>(AttributedModelServices.GetContractName(typeof(ExportWhichInheritsFromGeneric))));

            // This should be exported because it is inherited by ExportWhichInheritsFromGeneric
            Assert.NotNull(container.GetExportedValueOrDefault <object>(AttributedModelServices.GetContractName(typeof(ExportWithGenericParameter <string>))));
        }
Example #3
0
        public void PropertyExportWithContractInferredTest()
        {
            TypeCatalog          catalog   = new TypeCatalog(typeof(PropertyExportWithContractInferred <,>));
            CompositionContainer container = new CompositionContainer(catalog);

            var export = container.GetExportedValueOrDefault <IExport <IFoo, IBar> >();

            Assert.IsNotNull(export);

            var export2 = container.GetExportedValueOrDefault <IExport <IBar, IFoo> >();

            Assert.IsNotNull(export2);
        }
Example #4
0
        public void SelfExportTest()
        {
            TypeCatalog          catalog   = new TypeCatalog(typeof(SelfExport <,>));
            CompositionContainer container = new CompositionContainer(catalog);

            var export = container.GetExportedValueOrDefault <IExport <IFoo, IBar> >();

            Assert.IsNotNull(export);

            var export2 = container.GetExportedValueOrDefault <IExport <IBar, IFoo> >();

            Assert.IsNotNull(export2);
        }
        /// <summary>
        /// Searches "Application Extensions" for and activates "*SplashScreen*.dll"
        /// </summary>
        /// <returns></returns>
        public static ISplashScreenManager GetSplashScreenManager()
        {
            // This is a specific directory where a splash screen may be located.
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Application Extensions");

            if (!Directory.Exists(path))
            {
                return(null);
            }

            string file = Directory.EnumerateFiles(path, "*SplashScreen*.dll").FirstOrDefault();

            if (file == null)
            {
                return(null);
            }

            using (AggregateCatalog splashCatalog = new AggregateCatalog())
            {
                splashCatalog.Catalogs.Add(new AssemblyCatalog(file));
                using (CompositionContainer splashBatch = new CompositionContainer(splashCatalog))
                {
                    var splash = splashBatch.GetExportedValueOrDefault <ISplashScreenManager>();
                    if (splash != null)
                    {
                        splash.Activate();
                    }
                    return(splash);
                }
            }
        }
Example #6
0
        private static void ComposeConnectionFactory()
        {
            try
            {
                using (var catalog = new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory))
                    using (var container = new CompositionContainer(catalog))
                    {
                        var export = container.GetExportedValueOrDefault <IConnectionFactory>();
                        if (export != null)
                        {
                            Factory = export;
                            Console.WriteLine("Using {0}", Factory.GetType());
                        }
                    }
            }
            catch (ImportCardinalityMismatchException)
            {
                Console.WriteLine("More than one IConnectionFactory import was found.");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            if (Factory == null)
            {
                Factory = new DefaultConnectionFactory();
                Console.WriteLine("Using default connection factory...");
            }
        }
 public override T GetService <T>(Type type = null)
 {
     if (_disposeToken.IsDisposed)
     {
         return(null);
     }
     return(base.GetService <T>(type) ?? _compositionContainer.GetExportedValueOrDefault <T>());
 }
Example #8
0
        /// <summary>
        /// Tries to get an instance of the specified type with the optional specified contractName.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="contractName">Name of the contract.</param>
        /// <returns></returns>
        public static T TryGet <T>(string contractName = null)
        {
            CompositionContainer container = GlobalContainer;

            using (__lock.ForWrite())
            {
                return(container.GetExportedValueOrDefault <T>(contractName));
            }
        }
Example #9
0
        protected override void OnStartup(object sender, StartupEventArgs e)
        {
            DisplayRootViewFor <IShell>();
            RegisterApplicationCommandHandlers();

            var pipeServiceBootstrapper = _container.GetExportedValueOrDefault <PipeServiceBootstrapper>();

            pipeServiceBootstrapper?.Init();
        }
 private static T GetCustomProviderExport <T>(ISchemaProvider schemaProvider)
 {
     using (var assemblyCatalog = new AssemblyCatalog(schemaProvider.GetType().Assembly))
     {
         using (var container = new CompositionContainer(assemblyCatalog))
         {
             return(container.GetExportedValueOrDefault <T>());
         }
     }
 }
Example #11
0
        /// <summary>
        /// Tries to get an instance of the specified type with the optional specified contractName.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="contractName">Name of the contract.</param>
        /// <returns></returns>
        public static object TryGet(Type type, string contractName = null)
        {
            string contract = string.IsNullOrEmpty(contractName) ? AttributedModelServices.GetContractName(type) : contractName;
            CompositionContainer container = GlobalContainer;

            using (__lock.ForWrite())
            {
                return(container.GetExportedValueOrDefault <object>(contract));
            }
        }
 private static Object GetCustomProviderExport <T>(Assembly assembly)
 {
     using (var assemblyCatalog = new AssemblyCatalog(assembly))
     {
         using (var container = new CompositionContainer(assemblyCatalog))
         {
             return(container.GetExportedValueOrDefault <T>());
         }
     }
 }
Example #13
0
 public static T GetAdjacentComponent <T>(Type knownSiblingType)
 {
     using (var assemblyCatalog = new AssemblyCatalog(knownSiblingType.Assembly))
     {
         using (var container = new CompositionContainer(assemblyCatalog))
         {
             return(container.GetExportedValueOrDefault <T>());
         }
     }
 }
Example #14
0
        /// <summary>
        /// Injects a reference to the container into the container.
        /// </summary>
        /// <param name="container"></param>
        /// <returns></returns>
        public static CompositionContainer ConfigureContainer(CompositionContainer container)
        {
            Contract.Requires <ArgumentNullException>(container != null);

            if (container.GetExportedValueOrDefault <ContainerRef>() == null)
            {
                container.WithExport <ContainerRef>(new ContainerRef(container));
            }

            return(container);
        }
Example #15
0
 //get object method
 public static T CreateObject <T>(string objectType)
 {
     try
     {
         return(_container?.GetExportedValueOrDefault <T>(objectType));
     }
     catch (Exception)
     {
     }
     return(null);
 }
            public override T GetService <T>(Type type = null)
            {
                // Allow basic services even if disposed
                var service = base.GetService <T>(type);

                if (service == null && !_disposeToken.IsDisposed)
                {
                    service = _compositionContainer.GetExportedValueOrDefault <T>();
                }
                return(service);
            }
        public void ExportFactoryCausesRejectionBasedOnCardinality()
        {
            var parentCatalog = new TypeCatalog(typeof(BarImpl));
            var childCatalog  = new TypeCatalog(typeof(FooImpl), typeof(Foo2Impl));

            var scope     = parentCatalog.AsScope(childCatalog.AsScope());
            var container = new CompositionContainer(scope);

            var bar = container.GetExportedValueOrDefault <IBarContract>();

            Assert.Null(bar);
        }
Example #18
0
        public object TryGetService(Type serviceType)
        {
            if (!Initialized)
            {
                return(null);
            }

            if (!initializingLogging && log.Factory.Configuration == null)
            {
                initializingLogging = true;
                try
                {
                    var logging = TryGetService <ILoggingConfiguration>();
                    logging.Configure();
                }
                catch
                {
                }
            }

            string contract = AttributedModelServices.GetContractName(serviceType);

            var instance = AddToDisposables(tempContainer.GetExportedValueOrDefault <object>(contract));

            if (instance != null)
            {
                return(instance);
            }

            instance = AddToDisposables(ExportProvider.GetExportedValues <object>(contract).FirstOrDefault(x => contract.StartsWith("github.", StringComparison.OrdinalIgnoreCase) ? x.GetType().Assembly.GetName().Version == currentVersion : true));

            if (instance != null)
            {
                return(instance);
            }

            instance = serviceProvider.GetService(serviceType);
            if (instance != null)
            {
                return(instance);
            }

            if (gitServiceProvider != null)
            {
                instance = gitServiceProvider.GetService(serviceType);
                if (instance != null)
                {
                    return(instance);
                }
            }

            return(null);
        }
Example #19
0
        /// <summary>
        /// Called to request a service implementation.
        ///
        /// Here we call upon MEF to instantiate implementations of dependencies.
        /// </summary>
        /// <param name="serviceType">Type of service requested.</param>
        /// <returns>Service implementation or null.</returns>
        public object GetService(Type serviceType)
        {
            if (serviceType == null)
            {
                throw new ArgumentNullException("serviceType");
            }

            var name   = AttributedModelServices.GetContractName(serviceType);
            var export = _container.GetExportedValueOrDefault <object>(name);

            return(export);
        }
        public void SimpleChainWithLowerLoopRejection()
        {
            var parentCatalog = new TypeCatalog(typeof(BarImpl));
            var childCatalog  = new TypeCatalog(typeof(Foo3Impl));

            var scope     = parentCatalog.AsScope(childCatalog.AsScope());
            var container = new CompositionContainer(scope);

            var bar = container.GetExportedValueOrDefault <IBarContract>();

            Assert.Null(bar);
        }
Example #21
0
        private void SetupDefaultServices(CompositionContainer mefContainer)
        {
            if (mefContainer.GetExportedValueOrDefault <IResponseParser>() == null)
            {
                mefContainer.ComposeExportedValue <IResponseParser>(new ResponseParser());
            }

            if (mefContainer.GetExportedValueOrDefault <IBotServices>() == null)
            {
                mefContainer.ComposeExportedValue <IBotServices>(new SlackSendQueueService());
            }

            if (mefContainer.GetExportedValueOrDefault <IBotState>() == null)
            {
                mefContainer.ComposeExportedValue <IBotState>(new BotState());
            }

            if (mefContainer.GetExportedValueOrDefault <ISlackApi>() == null)
            {
                mefContainer.ComposeExportedValue <ISlackApi>(SlackApi.Create(this.apiKey));
            }

            if (mefContainer.GetExportedValueOrDefault <IBackOffStrategy>() == null)
            {
                mefContainer.ComposeExportedValue <IBackOffStrategy>(new BackOffRetry());
            }

            if (mefContainer.GetExportedValueOrDefault <ILog>() == null)
            {
                mefContainer.ComposeExportedValue <ILog>(LogManager.GetLogger(typeof(ISlackBot)));
            }
        }
Example #22
0
        public void CatalogSetup()
        {
            var catalog        = new AggregateCatalog(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
            var genericCatalog = new GenericCatalog(catalog);
            var container      = new CompositionContainer(genericCatalog);


            container.GetExportedValue <TEMP>().Say();

            container.GetExportedValueOrDefault <IUMC <string> >();
            container.GetExportedValueOrDefault <IUMC <int> >();

            var res = container.GetExportedValueOrDefault <IEntityCacheViewModel <IVisitType> >();

            container.GetExportedValueOrDefault <IUMC <IEntity <IId> > >().Say();


            container.GetExportedValueOrDefault <IUMC <IEntity <IId> > >().Say();

            container.GetExportedValueOrDefault <IUMC <string> >().Say();

            container.GetExportedValue <ITwoInterface <int, string> >().Say();
            container.GetExportedValue <ITwoInterface <string, int> >().Say();

            Console.WriteLine("-------------------------");
            var c = container.GetExports(typeof(IUMC <>), null, null);

            Console.WriteLine(c.Count());

            Console.ReadLine();
        }
Example #23
0
        public void SelfExportWithClosedGenericImportTest()
        {
            TypeCatalog          catalog   = new TypeCatalog(typeof(SelfImport <>), typeof(OpenGenericPartWithClosedGenericImport <>));
            CompositionContainer container = new CompositionContainer(catalog);

            var export = container.GetExportedValueOrDefault <IExport <object> >();

            Assert.IsNotNull(export);

            OpenGenericPartWithClosedGenericImport <object> impl = export as OpenGenericPartWithClosedGenericImport <object>;

            Assert.IsNotNull(impl);
            Assert.IsNotNull(impl.ClosedImport);
        }
Example #24
0
        public void SelfExportWithNakedLazyPropertyImportTest()
        {
            TypeCatalog          catalog   = new TypeCatalog(typeof(SelfExportWithNakedLazyPropertyImport <>), typeof(Foo));
            CompositionContainer container = new CompositionContainer(catalog);

            var export = container.GetExportedValueOrDefault <IExport <IFoo> >();

            Assert.IsNotNull(export);

            var partWithImport = export as IPartWithImport;

            Assert.IsNotNull(partWithImport);
            Assert.IsNotNull(partWithImport.GetValue());
        }
Example #25
0
        public void SelfExportWithParameterImportTest()
        {
            TypeCatalog          catalog   = new TypeCatalog(typeof(SelfExportWithParameterImport <,>), typeof(SelfImport <,>));
            CompositionContainer container = new CompositionContainer(catalog);

            var export = container.GetExportedValueOrDefault <IExport <IFoo, IBar> >();

            Assert.IsNotNull(export);

            var partWithImport = export as IPartWithImport;

            Assert.IsNotNull(partWithImport);
            Assert.IsNotNull(partWithImport.GetValue());
        }
        private static void Main(string[] args)
        {
            var catalogs = new AggregateCatalog();
            var catalog  = new System.ComponentModel.Composition.Hosting.AssemblyCatalog(Assembly.GetExecutingAssembly());

            catalogs.Catalogs.Add(catalog);
            var          sessionModel = new SessionModel(3);
            var          container    = new CompositionContainer(catalog);
            ISomeService someService  = container.GetExportedValueOrDefault <ISomeService>(sessionModel.cname);

            if (someService != null)
            {
                someService.DoSomething();
            }
        }
Example #27
0
 /// <summary>
 /// 获取指定协定名的部件
 /// </summary>
 /// <typeparam name="TPart">要导出的部件类型</typeparam>
 /// <param name="contractName">部件协定名</param>
 /// <returns></returns>
 public TPart GetPart <TPart>(string contractName)
 {
     try
     {
         TPart tmpResult;
         if (string.IsNullOrEmpty(contractName))
         {
             tmpResult = com.GetExportedValueOrDefault <TPart>();
         }
         else
         {
             tmpResult = com.GetExportedValueOrDefault <TPart>(contractName);
         }
         if (tmpResult is TPart)
         {
             return((TPart)tmpResult);
         }
     }
     catch (Exception ex)
     {
         LoggerManagerSingle.Instance.Error(ex);
     }
     return(default(TPart));
 }
Example #28
0
        protected override void OnStartup(object sender, StartupEventArgs e)
        {
            DisplayRootViewFor <IShell>();
            RegisterApplicationCommandHandlers();

#if (!DEBUG)
            Task.Factory.StartNew(() =>
            {
                AppUpdateManager.Current.CheckForUpdates(false).ConfigureAwait(false);
            });
#endif

            var pipeServiceBootstrapper = _container.GetExportedValueOrDefault <PipeServiceBootstrapper>();

            pipeServiceBootstrapper?.Init();
        }
Example #29
0
        /// <summary>
        /// Get a single object that exports the typeparam.
        /// </summary>
        /// <remarks>
        /// If there are multiple objects that export the typeparam then the first one is
        /// returned. There is no guarantee which object will be the first.
        /// </remarks>
        /// <typeparam name="T">An class type to find an export for.</typeparam>
        /// <returns>
        /// A constructed object of type T if a valid object is found. Otherwise null.
        /// </returns>
        public static T GetService <T>()
        {
            T retVal = default(T);

            if (IsInitialized)
            {
                try
                {
                    retVal = m_CompositionContainer.GetExportedValueOrDefault <T>();
                }
                catch (ImportCardinalityMismatchException)
                {
                    retVal = m_CompositionContainer.GetExportedValues <T>().FirstOrDefault();
                }
            }
            return(retVal);
        }
        public static TypeDescriptorServices GetTypeDescriptorServicesForContainer(CompositionContainer container)
        {
            if (container != null)
            {
                var result = container.GetExportedValueOrDefault <TypeDescriptorServices>();
                if (result == null)
                {
                    var v = new TypeDescriptorServices();
                    CompositionBatch batch = new CompositionBatch();
                    batch.AddPart(v);
                    container.Compose(batch);
                    return(v);
                }

                return(result);
            }
            return(null);
        }