Skip to content

Instantly share code, notes, and snippets.

@hpinio
Forked from Lobstrosity/1.Widget.cs
Created November 18, 2013 13:38
Show Gist options
  • Save hpinio/7527825 to your computer and use it in GitHub Desktop.
Save hpinio/7527825 to your computer and use it in GitHub Desktop.
public class Widget
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
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;
}
public IEnumerable<Widget> GetAllWidgets(SqlConnection connection)
{
return connection.Query<Widget>
(
"SELECT [Id], [Name], [Description] FROM [Widgets]"
);
}
public void AddWidget(SqlConnection connection, Widget widget)
{
connection.Execute
(
"INSERT INTO [Widgets] ([Id], [Name], [Description]) VALUES (@Id, @Name, @Description)",
widget
);
}
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; }
}
Query<TFirst, TSecond, ..., TReturn>
(
string query,
Func<TFIrst, TSecond, ... TReturn> map,
...
)
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;
}
);
}
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