Skip to content

Commit 8d82a2e

Browse files
authored
Update README.md
1 parent 223ca5e commit 8d82a2e

File tree

1 file changed

+94
-95
lines changed

1 file changed

+94
-95
lines changed

Diff for: README.md

+94-95
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ Slapper.AutoMapper maps dynamic data to static types.
99

1010
### What is it? ###
1111

12-
Slapper.AutoMapper ( Pronounced Slapper-Dot-Automapper ) is a mapping library that can convert dynamic data into
12+
Slapper.AutoMapper ( Pronounced Slap-er dot aw-toe-map-er ) is a mapping library that can convert dynamic data into
1313
static types and populate complex nested child objects.
1414

1515
It primarily converts C# dynamics and `IDictionary<string, object>` to strongly typed objects and supports
1616
populating an entire object graph by using underscore notation to underscore into nested objects.
1717

18-
Why use an IDictionary? Because a C# dynamic ( well really an ExpandoObject ) can easily be cast to one allowing
18+
Why use an IDictionary? Because a C# dynamic ( well really an ExpandoObject ) can easily be cast to an `IDictionary<string, object>` allowing
1919
this library to be used in a variety of ways not only with dictionaries of property names and values but with dynamics as well.
2020

2121
Okay, so what... doesn't other ORMs do this?
@@ -26,16 +26,16 @@ a Customer and it's list of Orders and it's list of OrderDetails.
2626

2727
### Is this an ORM? ###
2828

29-
No, this is not an ORM in itself but can easily be extended to create one. This library can be thought of as a building
29+
No, this is not an ORM but can be easily extended to create one. This library can be thought of as a building
3030
block of an ORM or used as an extension to an existing ORM or Micro-ORM.
3131

32-
ORMs typically query the database and then map the data into objects. Slapper just handles the mapping part and essentially
32+
ORMs typically query the database and then map the data into objects. Slapper handles the mapping part and essentially
3333
only has one input: a dictionary of property names and values.
3434

3535
### What problems does this solve? ###
3636

3737
Simply put, it allows you to convert dynamic data into strongly typed objects with ease and populating complex nested child
38-
objects in your object hierarchy comes free out of the box --something severely lacking in almost every Micro-ORM solution!
38+
objects in your object hierarchy comes for free out of the box --something severely lacking in almost every Micro-ORM solution!
3939

4040
### Auto mapping? ###
4141

@@ -67,10 +67,9 @@ you are wishing to populate. Slapper.AutoMapper does not handle data type conver
6767
data into the library.
6868

6969
And that's it, feel free to explore the examples below and the unit tests and hack away. This library is licensed with the MIT license
70-
so feel free to re-use the code in your own projects any way you please. I only ask that you keep the license comment at the top of the
71-
file or any file that uses significant portions of this projects code for proper attribution.
70+
so feel free to re-use the code in your own projects any way you please as long as you provide proper attribution.
7271

73-
Slapper.AutoMapper is also available on NuGet as a compiled dll if you prefer that. Check it out at: http://www.nuget.org/packages/Slapper.AutoMapper/
72+
Slapper.AutoMapper is also available on NuGet available here: http://www.nuget.org/packages/Slapper.AutoMapper/
7473

7574
Now let the slapping commence! :)
7675

@@ -85,21 +84,21 @@ The following simple example maps a dictionary of property names and values to a
8584
```csharp
8685
public class Person
8786
{
88-
public int Id;
89-
public string FirstName;
90-
public string LastName;
87+
public int Id;
88+
public string FirstName;
89+
public string LastName;
9190
}
9291

9392
[Test]
9493
public void Can_Map_Matching_Field_Names_With_Ease()
9594
{
9695
// Arrange
9796
var dictionary = new Dictionary<string, object>
98-
{
99-
{ "Id", 1 },
100-
{ "FirstName", "Clark" },
101-
{ "LastName", "Kent" }
102-
};
97+
{
98+
{ "Id", 1 },
99+
{ "FirstName", "Clark" },
100+
{ "LastName", "Kent" }
101+
};
103102

104103
// Act
105104
var person = Slapper.AutoMapper.Map<Person>( dictionary );
@@ -121,9 +120,9 @@ When mapping dynamics use the `MapDynamic<T>()` method instead of the `Map<T>()`
121120
```csharp
122121
public class Person
123122
{
124-
public int Id;
125-
public string FirstName;
126-
public string LastName;
123+
public int Id;
124+
public string FirstName;
125+
public string LastName;
127126
}
128127

