Skip to content

Commit

Permalink
Add datareader mapper, some code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lorddev committed Mar 18, 2017
1 parent 125619c commit 756fcba
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 55 deletions.
2 changes: 2 additions & 0 deletions Devlord.Utilities.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=DR/@EntryIndexedValue">DR</s:String></wpf:ResourceDictionary>
69 changes: 69 additions & 0 deletions src/Devlord.Utilities/DRMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MailThrottle.cs" company="Lord Design">
// (c) Lord Design. Modified GPL: You may use freely and commercially without modification; you can modify if result
// is also free.
// </copyright>
// <author>[email protected]</author>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace Devlord.Utilities
{
/// <summary>
/// Provides strong-typed results for data returned from ADO data readers
/// </summary>
/// <remarks>Similar to what EntityFramework and Automapper do, but with less "startup" overhead. It's useful when working on
/// small projects that don't have the EF scaffolding in place.
/// See http://improve.dk/performance-comparison-reading-data-from-the-database-strongly-typed/
/// </remarks>
public class DRMapper
{
public static List<T> ParseList<T>(SqlDataReader dr)
{
var list = new List<T>();

var properties = typeof(T).GetProperties();
var instance = Activator.CreateInstance<T>();

while (dr.Read())
{
foreach (var pi in properties)
{
pi.SetValue(instance, dr[pi.Name], null);
}

list.Add(instance);
}

return list;
}

public static T ParseRecord<T>(SqlDataReader dr, int rowIndex = 0)
{
var properties = typeof(T).GetProperties();
var instance = Activator.CreateInstance<T>();

var currentRow = 0;
while (dr.Read())
{
if (currentRow < rowIndex)
{
currentRow++;
continue;
}

foreach (var pi in properties)
{
pi.SetValue(instance, dr[pi.Name], null);
}

return instance;
}

return default(T);
}
}
}
1 change: 1 addition & 0 deletions src/Devlord.Utilities/Devlord.Utilities.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ApiResult.cs" />
<Compile Include="DRMapper.cs" />
<Compile Include="Services\ContinuousLoop.cs" />
<Compile Include="IApiCall.cs" />
<Compile Include="Services\LoopTimer.cs" />
Expand Down
75 changes: 38 additions & 37 deletions src/Devlord.Utilities/MailThrottle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
// </summary>
// <author>[email protected]</author>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace Devlord.Utilities
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

/// <summary>
/// The throttles.
/// </summary>
Expand All @@ -24,13 +25,13 @@ public class Throttles : IEachified<MailThrottle>
#region Constructors and Destructors

/// <summary>
/// Initializes a new instance of the <see cref="Throttles"/> class.
/// Initializes a new instance of the <see cref="Throttles" /> class.
/// </summary>
public Throttles()
{
this.MinuteThrottle = new MailThrottle { Interval = ThrottleInterval.Minute, Limit = 180 };
this.HourlyThrottle = new MailThrottle { Interval = ThrottleInterval.Hour, Limit = 3600 };
this.DailyThrottle = new MailThrottle { Interval = ThrottleInterval.Day, Limit = 10000 };
MinuteThrottle = new MailThrottle { Interval = ThrottleInterval.Minute, Limit = 180 };
HourlyThrottle = new MailThrottle { Interval = ThrottleInterval.Hour, Limit = 3600 };
DailyThrottle = new MailThrottle { Interval = ThrottleInterval.Day, Limit = 10000 };
}

#endregion
Expand All @@ -40,17 +41,17 @@ public Throttles()
/// <summary>
/// Gets the daily throttle.
/// </summary>
public MailThrottle DailyThrottle { get; private set; }
public MailThrottle DailyThrottle { get; }

/// <summary>
/// Gets the hourly throttle.
/// </summary>
public MailThrottle HourlyThrottle { get; private set; }
public MailThrottle HourlyThrottle { get; }

/// <summary>
/// Gets the minute throttle.
/// </summary>
public MailThrottle MinuteThrottle { get; private set; }
public MailThrottle MinuteThrottle { get; }

#endregion

Expand All @@ -64,36 +65,36 @@ public Throttles()
/// </param>
public void ForEach(Action<MailThrottle> func)
{
func(this.MinuteThrottle);
func(this.HourlyThrottle);
func(this.DailyThrottle);
func(MinuteThrottle);
func(HourlyThrottle);
func(DailyThrottle);
}

/// <summary>
/// The increment.
/// </summary>
public void Increment()
{
this.ForEach(throttle => throttle.Increment());
ForEach(throttle => throttle.Increment());
}

