Click here to Skip to main content
15,895,011 members
Articles / Desktop Programming / Windows Forms

MVP-VM (Model View Presenter - ViewModel) with Data Binding and BackgroundWorker in Windows Forms applications

Rate me:
Please Sign up or sign in to vote.
4.98/5 (29 votes)
17 Jun 2010CPOL19 min read 136.4K   4.9K   99  
This article explains a way to create a Windows Forms app with the thinnest possible Form.cs files.
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Linq;
using System.Runtime.Serialization;

namespace DataBinding
{
    [DataContract]
    public class Solver
    {
        private int max;
        private HashSet<int> multiples;

        [DataMember(Name = "Seeds")]
        private PositiveIntegerSetCollection seeds = new PositiveIntegerSetCollection();

        [DataMember]
        public int Max
        {
            get
            {
                return this.max;
            }

            set
            {
                Contract.Requires(value > 0, "Max must be a positive integer.");
                this.max = value;
            }
        }

        public PositiveIntegerSetCollection Seeds
        {
            get { return this.seeds; }
        }

        [DataMember]
        public int? Answer { get; private set; }

        public void FindSumOfMultiplesOfSeedsBelowMax()
        {
            Contract.Requires(this.Max > 0, "Max must be assigned a positive integer before calling this function.");
            Contract.Requires(this.Seeds.Count > 0, "At least one seed needs to be added before calling this function.");

            this.Answer = null;
            this.multiples = new HashSet<int>();
            this.GetMultiples();
            this.GetSum();
        }

        protected virtual bool FirstArgIsMultipleOfSecondArg(int firstArg, int secondArg)
        {
            return firstArg % secondArg == 0;
        }

        private void GetSum()
        {
            try
            {
                this.Answer = this.multiples.Sum();
            }
            catch (OverflowException ex)
            {
                var msg = string.Format(
                    CultureInfo.CurrentCulture,
                    "The sum was greater than {0}.\nThis program can not handle numbers larger than that. Please try different set of values.", 
                    int.MaxValue);
                throw new OperationFailedException(msg, ex);
            }
            finally
            {
                this.multiples.Clear();
            }
        }

        private void GetMultiples()
        {
            foreach (var v in this.seeds)
            {
                this.GetMultiplesOf(v);
            }
        }

        private void GetMultiplesOf(int seed)
        {
            for (var i = 1; i < this.Max; i++)
            {
                if (this.FirstArgIsMultipleOfSecondArg(i, seed))
                {
                    this.AccumulateResults(i);
                }
            }
        }

        private void AccumulateResults(int value)
        {
            try
            {
                this.multiples.Add(value);
            }
            catch (OutOfMemoryException ex)
            {
                this.multiples.Clear();
                var msg = string.Format(
                    CultureInfo.CurrentCulture, 
                    "Out of memory. Too many multiples ({0}) found...\nPlease try different set of values.",
                    this.multiples.Count);
                throw new OperationFailedException(msg, ex);
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Japan Japan
He started his career as a PDP-11 assembly language programmer in downtown Tokyo, learning what "patience" in real life means by punching a 110 baud ASR-33 Teletype frantically. He used to be able to put in the absolute loader sequence through the switch panel without consulting the DEC programming card.

Since then, his computer language experiences include 8051 assembly, FOCAL, BASIC, FORTRAN-IV, Turbo/MS C, VB. VB.NET, and C#.

Now, he lives with his wife, two grown-up kids (get out of my place!), and two cats in Westerville, Ohio.

Comments and Discussions