-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add datareader mapper, some code cleanup
- Loading branch information
Showing
6 changed files
with
123 additions
and
55 deletions.
There are no files selected for viewing
This file contains 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
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> |
This file contains 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
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); | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
||
|
@@ -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 | ||
|
@@ -107,12 +108,12 @@ public enum ThrottleInterval | |
/// <summary> | ||
/// The minute. | ||
/// </summary> | ||
Minute, | ||
Minute, | ||
|
||
/// <summary> | ||
/// The hour. | ||
/// </summary> | ||
Hour, | ||
Hour, | ||
|
||
/// <summary> | ||
/// The day. | ||
|
@@ -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 | ||
|
||
|
@@ -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; | ||
} | ||
|
||
|
@@ -169,8 +170,8 @@ public int Count() | |
/// </summary> | ||
public void Increment() | ||
{ | ||
this.counter.Add(DateTime.Now); | ||
this.Compact(); | ||
_counter.Add(DateTime.Now); | ||
Compact(); | ||
} | ||
|
||
#endregion | ||
|
@@ -182,7 +183,7 @@ public void Increment() | |
/// </summary> | ||
private void Compact() | ||
{ | ||
this.Compact(this.GetStartTime()); | ||
Compact(GetStartTime()); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -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); | ||
|
This file contains 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
This file contains 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