Ejemplo n.º 1
0
        public static void RegisterViewServiceInstance(Type viewServiceType, string name, object instance)
        {
            Guard.ArgumentNotNullOrWhiteSpace(name, "name");
            Guard.ArgumentNotNull(instance, "instance");

            // we get the view service locator and we unregister
            var _viewServiceMeta = GetViewServiceMeta(viewServiceType, name);

            if (_viewServiceMeta.Lifetime != ViewServiceLifetime.SelfRegisteredInstance)
            {
                throw new InvalidOperationException(string.Format(INSTANCEREGISTER_ERROR, viewServiceType.FullName));
            }

            // and register
            ViewServicesInstancesManager.RegisterInstance(viewServiceType, instance);
        }
        private Object InitializeInstance()
        {
            switch (ResourceMeta.Lifetime)
            {
            case ViewServiceLifetime.Singleton:
                if (_instance != null)
                {
                    return(_instance);
                }
                lock (_lock)
                {
                    if (_instance == null)
                    {
                        _instance = CreateObserverTypeInstance();
                    }
                }
                return(_instance);

            case ViewServiceLifetime.PerInstance:
                lock (_lock)
                {
                    return(CreateObserverTypeInstance());
                }

            case ViewServiceLifetime.DiscoveredInstance:
                lock (_lock)
                {
                    // we search in the application's visual tree
                    DependencyObject _viewServiceElement = null;
                    return((GetViewServiceElement(Application.Current.MainWindow, out _viewServiceElement)) ?
                           _viewServiceElement : null);
                }

            case ViewServiceLifetime.SelfRegisteredInstance:
                return(ViewServicesInstancesManager.GetRegisteredInstance(ResourceMeta.ViewServiceType));
            }

            // all else
            throw new InvalidOperationException("Unable to resolve view service type.");
        }