コード例 #1
0
        public static void InitializeDependencies(this IMobileObject obj, ILifetimeScope scope)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            if (scope == null)
            {
                throw new ArgumentNullException("scope");
            }

            obj.GetType().GetProperties(DPABindingFlags)
                .Where(pi => Attribute.IsDefined(pi, typeof(DependencyAttribute)))
                .ToList<PropertyInfo>()
                .ForEach(pi =>
                {
                    DependencyAttribute atb = pi.GetCustomAttribute<DependencyAttribute>();

                    //if the dependency is registered in the scope - resolve it - if not - leave it null
                    if (scope.IsRegistered(pi.PropertyType))
                    {
                        //get the instance now - we need to make sure the underlying type for the requested service may implement IDisposable
                        //whereas the Interface for the requested service may not - we need to be sure in both cases!
                        object depPropInstance = scope.Resolve(pi.PropertyType);

                        //if the dependency is client side and implements IDisposable - 
                        //make sure the registration with Autofac is ExternallyOwned - otherwise throw an exception
                        //this should not be too expensive from a performance standpoint because we only do it if both conditions of
                        //1: the object is going to come back to the client side - AND - 
                        //2: the object implements the IDisposable interface 
                        //(which will not happen very often except in the case of bringing back IObjectPortal<T> types)
                        //if ((atb.DependencyScope == ResolutionScope.Client || atb.DependencyScope == ResolutionScope.ClientAndServer)
                        //    && typeof(IDisposable).IsAssignableFrom(depPropInstance.GetType()))
                        //{
                        //    IComponentRegistration reg = scope
                        //                                             .ComponentRegistry
                        //                                             .RegistrationsFor(new TypedService(pi.PropertyType))
                        //                                             .Where(e => e.Ownership == InstanceOwnership.ExternallyOwned).FirstOrDefault();
                        //    if (reg == null)
                        //    {
                        //        throw new ResolutionOwnershipException(string.Format(ExceptionMessages.DepPropertySetter_InvalidContainerOwnership, pi.Name, pi.PropertyType), obj.GetType(), pi, atb.DependencyScope, Csla.ApplicationContext.LogicalExecutionLocation.ToString());
                        //    }
                        //}

                        pi.SetValue(obj, depPropInstance);
                    }
                });

        }
コード例 #2
0
ファイル: EmailService.cs プロジェクト: aaronmars/DReAM
        //--- Methods ---
        protected override Yield Start(XDoc config, ILifetimeScope container, Result result)
        {
            yield return Coroutine.Invoke(base.Start, config, new Result());
            _defaultSettings = new SmtpSettings { Host = config["smtp-host"].AsText };
            if(string.IsNullOrEmpty(_defaultSettings.Host)) {
                _defaultSettings.Host = "localhost";
            }
            _log.DebugFormat("Smtp Host: {0}", _defaultSettings.Host);

            // Note (arnec): ssl requires mono 2.0 and likely root certificate import via 'mozroots --import --ask-remove --machine'
            _defaultSettings.EnableSsl = config["use-ssl"].AsBool ?? false;
            if(config["smtp-port"].AsInt.HasValue) {
                _defaultSettings.Port = config["smtp-port"].AsInt.Value;
            }
            _defaultSettings.AuthUser = config["smtp-auth-user"].AsText;
            _defaultSettings.AuthPassword = config["smtp-auth-password"].AsText;
            _clientFactory = container.IsRegistered<ISmtpClientFactory>()
                ? container.Resolve<ISmtpClientFactory>()
                : new SmtpClientFactory();

            // get an apikey for accessing the services without it's private/internal keys
            _emailApikey = config["apikey"].AsText;
            result.Return();
        }