Ejemplo n.º 1
0
        internal FolderDataSource(
            FilePath root,
            IFolderDataWatcherFactory watcherFactory       = null,
            IDirectoryInteractorFactory interactorFactory  = null,
            IFileInformationFactory fileInformationFactory = null)
        {
            this.files = new Dictionary <FilePath, IFileInformation>();
            this.root  = root;

            this.interactorFactory      = interactorFactory ?? new DirectoryInteractorFactory();
            this.watcherFactory         = watcherFactory ?? new FolderDataWatcherFactory();
            this.fileInformationFactory = fileInformationFactory ?? new FileInformationFactory();

            this.interactor = this.interactorFactory.MakeInteractor();
            this.watcher    = this.watcherFactory.MakeFolderDataWatcher(root);

            this.disposed     = false;
            this.disposalLock = new object();

            watcher.EnableRaisingEvents = true;

            this.watcher.Added += (obj, e) =>
            {
                this.Add(e);
                this.OnChange.Invoke(obj, e);
            };
            this.watcher.Changed += (obj, e) =>
            {
                this.Update(e);
                this.OnChange.Invoke(obj, e);
            };
            this.watcher.Moved += (obj, e) =>
            {
                this.Move(e);
                this.OnChange.Invoke(obj, e);
            };
            this.watcher.Removed += (obj, e) =>
            {
                this.Remove(e);
                this.OnChange.Invoke(obj, e);
            };

            foreach (FilePath path in this.interactor.EnumerateFiles(this.root))
            {
                this.files.Add(path, this.fileInformationFactory.FromPath(path));
            }
        }
        /// <inheritdoc />
        public IDataSource <IFileInformation> MakeDataSource(IBoundConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            IFolderDataWatcherFactory factory =
                (IFolderDataWatcherFactory)(configuration.GetOrDefault(
                                                FolderDataSourceFactory.watcherFactory,
                                                () => new FolderDataWatcherFactory(
                                                    (string)configuration.GetOrDefault(FolderDataSourceFactory.factoryFilter, () => null),
                                                    (long)configuration.GetOrDefault(FolderDataSourceFactory.factoryChangeFilterTicks, () => 0L))));

            return(new FolderDataSource(
                       (FilePath)configuration[FolderDataSourceFactory.path],
                       factory,
                       (IDirectoryInteractorFactory)configuration[FolderDataSourceFactory.interactorFactory]));
        }
Ejemplo n.º 3
0
        /// <inheritdoc />
        public IDataSource MakeDataSource(IReadOnlyDictionary <IConfigurationRequirement, object> bindings)
        {
            if (bindings == null)
            {
                throw new ArgumentNullException(nameof(bindings));
            }

            List <Exception> failures = new List <Exception>();
            Dictionary <IConfigurationRequirement, object> successes =
                new Dictionary <IConfigurationRequirement, object>();

            foreach (IConfigurationRequirement requirement in this.Requirements)
            {
                bool present = bindings.TryGetValue(requirement, out object binding);
                if (!present && !requirement.IsOptional)
                {
                    failures.Add(
                        new ArgumentException(
                            string.Format(
                                en_us.Culture,
                                en_us.FolderDataSourceFactoryMissingRequirement,
                                requirement.Name)));
                }
                else if (present)
                {
                    Exception exception = requirement.Validate(binding);

                    if (exception != null)
                    {
                        failures.Add(exception);
                    }
                    else
                    {
                        successes.Add(requirement, binding);
                    }
                }
            }

            foreach (KeyValuePair <IConfigurationRequirement, object> pair in
                     successes
                     .Where(x => !x.Key.DependsOn.All(y => successes.ContainsKey(y)))
                     .ToArray())
            {
                successes.Remove(pair.Key);
                failures.Add(
                    new ArgumentException(
                        string.Format(
                            en_us.Culture,
                            en_us.FolderDataSourceFactoryDependenciesNotSatisfied,
                            pair.Key.Name)));
            }

            foreach (KeyValuePair <IConfigurationRequirement, object> pair in
                     successes
                     .Where(x => x.Key.ExclusiveWith.Any(y => successes.ContainsKey(y)))
                     .ToArray())
            {
                successes.Remove(pair.Key);
                failures.Add(
                    new ArgumentException(
                        string.Format(
                            en_us.Culture,
                            en_us.FolderDataSourceFactoryConflictingRequirementsSpecified,
                            pair.Key.Name)));
            }

            if (failures.Any())
            {
                throw new AggregateException(
                          en_us.FolderDataSourceFactoryRequirementsFailedValidation,
                          failures);
            }

            IFolderDataWatcherFactory factory =
                (IFolderDataWatcherFactory)(successes.GetOrDefault(
                                                FolderDataSourceFactory.watcherFactory,
                                                () => new FolderDataWatcherFactory(
                                                    (string)successes.GetOrDefault(FolderDataSourceFactory.factoryFilter, () => null),
                                                    (long)successes.GetOrDefault(FolderDataSourceFactory.factoryChangeFilterTicks, () => 0L))));

            return(new FolderDataSource(
                       (FilePath)successes[FolderDataSourceFactory.path],
                       factory,
                       (IDirectoryInteractorFactory)successes[FolderDataSourceFactory.interactorFactory]));
        }