예제 #1
0
        public Type IfaceImpl(Type type)
        {
            var impl = FaceImplRegister.Get(type);

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

            if (!type.IsAbstract && type.IsClass)
            {
                return(type);
            }

            ValidateReflectionPermission();
            if (type.IsInterface || type.IsAbstract)
            {
                var dsias = (DefaultImplAttribute[])type.GetCustomAttributes(typeof(DefaultImplAttribute), true);
                if (dsias.Any())
                {
                    var dsia = dsias.FirstOrDefault(x => x.TargetType != null);
                    if (dsia == null)
                    {
                        throw new PlasmaException(string.Format(CultureInfo.CurrentCulture, "Service for type '{0}' cannot be loaded by non-resolvable DefaultImpl: " + string.Join(", ", dsias.Select(x => x.TypeAqn)), type.Name));
                    }
                    type = dsia.TargetType;
                }
                else
                {
                    Exception exinner = null;
                    try
                    {
                        if (type.IsInterface && type.Name.StartsWith("I"))
                        {
                            var expectedName = type.Name.Substring(1);

                            var matched = AssemblyAnalyzeCache.SearchFor(expectedName)
                                          .Where(x => type.IsAssignableFrom(x))
                                          .ToArray();

                            if (matched.Length == 1)
                            {
                                return(matched[0]);
                            }

                            var impls = AssemblyAnalyzeCache.ImplsOf(type);
                            if (impls != null && impls.Count == 1)
                            {
                                return(impls.First());
                            }
                            else if (impls == null)
                            {
                                throw new Exception(string.Format("Impls of type '{0}' not registered", type));
                            }
                            else
                            {
                                throw new Exception(string.Format("Impls of type '{0}': {1}", PlasmaContainer.GetTypeName(type), string.Join(", ", impls.Select(PlasmaContainer.GetTypeName))));
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        exinner = ex;
                    }

                    throw new PlasmaException(string.Format(CultureInfo.CurrentCulture, "Cannot register service for type '{0}'. Specify instance, factory, use DefaultImplAttribute or just call class the same as interface", type.Name), exinner);
                }
            }
            return(type);
        }
예제 #2
0
        public static void Run(Plasma.ReflectionPermission?reflectionPermission = null)
        {
            if (_executed)
            {
                return;
            }
            _executed = true;
            Plasma.PlasmaContainer.DefaultReflectionPermission = reflectionPermission ?? Plasma.ReflectionPermission.Throw;



// Factory
            TypeFactoryRegister.Add <Class1>(c => new Class1());
            TypeFactoryRegister.Add <SessionFactory>(c => new SessionFactory());
            TypeFactoryRegister.Add <Session>(c => c.Get <SessionFactory>().Create());
            TypeFactoryRegister.Add <GenericPerformer>(c => new GenericPerformer(c.Get <HibernateCrudDao <IMyService> >()));
            TypeFactoryRegister.Add <MyGenericMethod>(c => new MyGenericMethod());
            TypeFactoryRegister.Add <MyService4>(c => new MyService4());
            TypeFactoryRegister.Add <MyService5>(c => new MyService5());
            TypeFactoryRegister.Add <MyServiceWithAutomaticSetterOnlyInjection>(c => new MyServiceWithAutomaticSetterOnlyInjection());
            TypeFactoryRegister.Add <MyServiceWithMatchedIface>(c => new MyServiceWithMatchedIface());
            TypeFactoryRegister.Add <MyServiceWithUniqueIFace>(c => new MyServiceWithUniqueIFace());
            TypeFactoryRegister.Add <PrivateInner>(c => new PrivateInner());
            TypeFactoryRegister.Add <SuggestedProxyMembershipProvider>(c => new SuggestedProxyMembershipProvider(c.Get <IMembershipProvider>()));
            TypeFactoryRegister.Add <DataLazyConstructorInjection>(c => new DataLazyConstructorInjection(new Lazy <IMyService>(c.Get <IMyService>)));
            TypeFactoryRegister.Add <DataFuncConstructorInjection>(c => new DataFuncConstructorInjection(c.Get <IMyService>));
            TypeFactoryRegister.Add <DataLazyPropertyInjection>(c => new DataLazyPropertyInjection());
            TypeFactoryRegister.Add <DataFuncPropertyInjection>(c => new DataFuncPropertyInjection());
            TypeFactoryRegister.Add <MyInmemStorage>(c => new MyInmemStorage());
            TypeFactoryRegister.Add <MyPipeStorage>(c => new MyPipeStorage());
            TypeFactoryRegister.Add <MyFileStorage>(c => new MyFileStorage());
            TypeFactoryRegister.Add <MyNodeHost>(c => new MyNodeHost());
            TypeFactoryRegister.Add <MyObjectMan>(c => new MyObjectMan(c.Get <IMyStorage, MyPipeStorage>()));
            TypeFactoryRegister.Add <MyPerformer>(c => new MyPerformer());
            TypeFactoryRegister.Add <MyService>(c => new MyService());
            TypeFactoryRegister.Add <MyService6WithoutInterface>(c => new MyService6WithoutInterface(c.Get <MyService7Dependency>()));
            TypeFactoryRegister.Add <MyService7Dependency>(c => new MyService7Dependency());
#warning No constructor for type 'MyBadServiceDep'

/*
 * No constructor for type 'MyBadServiceDep'
 * Inner: No constructor for type 'MyBadServiceDep'
 */
            TypeFactoryRegister.Add <MyBadService>(c => new MyBadService(c.Get <MyBadServiceDep>()));
            TypeFactoryRegister.Add <MyService2>(c => new MyService2(c.Get <IMyService>()));
            TypeFactoryRegister.Add <MyService3>(c => new MyService3(c.Get <IMyPerformer>()));
            TypeFactoryRegister.Add <MyServiceWithOptionalArguments>(c => new MyServiceWithOptionalArguments(c.Get <IMyStorage>(), c.TryGet <IMyWorker>()));
            TypeFactoryRegister.Add <MyServiceWithString>(c => new MyServiceWithString(default(string)));
            TypeFactoryRegister.Add <MyServiceWithStruct>(c => new MyServiceWithStruct(default(Guid)));
            TypeFactoryRegister.Add <MyServiceWithOptionalStruct>(c => new MyServiceWithOptionalStruct());
            TypeFactoryRegister.Add <MyServiceWithOptionalString>(c => new MyServiceWithOptionalString());
            TypeFactoryRegister.Add <MyServiceWithStructPro>(c => new MyServiceWithStructPro());
            TypeFactoryRegister.Add <MyServiceWithSeveralCtors>(c => new MyServiceWithSeveralCtors(c.Get <IMyService>(), c.Get <IMyService>()));
            TypeFactoryRegister.Add <MySubGroup>(c => new MySubGroup(c.Get <Plasma.IPlasmaProvider>()));
            TypeFactoryRegister.Add <MyWorker>(c => new MyWorker());
            TypeFactoryRegister.Add <HibernateCrudDao <IMyService> >(c => new HibernateCrudDao <IMyService>());
            TypeFactoryRegister.Add <HibernateCrudDao <IMyService2> >(c => new HibernateCrudDao <IMyService2>());
            TypeFactoryRegister.Add <HibernateCrudDao <IMyService3> >(c => new HibernateCrudDao <IMyService3>());
            TypeFactoryRegister.Add <Plasma.PlasmaContainer>(c => new Plasma.PlasmaContainer());

// Iface impl
            FaceImplRegister.Register <ISession, Session>();
            FaceImplRegister.Register <IMyService4, MyService4>();
            FaceImplRegister.Register <IMyServiceComplex, MyService5>();
            FaceImplRegister.Register <IMyServiceWithMatchedIface, MyServiceWithMatchedIface>();
            FaceImplRegister.Register <IMyUniqueIFace, MyServiceWithUniqueIFace>();
#warning Cannot register service for type 'IPrivateIFace'. Specify instance, factory, use DefaultImplAttribute or just call class the same as interface

/*
 * Cannot register service for type 'IPrivateIFace'. Specify instance, factory, use DefaultImplAttribute or just call class the same as interface
 * Inner: Impls of type 'PlasmaTests.Sample.IPrivateIFace' not registered
 */
#warning Cannot register service for type 'ISimpleDataForStubbing'. Specify instance, factory, use DefaultImplAttribute or just call class the same as interface

/*
 * Cannot register service for type 'ISimpleDataForStubbing'. Specify instance, factory, use DefaultImplAttribute or just call class the same as interface
 * Inner: Impls of type 'PlasmaTests.Sample.Proxy.ISimpleDataForStubbing' not registered
 */
#warning Cannot register service for type 'IComplexStubbing'. Specify instance, factory, use DefaultImplAttribute or just call class the same as interface

/*
 * Cannot register service for type 'IComplexStubbing'. Specify instance, factory, use DefaultImplAttribute or just call class the same as interface
 * Inner: Impls of type 'PlasmaTests.Sample.Proxy.IComplexStubbing' not registered
 */
#warning Cannot register service for type 'IComplexStubbingDerived'. Specify instance, factory, use DefaultImplAttribute or just call class the same as interface

/*
 * Cannot register service for type 'IComplexStubbingDerived'. Specify instance, factory, use DefaultImplAttribute or just call class the same as interface
 * Inner: Impls of type 'PlasmaTests.Sample.Proxy.IComplexStubbingDerived' not registered
 */
#warning Cannot register service for type 'IComplexStubbingDerived2'. Specify instance, factory, use DefaultImplAttribute or just call class the same as interface

/*
 * Cannot register service for type 'IComplexStubbingDerived2'. Specify instance, factory, use DefaultImplAttribute or just call class the same as interface
 * Inner: Impls of type 'PlasmaTests.Sample.Proxy.IComplexStubbingDerived2' not registered
 */
#warning Cannot register service for type 'IComplexStubbingDerived3'. Specify instance, factory, use DefaultImplAttribute or just call class the same as interface

/*
 * Cannot register service for type 'IComplexStubbingDerived3'. Specify instance, factory, use DefaultImplAttribute or just call class the same as interface
 * Inner: Impls of type 'PlasmaTests.Sample.Proxy.IComplexStubbingDerived3' not registered
 */
#warning Cannot register service for type 'IMembershipProvider'. Specify instance, factory, use DefaultImplAttribute or just call class the same as interface

/*
 * Cannot register service for type 'IMembershipProvider'. Specify instance, factory, use DefaultImplAttribute or just call class the same as interface
 * Inner: Impls of type 'PlasmaTests.Sample.Proxy.IMembershipProvider' not registered
 */
            FaceImplRegister.Register <IMyPerformer, MyPerformer>();
            FaceImplRegister.Register <IMyService, MyService>();
            FaceImplRegister.Register <IMyService2, MyService2>();
            FaceImplRegister.Register <IMyService3, MyService3>();
            FaceImplRegister.Register <IMyStorage, MyInmemStorage>();
            FaceImplRegister.Register <IMyWorker, MyWorker>();
            FaceImplRegister.Register <IMyServiceWithOptionalArguments, MyServiceWithOptionalArguments>();
            FaceImplRegister.Register <Plasma.IPlasmaProvider, Plasma.PlasmaContainer>();

// Plumbers (Property injectors)
            TypeAutoPlumberRegister.RegisterNone(typeof(Class1));
            TypeAutoPlumberRegister.RegisterNone(typeof(SessionFactory));
            TypeAutoPlumberRegister.RegisterNone(typeof(Session));
            TypeAutoPlumberRegister.Register <GenericPerformer>((c, x) => {
                x.Sdao2 = c.Get <HibernateCrudDao <IMyService2> >();
                x.Sdao3 = c.Get <HibernateCrudDao <IMyService3> >();
            });
            TypeAutoPlumberRegister.RegisterNone(typeof(MyGenericMethod));
            TypeAutoPlumberRegister.Register <MyService4>((c, x) => {
                x.Performer = c.Get <IMyPerformer>();
            });
            TypeAutoPlumberRegister.RegisterNone(typeof(MyService5));
            TypeAutoPlumberRegister.Register <MyServiceWithAutomaticSetterOnlyInjection>((c, x) => {
                x.Service = c.Get <IMyService>();
            });
            TypeAutoPlumberRegister.RegisterNone(typeof(MyServiceWithMatchedIface));
            TypeAutoPlumberRegister.RegisterNone(typeof(MyServiceWithUniqueIFace));
            TypeAutoPlumberRegister.RegisterNone(typeof(PrivateInner));
            TypeAutoPlumberRegister.RegisterNone(typeof(SuggestedProxyMembershipProvider));
            TypeAutoPlumberRegister.RegisterNone(typeof(DataLazyConstructorInjection));
            TypeAutoPlumberRegister.RegisterNone(typeof(DataFuncConstructorInjection));
            TypeAutoPlumberRegister.Register <DataLazyPropertyInjection>((c, x) => {
                x.LazyService = new Lazy <IMyService>(c.Get <IMyService>);
            });
            TypeAutoPlumberRegister.Register <DataFuncPropertyInjection>((c, x) => {
                x.LazyService = c.Get <IMyService>;
            });
            TypeAutoPlumberRegister.RegisterNone(typeof(MyInmemStorage));
            TypeAutoPlumberRegister.RegisterNone(typeof(MyPipeStorage));
            TypeAutoPlumberRegister.RegisterNone(typeof(MyFileStorage));
            TypeAutoPlumberRegister.Register <MyNodeHost>((c, x) => {
                x.Storage = c.Get <IMyStorage, MyFileStorage>();
            });
            TypeAutoPlumberRegister.RegisterNone(typeof(MyObjectMan));
            TypeAutoPlumberRegister.RegisterNone(typeof(MyPerformer));
            TypeAutoPlumberRegister.Register <MyService>((c, x) => {
                x.Worker = c.Get <IMyWorker>();
            });
            TypeAutoPlumberRegister.RegisterNone(typeof(MyService6WithoutInterface));
            TypeAutoPlumberRegister.RegisterNone(typeof(MyService7Dependency));
            TypeAutoPlumberRegister.RegisterNone(typeof(MyBadServiceDep));
            TypeAutoPlumberRegister.RegisterNone(typeof(MyBadService));
            TypeAutoPlumberRegister.RegisterNone(typeof(MyService2));
            TypeAutoPlumberRegister.RegisterNone(typeof(MyService3));
            TypeAutoPlumberRegister.RegisterNone(typeof(MyServiceWithOptionalArguments));
            TypeAutoPlumberRegister.RegisterNone(typeof(MyServiceWithString));
            TypeAutoPlumberRegister.RegisterNone(typeof(MyServiceWithStruct));
            TypeAutoPlumberRegister.RegisterNone(typeof(MyServiceWithOptionalStruct));
            TypeAutoPlumberRegister.RegisterNone(typeof(MyServiceWithOptionalString));
            TypeAutoPlumberRegister.RegisterNone(typeof(MyServiceWithStructPro));
            TypeAutoPlumberRegister.RegisterNone(typeof(MyServiceWithSeveralCtors));
            TypeAutoPlumberRegister.RegisterNone(typeof(MySubGroup));
            TypeAutoPlumberRegister.RegisterNone(typeof(MyWorker));
            TypeAutoPlumberRegister.Register <HibernateCrudDao <IMyService> >((c, x) => {
                x.Session = c.Get <ISession>();
            });
            TypeAutoPlumberRegister.Register <HibernateCrudDao <IMyService2> >((c, x) => {
                x.Session = c.Get <ISession>();
            });
            TypeAutoPlumberRegister.Register <HibernateCrudDao <IMyService3> >((c, x) => {
                x.Session = c.Get <ISession>();
            });
            TypeAutoPlumberRegister.RegisterNone(typeof(Plasma.PlasmaContainer));
            Null.Register <ISession>(NullSession.Instance);
            Null.Register <IMyService4>(NullMyService4.Instance);
            Null.Register <IMyServiceComplex>(NullMyServiceComplex.Instance);
            Null.Register <IMyServiceWithMatchedIface>(NullMyServiceWithMatchedIface.Instance);
            Null.Register <IMyUniqueIFace>(NullMyUniqueIFace.Instance);
            Null.Register <IPrivateIFace>(NullPrivateIFace.Instance);
            Null.Register <ISimpleDataForStubbing>(NullSimpleDataForStubbing.Instance);
            Null.Register <IComplexStubbing>(NullComplexStubbing.Instance);
            Null.Register <IComplexStubbingDerived>(NullComplexStubbingDerived.Instance);
            Null.Register <IComplexStubbingDerived2>(NullComplexStubbingDerived2.Instance);
            Null.Register <IComplexStubbingDerived3>(NullComplexStubbingDerived3.Instance);
            Null.Register <IMembershipProvider>(NullMembershipProvider.Instance);
            Null.Register <IMyPerformer>(NullMyPerformer.Instance);
            Null.Register <IMyService>(NullMyService.Instance);
            Null.Register <IMyService2>(NullMyService2.Instance);
            Null.Register <IMyService3>(NullMyService3.Instance);
            Null.Register <IMyStorage>(NullMyStorage.Instance);
            Null.Register <IMyWorker>(NullMyWorker.Instance);
            Null.Register <IMyServiceWithOptionalArguments>(NullMyServiceWithOptionalArguments.Instance);
            Null.RegisterGeneric(typeof(IEnumerable <>), t =>
                                 typeof(NullEnumerable <>).MakeGenericType(t).GetField("Instance").GetValue(null));
            Null.RegisterGeneric(typeof(IList <>), t =>
                                 typeof(NullList <>).MakeGenericType(t).GetField("Instance").GetValue(null));
            Null.RegisterGeneric(typeof(IEnumerator <>), t =>
                                 typeof(NullEnumerator <>).MakeGenericType(t).GetField("Instance").GetValue(null));
            Null.Register <System.Collections.IEnumerator>(NullEnumerator.Instance);
        }