1) What do you do if you want to switch out ORMs, you would have
specific ORM code in your application if you do not contain it in a
repository.
I haven't yet been in a position where company all of a sudden decided to switch data access technology. If this does happen, some work will be required. I tend to abstract data access operations through interfaces. Repository is one way to solve this.
I would then have a different assembly for concrete implementation of my data access layer. For example, I might have:
Company.Product.Data
and Company.Product.Data.EntityFramework
assemblies. First assembly would be used purely for interfaces, when another would be a concrete implementation of Entity Framework's data access logic.
2) Is the repository pattern still valid when not using an ORM and you
are using ADO.net for data access and populating object data yourself
?
I think that it's up to you to decide which pattern is valid or not. I've used a repository pattern in the presentation layer. One thing to keep in mind is that people like to throw responsibilities into repositories. Before you know it, your repository class will be dancing, singing and doing all sort of things. You want to avoid this.
I've seen a repository class that started off by having GetAll, GetById, Update and Delete responsibilities, which is fine. By the time the project was complete, that same class had dozens of methods (responsibilities) which should have never been there. For example GetByForename, GetBySurname, UpdateWithExclusions and all kinds of crazy stuff.
This is where queries and commands come into play.
3) If you use an ORM but not the repository pattern where do you keep
commonly used queries. Would it be wise to represent each query as a
class and have some sort of query factory to create instances ?
I think it's a very good idea to have use queries and commands instead of repositories. I do the following:
Define interface for a query. This will help you unit test. E.g. public interface IGetProductsByCategoryQuery { ... }
Define concrete implementation for a query. You will be able to inject these through IoC framework of your choice. E.g. public class GetProductsByCategoryQuery : IGetProductsByCategoryQuery
Now instead of polluting repository with dozens of responsibilities, I simply group my queries into namespaces. For example, an interface for the above query may live in: Company.SolutionName.Products.Queries
and the implementation may live in Company.SolutionName.Products.Queries.Implementation
When it comes to updating or removing data, I use command pattern in the same manner.
Some might disagree and say that before the project is complete you will have dozens of classes and namespaces. Yes you will. In my mind it's a good thing as you can browse through the solution in IDE of your choice and instantly see what kind of responsibilities certain component has. If you have decided to use a repository pattern instead, you will have to look inside each repository class trying to work out its responsibilities.