/// <summary>
/// The wait.
/// </summary>
/// <returns>
/// The <see cref="bool"/>.
/// The <see cref="bool" />.
/// </returns>
public void Wait()
{
this.ForEach(
ForEach(
x =>
{
while (x.Count() >= x.Limit)
{
while (x.Count() >= x.Limit)
{
Console.Write("Waiting...");
Thread.Sleep(100);
}
});
Console.Write("Waiting...");
Thread.Sleep(100);
}
});
}

#endregion
Expand All @@ -107,12 +108,12 @@ public enum ThrottleInterval
/// <summary>
/// The minute.
/// </summary>
Minute,
Minute,

/// <summary>
/// The hour.
/// </summary>
Hour,
Hour,

/// <summary>
/// The day.
Expand All @@ -130,7 +131,7 @@ public class MailThrottle
/// <summary>
/// The counter.
/// </summary>
private readonly List<DateTime> counter = new List<DateTime>();
private readonly List<DateTime> _counter = new List<DateTime>();

#endregion

Expand All @@ -154,13 +155,13 @@ public class MailThrottle
/// The count.
/// </summary>
/// <returns>
/// The <see cref="int"/>.
/// The <see cref="int" />.
/// </returns>
public int Count()
{
DateTime startTime = this.GetStartTime();
int count = this.counter.Count(x => x > startTime);
this.Compact(startTime);
var startTime = GetStartTime();
var count = _counter.Count(x => x > startTime);
Compact(startTime);
return count;
}

Expand All @@ -169,8 +170,8 @@ public int Count()
/// </summary>
public void Increment()
{
this.counter.Add(DateTime.Now);
this.Compact();
_counter.Add(DateTime.Now);
Compact();
}

#endregion
Expand All @@ -182,7 +183,7 @@ public void Increment()
/// </summary>
private void Compact()
{
this.Compact(this.GetStartTime());
Compact(GetStartTime());
}

/// <summary>
Expand All @@ -193,21 +194,21 @@ private void Compact()
/// </param>
private void Compact(DateTime startTime)
{
this.counter.RemoveAll(x => x < startTime);
_counter.RemoveAll(x => x < startTime);
}

/// <summary>
/// The get start time.
/// </summary>
/// <returns>
/// The <see cref="DateTime"/>.
/// The <see cref="DateTime" />.
/// </returns>
/// <exception cref="System.ArgumentOutOfRangeException"></exception>
private DateTime GetStartTime()
{
// Per minute 180, Per hour 3600, Per day 10,000
// http://support.google.com/a/bin/answer.py?hl=en&answer=1366776
switch (this.Interval)
switch (Interval)
{
case ThrottleInterval.Minute:
return DateTime.Now.AddMinutes(-1);
Expand Down
29 changes: 12 additions & 17 deletions src/Devlord.Utilities/RestRouteHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
// </copyright>
// -----------------------------------------------------------------------

using System.Web;
using System.Web.Compilation;
using System.Web.Routing;
using System.Web.UI;

namespace Devlord.Utilities
{
using System.Web;
using System.Web.Compilation;
using System.Web.Routing;
using System.Web.UI;

public struct RouteMap
{
public string PathFormat { get; set; }
Expand All @@ -24,27 +24,22 @@ public struct RouteMap

public class RestRouteHandler : IRouteHandler
{
private readonly string virtualPath;

private readonly string dataKey;
private readonly string _dataKey;
private readonly string _virtualPath;

public RestRouteHandler(string virtualPath, string dataKey)
{
this.virtualPath = virtualPath;
this.dataKey = dataKey;
_virtualPath = virtualPath;
_dataKey = dataKey;
}

public IHttpHandler GetHttpHandler(RequestContext context)
{
string path = string.Format(
"{0}?{1}={2}",
this.virtualPath,
this.dataKey,
context.RouteData.Values[this.dataKey]);
string path = $"{_virtualPath}?{_dataKey}={context.RouteData.Values[_dataKey]}";
HttpContext.Current.RewritePath(path);

var page = BuildManager.CreateInstanceFromVirtualPath(this.virtualPath, typeof(Page)) as IHttpHandler;
var page = BuildManager.CreateInstanceFromVirtualPath(_virtualPath, typeof(Page)) as IHttpHandler;
return page;
}
}
}
}
2 changes: 1 addition & 1 deletion test/Devlord.Utilities.Tests/DistanceApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void ReturnsExpectedResultWithCustomApi()
/// https://maps.googleapis.com/maps/api/distancematrix/json?sensor=false&origins=95969&destinations=95928
/// ...
/// </summary>
[Test]
[Test, Ignore]
public void ReturnsJsonResults()
{
const string endPoint =
Expand Down

0 comments on commit 756fcba

Please sign in to comment.