本文整理汇总了C#中InMemoryRepository.Get方法的典型用法代码示例。如果您正苦于以下问题:C# InMemoryRepository.Get方法的具体用法?C# InMemoryRepository.Get怎么用?C# InMemoryRepository.Get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类InMemoryRepository
的用法示例。
在下文中一共展示了InMemoryRepository.Get方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SharpRepository_Supports_Basic_Crud_Operations
public void SharpRepository_Supports_Basic_Crud_Operations()
{
// Declare your generic InMemory Repository.
// Check out HowToAbstractAwayTheGenericRepository.cs for cleaner ways to new up a repo.
var repo = new InMemoryRepository<Order, int>();
// Create
var create = new Order { Name = "Big sale" };
repo.Add(create);
const int expectedOrderId = 1;
create.OrderId.ShouldEqual(expectedOrderId);
// Read
var read = repo.Get(expectedOrderId);
read.Name.ShouldEqual(create.Name);
// Update
read.Name = "Really big sale";
repo.Update(read);
var update = repo.Get(expectedOrderId);
update.OrderId.ShouldEqual(expectedOrderId);
update.Name.ShouldEqual(read.Name);
// Delete
repo.Delete(update);
var delete = repo.Get(expectedOrderId);
delete.ShouldBeNull();
}
示例2: Repository_Supports_Selectors
public void Repository_Supports_Selectors()
{
var repo = new InMemoryRepository<Order>();
// let's add a couple of orders to work with
repo.Add(new Order()
{
Name = "Order 1",
Total = 120.00,
OrderDate = new DateTime(2013, 4, 26)
});
repo.Add(new Order()
{
Name = "Order 2",
Total = 80.00,
OrderDate = new DateTime(2013, 4, 24)
});
// normal Get method
var order = repo.Get(1);
order.OrderId.ShouldEqual(1);
// in this case we only need the order name
var orderName = repo.Get(1, x => x.Name);
orderName.ShouldEqual("Order 1");
// 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 });
anonymousType.IsExpensiveOrder.ShouldBeTrue();
// 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});
viewModel.IsExpensiveOrder.ShouldBeTrue();
// We have the same options with the GetAll, Find and FindAll as well
orderName = repo.Find(x => x.OrderId == 2, x => x.Name);
orderName.ShouldEqual("Order 2");
// we can also bring back an anonymous type if needed
var anonymousTypes = repo.GetAll(x => new { Name = x.Name, IsExpensiveOrder = x.Total > 100.0 }).ToList();
anonymousTypes.Count.ShouldEqual(2);
anonymousTypes.First().Name.ShouldEqual("Order 1");
anonymousTypes.First().IsExpensiveOrder.ShouldBeTrue();
anonymousTypes.Last().Name.ShouldEqual("Order 2");
anonymousTypes.Last().IsExpensiveOrder.ShouldBeFalse();
// or we can map it to a specific type we have defined like a ViewModel
var viewModels = repo.FindAll(x => x.OrderId < 5, x => new OrderViewModel() { Name = x.Name, IsExpensiveOrder = x.Total > 100.0 }).ToList();
viewModels.Count.ShouldEqual(2);
viewModels.First().Name.ShouldEqual("Order 1");
viewModels.First().IsExpensiveOrder.ShouldBeTrue();
viewModels.Last().Name.ShouldEqual("Order 2");
viewModels.Last().IsExpensiveOrder.ShouldBeFalse();
}
示例3: Cache_Should_Timeout
public void Cache_Should_Timeout()
{
var repository = new InMemoryRepository<Contact, int>(new TimeoutCachingStrategy<Contact, int>(2) { CachePrefix = "#RepoTimeoutCache" });
repository.Add(new Contact() { Name = "Test User" });
repository.Get(1);
repository.CacheUsed.ShouldBeTrue();
Thread.Sleep(5000);
repository.Get(1);
repository.CacheUsed.ShouldBeFalse();
}
示例4: Second_Get_Call_Should_Get_New_Item_From_Cache
public void Second_Get_Call_Should_Get_New_Item_From_Cache()
{
var repository = new InMemoryRepository<Contact, int>(new TimeoutCachingStrategy<Contact, int>(10) { CachePrefix = "#RepoTimeoutCache"});
repository.Add(new Contact() { Name = "Test User"});
var item = repository.Get(1); // after this call it's in cache
item.Name.ShouldEqual("Test User");
repository.Update(new Contact() { ContactId = 1, Name = "Test User EDITED" }); // does update cache
var item2 = repository.Get(1); // should get from cache since the timeout hasn't happened
item2.Name.ShouldEqual("Test User EDITED");
}
示例5: CanSaveAndGetAnEntity
public void CanSaveAndGetAnEntity()
{
IRepository<SomeEntity> repo = new InMemoryRepository<SomeEntity>();
SomeEntity entity = new SomeEntity("a");
var unstoredEntity = repo.Get("a");
Assert.IsNull(unstoredEntity);
repo.Save(entity);
var storedEntity = repo.Get("a");
Assert.IsNotNull(storedEntity);
Assert.AreSame(entity, storedEntity);
}
示例6: CompoundKeyRepository_Should_Work
public void CompoundKeyRepository_Should_Work()
{
var repository = new InMemoryRepository<CompoundKeyItemInts, int, int>();
repository.Add(new CompoundKeyItemInts { SomeId = 1, AnotherId = 1, Title = "1-1"});
repository.Add(new CompoundKeyItemInts { SomeId = 1, AnotherId = 2, Title = "1-2"});
repository.Add(new CompoundKeyItemInts { SomeId = 1, AnotherId = 3, Title = "1-3"});
repository.Add(new CompoundKeyItemInts { SomeId = 2, AnotherId = 1, Title = "2-1"});
repository.Add(new CompoundKeyItemInts { SomeId = 2, AnotherId = 2, Title = "2-2"});
repository.Add(new CompoundKeyItemInts { SomeId = 2, AnotherId = 3, Title = "2-3"});
repository.Get(1, 1).Title.ShouldEqual("1-1");
repository.Get(2, 1).Title.ShouldEqual("2-1");
repository.Get(1, 2).Title.ShouldEqual("1-2");
repository.FindAll(x => x.SomeId == 1).Count().ShouldEqual(3);
}
示例7: TripleCompoundKeyRepository_Should_Work
public void TripleCompoundKeyRepository_Should_Work()
{
var repository = new InMemoryRepository<TripleCompoundKeyItemInts, int, int, int>();
repository.Add(new TripleCompoundKeyItemInts { SomeId = 1, AnotherId = 1, LastId = 10, Title = "1-1-10" });
repository.Add(new TripleCompoundKeyItemInts { SomeId = 1, AnotherId = 2, LastId = 11, Title = "1-2-11" });
repository.Add(new TripleCompoundKeyItemInts { SomeId = 1, AnotherId = 3, LastId = 10, Title = "1-3-10" });
repository.Add(new TripleCompoundKeyItemInts { SomeId = 2, AnotherId = 1, LastId = 11, Title = "2-1-11" });
repository.Add(new TripleCompoundKeyItemInts { SomeId = 2, AnotherId = 2, LastId = 10, Title = "2-2-10" });
repository.Add(new TripleCompoundKeyItemInts { SomeId = 2, AnotherId = 3, LastId = 11, Title = "2-3-11" });
repository.Get(1, 1, 10).Title.ShouldEqual("1-1-10");
repository.Get(2, 1, 11).Title.ShouldEqual("2-1-11");
repository.Get(1, 2, 11).Title.ShouldEqual("1-2-11");
repository.FindAll(x => x.LastId == 11).Count().ShouldEqual(3);
}
示例8: When_executing_then_an_item_is_added
public void When_executing_then_an_item_is_added()
{
// Arrange
var repository = new InMemoryRepository();
var command = new AddSearchableItemCommand(repository);
var request = new Request<string> {Parameter = "My new item name"};
// Act
var response = command.Execute(request);
// Assert
Assert.That(response.Result.Title,Is.EqualTo(request.Parameter));
Assert.That(response.Result.Id,Is.Not.EqualTo(""));
var savedItem = repository.Get<SearchableItem>(response.Result.Id);
Assert.That(savedItem,Is.Not.Null);
Assert.That(savedItem.Title,Is.EqualTo(request.Parameter));
}
示例9: Get_With_Selector_Should_Not_Use_Cache_If_Entity_Updated
public void Get_With_Selector_Should_Not_Use_Cache_If_Entity_Updated()
{
var repository = new InMemoryRepository<Contact, string>(new StandardCachingStrategy<Contact, string>()); // by default uses InMemoryCache
for (var i = 1; i < 3; i++)
{
repository.Add(new Contact { ContactId = i.ToString(), Name = "Contact " + i, ContactTypeId = 1 });
}
const string contactId = "1";
var contactName = repository.Get(contactId, c => c.Name);
contactName.ShouldEqual("Contact 1");
var contact = repository.Get(contactId);
contact.Name = "Contact 1 - EDITED";
contactName = repository.Get(contactId, c => c.Name);
contactName.ShouldEqual("Contact 1 - EDITED");
}
示例10: ExecuteGet_With_Selector_Should_Use_Cache_After_First_Call
public void ExecuteGet_With_Selector_Should_Use_Cache_After_First_Call()
{
var repos = new InMemoryRepository<Contact>(new StandardCachingStrategy<Contact>());
repos.Add(new Contact { Name = "Test1" });
repos.Add(new Contact { Name = "Test2" });
var item = repos.Get(1, x => x.Name);
repos.CacheUsed.ShouldBeTrue();
item.ShouldNotBeNull();
}