129128
[Test]
@@ -149,7 +148,7 @@ public void Can_Map_Matching_Field_Names_Using_Dynamic()
149148
### Mapping Nested Types Using a Dictionary ###
150149

151150
The following example maps a list of dictionaries of property names and values to a Customer class and using underscore notation ("_"),
152-
Slapper.AutoMapper properly populates the nested child types. This is really what I would consider this libraries secret sauce.
151+
Slapper.AutoMapper properly populates the nested child types. This is really what I would consider this library's secret sauce.
153152
You can just as easily use a list of dynamics which is demonstrated below too which is what is typically returned back from Micro ORMs.
154153

155154
As an example, the following SQL would return similar results to what is in the dictionaries in the example below ( Note the use of SQL aliases ).
@@ -159,90 +158,90 @@ SQL and the mapping to C# objects at the same time by use of SQL aliases.*
159158

160159
```sql
161160
SELECT c.CustomerId,
162-
c.FirstName,
163-
c.LastName,
164-
o.OrderId AS Orders_OrderId,
165-
o.OrderTotal AS Orders_OrderTotal,
166-
od.OrderDetailId AS Orders_OrderDetails_OrderId,
167-
od.OrderDetailId AS Orders_OrderDetails_OrderDetailId,
168-
od.OrderDetailTotal AS Orders_OrderDetails_OrderDetailTotal
161+
c.FirstName,
162+
c.LastName,
163+
o.OrderId AS Orders_OrderId,
164+
o.OrderTotal AS Orders_OrderTotal,
165+
od.OrderDetailId AS Orders_OrderDetails_OrderId,
166+
od.OrderDetailId AS Orders_OrderDetails_OrderDetailId,
167+
od.OrderDetailTotal AS Orders_OrderDetails_OrderDetailTotal
169168
FROM Customer c
170-
JOIN Order o ON c.CustomerId = o.CustomerId
171-
JOIN OrderDetail od ON o.OrderId = od.OrderId
169+
JOIN Order o ON c.CustomerId = o.CustomerId
170+
JOIN OrderDetail od ON o.OrderId = od.OrderId
172171
```
173172

174173
This example is indicative of the results you would commonly encounter when querying a database and joining on an Orders
175174
and OrderDetails table --you would get back duplicate results in some fields. Notice how the CustomerId in both dictionaries
176175
are the same. Because of Slapper.AutoMapper's default conventions, it will identify the CustomerId field as being the
177176
identifier ( or primary key so to speak ). This means that when it attempts to convert the second dictionary to a Customer
178-
object, it will see that it has already created a Customer object with an CustomerId of 1 and will simply re-use the previous
177+
object, it will see that it has already created a Customer object with a CustomerId of 1 and will simply re-use the previous
179178
instance resulting in only one Customer object being returned back. This is how Slapper.AutoMapper effectively groups results
180-
together and is the key to this libraries awesomeness.
179+
together and is the key to this library's awesomeness.
181180

182181

