Skip to content

Get Selectors

jtreuting edited this page Apr 26, 2013 · 2 revisions

Get Selectors

What are Get Selectors?

You can think of them as the Expression that you would use in a LINQ .Select() method. This allows you to limited the information that is sent over the network when a database call is made, or to map your Entity to a different object (like a ViewModel perhaps), or other things that are escaping me at the moment :)

The selector is an optional parameter that is available when calling Get, Find, GetAll or FindAll.

Usage

public class Order
{
    public int OrderId { get; set; }
    public string Name { get; set; }
    public double Total { get; set; }
    public DateTime OrderDate { get; set; }
}

var repo = new InMemoryRepository<Order>();

// bring back the entire normal entity
var order = repo.Get(1);

// in this case we only need the order name
var orderName = repo.Get(1, x => x.Name);

// we can also bring back an anonymous type if needed
var anonymousType = repo.Get(1, x => new { Name = x.Name, IsExpensiveOrder = x.Total > 100.0 });

// or we can map it to a specific type we have defined like a ViewModel
var viewModel = repo.Get(1, x => new OrderViewModel() {Name = x.Name, IsExpensiveOrder = x.Total > 100.0});

// We have the same options with the GetAll, Find and FindAll as well
orderName = repo.Find(x => x.OrderId == 2, x => x.Name);
var anonymousTypes = repo.GetAll(x => new { Name = x.Name, IsExpensiveOrder = x.Total > 100.0 });
var viewModels = repo.FindAll(x => x.OrderId < 5, x => new OrderViewModel() { Name = x.Name, IsExpensiveOrder = x.Total > 100.0 });

Hopefully the code examples are self explanatory.

Clone this wiki locally