Commits

Anonymous committed c06a52f

Minor refactoring

Comments (1)

Files changed (8)

HamOntFire.Core/GeoCoder.cs

                 
                 _logger.DebugFormat("URL: {0}", url);
 
-                var c = new WebClient();
-                var json = c.DownloadString(url);
+                string json;
+                using (var c = new WebClient())
+                {
+                    json = c.DownloadString(url);
+                }
                 _logger.DebugFormat("Response: {0}", json);
                 JObject jObject = JObject.Parse(json);
 

HamOntFire.Core/MapGenerator.cs

         private const int HeatMapWidth = 916;
         private const int HeatMapHeight = 844;
 
-        private IDocumentSession _session;
-
-        public MapGenerator(IDocumentSession session)
+        public Bitmap Generate(string type, int ward, IDocumentSession session)
         {
-            _session = session;
-        }
-
-        public Bitmap Generate(string type, int ward)
-        {
-            var td = new TweetManager(_session);
+            var td = new TweetManager(session);
             var events = td.GetEvents(type, ward);
 
             // Create new memory bitmap
             return (radians);
         }
         
-        public static Bitmap Colorize(Bitmap Mask, byte Alpha)
+        public static Bitmap Colorize(Image mask, byte alpha)
         {
             // Create new bitmap to act as a work surface for the colorization process
-            Bitmap Output = new Bitmap(Mask.Width, Mask.Height, PixelFormat.Format32bppArgb);
+            Bitmap Output = new Bitmap(mask.Width, mask.Height, PixelFormat.Format32bppArgb);
 
             // Create a graphics object from our memory bitmap so we can draw on it and clear it's drawing surface
             Graphics Surface = Graphics.FromImage(Output);
 
             // Build an array of color mappings to remap our greyscale mask to full color
             // Accept an alpha byte to specify the transparancy of the output image
-            ColorMap[] Colors = CreatePaletteIndex(Alpha);
+            ColorMap[] Colors = CreatePaletteIndex(alpha);
 
             // Create new image attributes class to handle the color remappings
             // Inject our color map array to instruct the image attributes class how to do the colorization
             Remapper.SetRemapTable(Colors);
 
             // Draw our mask onto our memory bitmap work surface using the new color mapping scheme
-            Surface.DrawImage(Mask, new Rectangle(0, 0, Mask.Width, Mask.Height), 0, 0, Mask.Width, Mask.Height, GraphicsUnit.Pixel, Remapper);
+            Surface.DrawImage(mask, new Rectangle(0, 0, mask.Width, mask.Height), 0, 0, mask.Width, mask.Height, GraphicsUnit.Pixel, Remapper);
 
             // Send back newly colorized memory bitmap
             return Output;

HamOntFire.Core/TweetManager.cs

             GC.SuppressFinalize(this);
         }
 
-        private void Dispose(bool disposing)
+        protected void Dispose(bool disposing)
         {
             if (disposing)
             {
 
             var geocoder = new GeoCoder();
 
-            var c = new WebClient();
-            var json = c.DownloadString( url );
+            string json;
+            using (var c = new WebClient())
+            {
+                json = c.DownloadString(url);
+            }
             Logger.DebugFormat("Response: {0}", json);
             JArray jArray = JArray.Parse(json);
             var events = new List<Event>(jArray.Count);

HamOntFire.Test/MapGeneratorTests.cs

             _store.Initialize();
 
             _session = _store.OpenSession();
-            _generator = new MapGenerator(_session);
+            _generator = new MapGenerator();
         }
 
         [Test]

HamOntFire.Web/Controllers/StatisticsController.cs

         {
             if (type == string.Empty)
                 type = null;
-            var mapGen = new MapGenerator(RavenSession);
-            var bmap = mapGen.Generate(type, 0);
+            var mapGen = new MapGenerator();
+            var bmap = mapGen.Generate(type, 0, RavenSession);
             var ms = new MemoryStream();
             bmap.Save(ms, ImageFormat.Jpeg);
             ms.Position = 0;
     {
         public StatisticsViewModel()
         {
-            EventTypes = new List<SelectListItem>();
-            EventTypes.Add(new SelectListItem() {Text = "All", Value = string.Empty});
+            EventTypes = new List<SelectListItem> {new SelectListItem {Text = "All", Value = string.Empty}};
         }
 
         public List<Events_Count.ReduceResult> EventCountByType { get; set; }

HamOntFire.Web/Global.asax.cs

-using System.Reflection;
+using System;
+using System.Reflection;
 using System.Web.Mvc;
 using System.Web.Routing;
 using HamOntFire.Core;
     public class MvcApplication : System.Web.HttpApplication
     {
         private static ILog _logger;
+
+        [ThreadStatic]
         public static IDocumentStore Store;
 
         public static void RegisterGlobalFilters(GlobalFilterCollection filters)

HamOntFire.Web/Models/EventManager.cs

 
 namespace HamOntFire.Web.Models
 {
-    public class EventManager
+    public class EventManager : IDisposable
     {
 // ReSharper disable InconsistentNaming
         private readonly static Lazy<EventManager>  _instance = new Lazy<EventManager>(() => new EventManager());
         private long _greatestTweetId;
 
         public LoaderState State { get; set; }
+        public bool IsDisposed { get; private set; }
+
+        #region ctor/dtor/Dispose
 
         private EventManager()
         {
+            IsDisposed = false;
+
             using (var session = MvcApplication.Store.OpenSession())
             {
                 _logger.Debug("Getting greatest tweet.");
             }
         }
 
+        ~EventManager()
+        {
+            Dispose(false);
+        }
+
+        public void Dispose()
+        {
+            Dispose(true);
+            GC.SuppressFinalize(this);
+        }
+
+        private void Dispose(bool disposing)
+        {
+            if (disposing)
+            {
+                if (_timer != null)
+                    _timer.Dispose();
+                IsDisposed = true;
+            }
+        }
+
+        #endregion
+
         public static EventManager Instance
         {
             get { return _instance.Value; }

HamOntFire.Web/Models/EventManagerHub.cs

-using Microsoft.AspNet.SignalR.Hubs;
+using System;
+using Microsoft.AspNet.SignalR.Hubs;
 
 namespace HamOntFire.Web.Models
 {
 
         public EventManagerHub() : this(EventManager.Instance) { }
 
+#region ctor/dtor/Dispose
+
         public EventManagerHub(EventManager eventManager)
         {
             _eventManager = eventManager;
         }
 
+        ~EventManagerHub()
+        {
+            Dispose(false);
+        }
+
+        protected override void Dispose(bool disposing)
+        {
+            base.Dispose(disposing);
+            if (disposing)
+            {
+                if (_eventManager != null && !_eventManager.IsDisposed)
+                    _eventManager.Dispose();
+            }
+        }
+
+#endregion
+
         public void Start()
         {
             _eventManager.Start();