Ejemplo n.º 1
0
        public async Task Initialize()
        {
            _community = new Community(new MemoryStorageStrategy())
                .Register<CorrespondenceModel>();

            _company = await _community.AddFactAsync(new Company("improvingEnterprises"));
            _quarter = await _community.AddFactAsync(new Quarter(_company, new DateTime(2013, 1, 1)));
            _categoryGenerator = new CategoryGenerator(_community, _company, _quarter);
            await _categoryGenerator.GenerateAsync();
        }
        public async void Initialize()
        {
            try
            {
                var storage = new FileStreamStorageStrategy();
                var http = new HTTPConfigurationProvider();
                var communication = new BinaryHTTPAsynchronousCommunicationStrategy(http);

                _community = new Community(storage);
                //_community.AddAsynchronousCommunicationStrategy(communication);
                _community.Register<CorrespondenceModel>();
                _community.Subscribe(() => _individual.Value);

                // Synchronize periodically.
                DispatcherTimer timer = new DispatcherTimer();
                int timeoutSeconds = Math.Min(http.Configuration.TimeoutSeconds, 30);
                timer.Interval = TimeSpan.FromSeconds(5 * timeoutSeconds);
                timer.Tick += delegate(object sender, object e)
                {
                    Synchronize();
                };
                timer.Start();

                var company = await _community.AddFactAsync(new Company("improvingEnterprises"));
                var quarter = await _community.AddFactAsync(new Quarter(company, CurrentQuarter));

                var categoryGenerator = new CategoryGenerator(_community, company, quarter);
                await categoryGenerator.GenerateAsync();

                lock (this)
                {
                    _company.Value = company;
                    _quarter.Value = quarter;
                }

                Individual individual = await _community.LoadFactAsync<Individual>(ThisIndividual);
                if (individual == null)
                {
                    string randomId = Punctuation.Replace(Guid.NewGuid().ToString(), String.Empty).ToLower();
                    individual = await _community.AddFactAsync(new Individual(randomId));
                    await _community.SetFactAsync(ThisIndividual, individual);
                }
                lock (this)
                {
                    _individual.Value = individual;
                }
                http.Individual = individual;

                // Synchronize whenever the user has something to send.
                _community.FactAdded += delegate
                {
                    Synchronize();
                };

                // Synchronize when the network becomes available.
                System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged += (sender, e) =>
                {
                    if (NetworkInterface.GetIsNetworkAvailable())
                        Synchronize();
                };

                // And synchronize on startup or resume.
                Synchronize();
            }
            catch (Exception x)
            {
                System.Diagnostics.Debug.WriteLine(x.Message);
            }
        }