183182
```csharp
184183
public class Customer
185184
{
186-
public int CustomerId;
187-
public string FirstName;
188-
public string LastName;
189-
public IList<Order> Orders;
185+
public int CustomerId;
186+
public string FirstName;
187+
public string LastName;
188+
public IList<Order> Orders;
190189
}
191190

192191
public class Order
193192
{
194-
public int OrderId;
195-
public decimal OrderTotal;
196-
public IList<OrderDetail> OrderDetails;
193+
public int OrderId;
194+
public decimal OrderTotal;
195+
public IList<OrderDetail> OrderDetails;
197196
}
198197

199198
public class OrderDetail
200199
{
201-
public int OrderDetailId;
202-
public decimal OrderDetailTotal;
200+
public int OrderDetailId;
201+
public decimal OrderDetailTotal;
203202
}
204203

205204
[Test]
206205
public void I_Can_Map_Nested_Types_And_Resolve_Duplicate_Entries_Properly()
207206
{
208-
// Arrange
209-
var dictionary = new Dictionary<string, object>
210-
{
211-
{ "CustomerId", 1 },
212-
{ "FirstName", "Bob" },
213-
{ "LastName", "Smith" },
214-
{ "Orders_OrderId", 1 },
215-
{ "Orders_OrderTotal", 50.50m },
216-
{ "Orders_OrderDetails_OrderDetailId", 1 },
217-
{ "Orders_OrderDetails_OrderDetailTotal", 25.00m }
218-
};
219-
220-
var dictionary2 = new Dictionary<string, object>
221-
{
222-
{ "CustomerId", 1 },
223-
{ "FirstName", "Bob" },
224-
{ "LastName", "Smith" },
225-
{ "Orders_OrderId", 1 },
226-
{ "Orders_OrderTotal", 50.50m },
227-
{ "Orders_OrderDetails_OrderDetailId", 2 },
228-
{ "Orders_OrderDetails_OrderDetailTotal", 25.50m }
229-
};
230-
231-
var list = new List<IDictionary<string, object>> { dictionary, dictionary2 };
232-
233-
// Act
234-
var customers = Slapper.AutoMapper.Map<Customer>( list );
235-
236-
// Assert
207+
// Arrange
208+
var dictionary = new Dictionary<string, object>
209+
{
210+
{ "CustomerId", 1 },
211+
{ "FirstName", "Bob" },
212+
{ "LastName", "Smith" },
213+
{ "Orders_OrderId", 1 },
214+
{ "Orders_OrderTotal", 50.50m },
215+
{ "Orders_OrderDetails_OrderDetailId", 1 },
216+
{ "Orders_OrderDetails_OrderDetailTotal", 25.00m }
217+
};
218+
219+
var dictionary2 = new Dictionary<string, object>
220+
{
221+
{ "CustomerId", 1 },
222+
{ "FirstName", "Bob" },
223+
{ "LastName", "Smith" },
224+
{ "Orders_OrderId", 1 },
225+
{ "Orders_OrderTotal", 50.50m },
226+
{ "Orders_OrderDetails_OrderDetailId", 2 },
227+
{ "Orders_OrderDetails_OrderDetailTotal", 25.50m }
228+
};
229+
230+
var list = new List<IDictionary<string, object>> { dictionary, dictionary2 };
231+
232+
// Act
233+
var customers = Slapper.AutoMapper.Map<Customer>( list );
234+
235+
// Assert
237236
238-
// There should only be a single customer
239-
Assert.That( customers.Count() == 1 );
237+
// There should only be a single customer
238+
Assert.That( customers.Count() == 1 );
240239

241-
// There should only be a single Order
242-
Assert.That( customers.FirstOrDefault().Orders.Count == 1 );
240+
// There should only be a single Order
241+
Assert.That( customers.FirstOrDefault().Orders.Count == 1 );
243242

244-
// There should be two OrderDetails
245-
Assert.That( customers.FirstOrDefault().Orders.FirstOrDefault().OrderDetails.Count == 2 );
243+
// There should be two OrderDetails
244+
Assert.That( customers.FirstOrDefault().Orders.FirstOrDefault().OrderDetails.Count == 2 );
246245
}
247246

248247
[Test]
@@ -294,10 +293,10 @@ Auto mapping allows Slapper to figure out how to effectively group objects toget
294293
duplicate results. Now internally, no actual grouping is happening but this is the easiest way to conceptualize
295294
how it works.
296295

297-
*For the curious, the actual implementation relies upon an instance cache implemented as a Dictionary where the key is the all of
298-
the identifier's hashes summed together and the value being the instance.*
296+
*For the curious, the actual implementation relies upon an instance cache implemented as a Dictionary where the key is all of
297+
the identifier's hashes summed together and the value is the instance.*
299298

300-
A classes identifier(s) play an important role in the ability of the mapper to effectively group objects together. If no
299+
A class' identifier(s) play an important role in the ability of the mapper to effectively group objects together. If no
301300
identifiers are found, the mapper will still map the results to the requested type but there will be duplicates in the results.
302301

303302

@@ -331,13 +330,13 @@ The following example specifies two identifiers for the Customer object by using
331330
```csharp
332331
public class Customer
333332
{
334-
public int CustomerId;
333+
public int CustomerId;
335334

336-
public string CustomerType;
335+
public string CustomerType;
337336

338-
public string FirstName;
337+
public string FirstName;
339338

340-
public string LastName;
339+
public string LastName;
341340
}
342341

