-
-
Save hpinio/7527825 to your computer and use it in GitHub Desktop.
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
public class Widget | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public string Description { get; set; } | |
} |
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
public IEnumerable<Widget> GetAllWidgets(SqlConnection connection) | |
{ | |
List<Widget> widgets = new List<Widget>(); | |
using (SqlCommand command = new SqlCommand("SELECT [Id], [Name], [Description] FROM [Widgets]", connection)) | |
using (SqlDataReader reader = command.ExecuteReader()) | |
{ | |
while (reader.read()) | |
{ | |
widgets.Add | |
( | |
new Widget | |
{ | |
Id = reader.GetInt32(0), | |
Name = reader.GetString(1), | |
Description = reader.GetString(2) | |
} | |
); | |
} | |
} | |
return widgets; | |
} |
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
public IEnumerable<Widget> GetAllWidgets(SqlConnection connection) | |
{ | |
return connection.Query<Widget> | |
( | |
"SELECT [Id], [Name], [Description] FROM [Widgets]" | |
); | |
} |
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
public void AddWidget(SqlConnection connection, Widget widget) | |
{ | |
connection.Execute | |
( | |
"INSERT INTO [Widgets] ([Id], [Name], [Description]) VALUES (@Id, @Name, @Description)", | |
widget | |
); | |
} |
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
public class Category | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public List<Widget> Widgets { get; set; } | |
public Category() | |
{ | |
Widgets = new List<Widget>(); | |
} | |
} | |
public class Widget | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public Category Category { get; set; } | |
} |
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
Query<TFirst, TSecond, ..., TReturn> | |
( | |
string query, | |
Func<TFIrst, TSecond, ... TReturn> map, | |
... | |
) |
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
public IEnumerable<Category> GetHierarchy(SqlConnection connection) | |
{ | |
return connection.Query<Category, Widget, Category> | |
( | |
@"SELECT | |
[Categories].[Id], | |
[Categories].[Name], | |
[Widgets].[Id], | |
[Widgets].[Name] | |
FROM | |
[Categories] | |
JOIN | |
[Widgets] | |
ON | |
[Widgets].[CategoryId] = [Categories].[Id]", | |
(category, widget) => | |
{ | |
category.Widgets.Add(widget); | |
widget.Category = category; | |
return category; | |
} | |
); | |
} |
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
public IEnumerable<Category> GetHierarchy(SqlConnection connection) | |
{ | |
// Use a lookup to store unique categories. | |
Dictionary<int, Category> lookup = new Dictionary<int, Category>(); | |
return connection.Query<Category, Widget, Category> | |
( | |
@"SELECT | |
[Categories].[Id], | |
[Categories].[Name], | |
[Widgets].[Id], | |
[Widgets].[Name] | |
FROM | |
[Categories] | |
JOIN | |
[Widgets] | |
ON | |
[Widgets].[CategoryId] = [Categories].[Id]", | |
(possibleDupeCategory, widget) => | |
{ | |
Category category; | |
// Look for the current category, storing it in `category` if it | |
// exists. | |
if (!lookup.TryGetValue(possibleDupeCategory.Id, out category)) | |
{ | |
// If the lookup doesn't contain the current category, add | |
// it and store it in `category` as well. | |
lookup.Add(possibleDupeCategory.Id, possibleDupeCategory); | |
category = possibleDupeCategory; | |
} | |
// Regardless of the state of the lookup before this mapping, | |
// `category` now refers to a distinct category. | |
category.Widgets.Add(widget); | |
widget.Category = category; | |
return category; | |
} | |
).Distinct(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment