Created
August 13, 2013 11:39
-
-
Save uzzu/6220292 to your computer and use it in GitHub Desktop.
MVP Framework ;)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections.Generic; | |
| /// <summary> | |
| /// The base class of the Presenter class which does not operate View. | |
| /// </summary> | |
| public abstract class Presenter | |
| { | |
| protected Presenter() | |
| { | |
| } | |
| } | |
| /// <summary> | |
| /// The base class of the Presenter class which operates View | |
| /// </summary> | |
| public abstract class Presenter<TView> | |
| { | |
| #region properties | |
| public int ViewsCount | |
| { | |
| get | |
| { | |
| return views.Count; | |
| } | |
| } | |
| protected List<TView> views; | |
| #endregion | |
| #region public methods | |
| public void AddView(TView view) | |
| { | |
| if (view == null || ContainsView(view)) | |
| { | |
| return; | |
| } | |
| views.Add(view); | |
| } | |
| public void AddView(TView view, params TView[] moreViews) | |
| { | |
| AddView(view); | |
| AddView(moreViews); | |
| } | |
| public void AddView(IEnumerable<TView> views) | |
| { | |
| if (views == null) | |
| { | |
| return; | |
| } | |
| foreach (TView view in views) | |
| { | |
| AddView(view); | |
| } | |
| } | |
| public bool ContainsView(TView view) | |
| { | |
| return views.Contains(view); | |
| } | |
| public void RemoveView(TView view) | |
| { | |
| if (view == null || !ContainsView(view)) | |
| { | |
| return; | |
| } | |
| views.Remove(view); | |
| } | |
| public void RemoveView(TView view, params TView[] moreViews) | |
| { | |
| RemoveView(view); | |
| RemoveView(moreViews); | |
| } | |
| public void RemoveView(IEnumerable<TView> views) | |
| { | |
| if (views == null) | |
| { | |
| return; | |
| } | |
| foreach (TView view in views) | |
| { | |
| RemoveView(view); | |
| } | |
| } | |
| public void RemoveAllView(TView view) | |
| { | |
| views.Clear(); | |
| } | |
| #endregion | |
| #region private methods | |
| protected Presenter(TView view) : this() | |
| { | |
| AddView(view); | |
| } | |
| protected Presenter(IEnumerable<TView> views) : this() | |
| { | |
| AddView(views); | |
| } | |
| protected Presenter() | |
| { | |
| views = new List<TView>(); | |
| } | |
| protected IEnumerable<TView> GetViews() | |
| { | |
| return new List<TView>(views); | |
| } | |
| #endregion | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using NUnit.Framework; | |
| using System.Collections.Generic; | |
| [TestFixture] | |
| public class PresenterTest | |
| { | |
| public class MockPresenter : Presenter<MockPresenter.IView> | |
| { | |
| public interface IView | |
| { | |
| } | |
| } | |
| public class MockView : MockPresenter.IView | |
| { | |
| } | |
| [Test] | |
| public void TestViewDuplicationIsNotAllowed() | |
| { | |
| MockView view = new MockView(); | |
| MockPresenter presenter = new MockPresenter(); | |
| presenter.AddView(view); | |
| Assert.AreEqual(1, presenter.ViewsCount); | |
| Assert.IsTrue(presenter.ContainsView(view)); | |
| presenter.AddView(view); | |
| Assert.AreEqual(1, presenter.ViewsCount); | |
| Assert.IsTrue(presenter.ContainsView(view)); | |
| } | |
| [Test] | |
| public void TestRangeAddRange() | |
| { | |
| int viewsCount = 10; | |
| List<MockPresenter.IView> views = new List<MockPresenter.IView>(); | |
| for (int i = 0; i < viewsCount; i++) | |
| { | |
| views.Add(new MockView()); | |
| } | |
| MockPresenter presenter = new MockPresenter(); | |
| presenter.AddView(views[0], views[1], views[2], views[3]); | |
| Assert.AreEqual(4, presenter.ViewsCount); | |
| Assert.IsTrue(presenter.ContainsView(views[0])); | |
| Assert.IsTrue(presenter.ContainsView(views[1])); | |
| Assert.IsTrue(presenter.ContainsView(views[2])); | |
| Assert.IsTrue(presenter.ContainsView(views[3])); | |
| presenter.AddView(views); | |
| Assert.AreEqual(viewsCount, presenter.ViewsCount); | |
| foreach (MockView view in views) | |
| { | |
| Assert.IsTrue(presenter.ContainsView(view)); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment