iqueryable

NHibernate testing, mocking ISession

雨燕双飞 提交于 2019-12-05 08:22:08
I am using NHibernate and Rhinomocks and having trouble testing what I want. I would like to test the following repository method without hitting the database (where _session is injected into the repository as ISession): public class Repository : IRepository { (... code snipped for brevity ...) public T FindBy<T>(Expression<Func<T, bool>> where) { return _session.Linq<T>().Where(where).FirstOrDefault(); } } My initial approach is to mock ISession, and return an IQueryable stub (hand coded) when Linq is called. I have a IList of Customer objects I would like to query in memeory to test my Linq

using the OData Queryable with a repository?

▼魔方 西西 提交于 2019-12-05 06:14:51
问题 I have this repository method: public IQueryable<TModel> GetAll() { using (var context = new DatabaseContext()) { return context.Set<TModel>().AsQueryable(); } } where TModel is an artist-model.. anyway And then I have this action in my controller: // GET api/artist [Queryable] public IQueryable<ArtistModel> Get() { return _repo.GetAll().AsQueryable(); } Now.. if I would change the repository method to return a List and also add .ToList for my result.. then this would work great. But then no

What is a good way to to indicate an IEnumerable is “slow” or “fast”?

▼魔方 西西 提交于 2019-12-05 05:37:17
The answer to What is the expected performance of IEnumerable? says there's no way to know anyting about the performance of iterating an arbitrary IEnumerable. Each iteration could hit a database or make a web serivce call; or it might just return the next item in an array/list. Given that, is there a good way to indicate "this is fast"? For example, with T[] or List<T> instead of IEnumerable<T> I know that going from T[i] to T[i+1] will be quick. (Of course, forcing the enumeration to return a list/array could create other performance concerns. List<T> also exposes editable semantics.)

Adding new items dynamically to IQueryable hard-coded fake repository

北城余情 提交于 2019-12-05 05:35:15
Building an application, before using a real database, just to get things work I can first use a hard-coded list as a fake, in-memory repository: public class FakeProductsRepository { private static IQueryable<Product> fakeProducts = new List<Product> { new Product{ ProductID = "xxx", Description = "xxx", Price = 1000}, new Product{ ProductID = "yyy", Description = "xxx", Price = 2000}, new Product{ ProductID = "zzz", Description = "xxx", Price = 3000} }.AsQueryable(); public IQueryable<Product> Products { get { return fakeProducts; } } } How to add a method to this class for adding new, not

IQueryable problems using WCF

僤鯓⒐⒋嵵緔 提交于 2019-12-05 04:52:04
I have a quite simple WCF service method which returns an IQueryable, just for testing. Perhaps I got something wrong when trying to understand what IQueryable is designed for. I clearly plan to use this with the IQueryable provider of NHibernate later. But first I ran into some sort of serialization problems (at least I think it might be the problem) whenever using a WCF method returning an IQueryable. It doesn't even work for a simple string. Here's my code: public IQueryable<string> GetEquipmentConfigurations() { var returnValue = new List<string>(); returnValue.Add("test"); return

How to maintain LINQ deferred execution?

你。 提交于 2019-12-05 03:30:33
Suppose I have an IQueryable<T> expression that I'd like to encapsulate the definition of, store it and reuse it or embed it in a larger query later. For example: IQueryable<Foo> myQuery = from foo in blah.Foos where foo.Bar == bar select foo; Now I believe that I can just keep that myQuery object around and use it like I described. But some things I'm not sure about: How best to parameterize it? Initially I've defined this in a method and then returned the IQueryable<T> as the result of the method. This way I can define blah and bar as method arguments and I guess it just creates a new

What would you put into the unit test of a repository class (data access layer)?

别等时光非礼了梦想. 提交于 2019-12-05 01:30:18
问题 I'd like to write a unit test for my data access layer to make sure that everything works allright in it. The question is, what kind of things should I put into the tests? The DAL is a static Repository class which hides the underlying layer (Fluent NHibernate) and exposes stuff to the public through an IQueryable . I thought about CRUD (Create/Retrieve/Update/Delete) operations Transactions Is there anything else about a DAL that is worth testing? Thanks in advance for your answers! 回答1:

How to invoke Expression<Func<Entity, bool>> against a collection

て烟熏妆下的殇ゞ 提交于 2019-12-05 00:17:33
I have an interface that defines a repository from the Repository pattern: interface IRepository { List<Customer> GetAllCustomers(Expression<Func<Customer, bool>> expression); } I've implemented it against Entity Framework: class EntityFrameworkRepository { public List<Customer> GetAllCustomers(Expression<Func<Customer, bool>> expression) { return DBContext.Customers.Where(expression).ToList(); } } That seems to work well, it allows me to do something like: var customers = entityFrameworkRepository.Where( customer => String.IsNullOrEmpty(customer.PhoneNumber) ); Now I'd like to have an

Determine whether an IQueryable<T> has been ordered or not

蓝咒 提交于 2019-12-04 22:20:46
Is there a way to know if an IQueryable<T> has been ordered (using OrderBy or OrderbyDescending )? So I know whether to call OrderBy or ThenBy on the collection. IQueryable<Contact> contacts = Database.GetContacts(); I tried contacts is IOrderedQueryable<Contact> , but it's always true. Edit : I just changed my example, the previous one wasn't really showing my point. Assume that GetContacts uses Entity Framework and simply returns all the records of a table. Later on, I apply several functions to contacts , I have no knowledge of what those functions do. They can sort or filter the IQueryable

Performing part of a IQueryable query and deferring the rest to Linq for Objects

我的梦境 提交于 2019-12-04 19:44:41
问题 I have a Linq provider that sucessfully goes and gets data from my chosen datasource, but what I would like to do now that I have my filtered resultset, is allow Linq to Objects to process the rest of the Expression tree (for things like Joins, projection etc) My thought was that I could just replace the expression constant that contains my IQueryProvider with the result-sets IEnumerable via an ExpressionVisitor and then return that new expression. Also return the IEnumerable's provider from