343342
Slapper.AutoMapper.Configuration.AddIdentifiers( typeof( Customer ), new List<string> { "CustomerId", "CustomerType" } );
@@ -347,7 +346,7 @@ Slapper.AutoMapper.Configuration.AddIdentifiers( typeof( Customer ), new List<st
347346

348347
Slapper.AutoMapper also supports attribute-based identifiers.
349348

350-
By default, the library uses it's own Id attribute that allows you to simply decorate the identifiers on your class with
349+
By default, the library uses its own Id attribute that allows you to simply decorate the identifiers on your class with
351350
a `[Slapper.AutoMapper.Id]` attribute.
352351

353352
If you wish to use your own attribute instead of the default one, just set the Type to use on the following field:
@@ -361,15 +360,15 @@ The following example specifies two identifiers for the Customer object:
361360
```csharp
362361
public class Customer
363362
{
364-
[Slapper.AutoMapper.Id]
365-
public int CustomerId;
363+
[Slapper.AutoMapper.Id]
364+
public int CustomerId;
366365

367-
[Slapper.AutoMapper.Id]
368-
public string CustomerType;
366+
[Slapper.AutoMapper.Id]
367+
public string CustomerType;
369368

370-
public string FirstName;
369+
public string FirstName;
371370

372-
public string LastName;
371+
public string LastName;
373372
}
374373
````
375374

@@ -379,20 +378,20 @@ Usage - Caching
379378
#### Caching Explained ####
380379

381380
Slapper.AutoMapper internally maintains a cache of every object it creates, referred to as the instance cache.
382-
This cache plays an important role in Slapper's ability to easily lookup existing objects and ultimately assists
381+
This cache plays an important role in Slapper's ability to easily look up existing objects and ultimately assists
383382
in the ability for Slapper.AutoMapper to populate complex nested types.
384383

385384
Slapper.AutoMapper itself never removes an instance from this cache, so if you tell it to create 50,000 objects,
386-
then there are going to be 50,000 objects in the cache for the lifetime of the current thread or Http context.
385+
then there are going to be 50,000 objects in the cache for the lifetime of the current thread or HttpContext.
387386

388387
The instance cache exists for the lifetime of the current thread and each of your application's threads will
389-
get it's own unique cache making use of this library thread safe.
388+
get its own unique cache making use of this library thread safe.
390389

391390
#### Cache Backing Store ####
392391

393-
The instance cache backing store will either make use of the HttpContext if one exists or the CallContext of the
394-
executing thread. The library makes use of reflection in order to persist the cache in the HttpContext when
395-
neccessary so that the library does not require a dependency on the System.Web library.
392+
The instance cache backing store will either use the HttpContext if one exists or the CallContext of the
393+
executing thread. The library uses reflection to persist the cache in the HttpContext when
394+
necessary so that the library does not require a dependency on the System.Web library.
396395

397396
#### Clearing the Cache ###
398397

0 commit comments

Comments
 (0)