+3  A: 

Not a dodge to your question, but it's ultimately up to you to decide how your repository would work.

The high level premise is that your controller would point to some repository interface, say IRepository<T> where T : IProduct. The implementation of which could do any number of things---load up your whole database from disk and store in memory and then parse LINQ expressions to return stuff. Or it could just return a fixed set of dummy data for testing purposes. Because you're banging away on an repository interface, then you could have any number of concrete implementations.

Now, if you're looking for a critique of Rob's specific implementation, I'm not sure that's germaine to SO.

bakasan
An aside--re-reading your post, I'd strongly suggest you not follow a pattern where you load only subsets of your object's properties based upon context. It's a fake lazy loading and it's extremely error prone as that object gets passed around to different contexts. If you must, then consider a base and derived class, one w/ a smaller subset of properties while the other has the full details.
bakasan
Thank you for this suggestion and that what i were going to do but actually I'm trying to find something easier ;) So Thanks again.
Wahid Bitar
+2  A: 

While it's possible to populate part of an object based on a query of a subset of the columns for that object using a query (which has nothing to do with the repository pattern), that's not how things are "normally" done.

If you want to return a subset of an object, you generally create a new class with just that subset of properties. This is often (in the MVC world view) referred to as a View Model class. Then, you use a projection query to fill that new class.

You can do all of that whether you are using the repository pattern or not. I would argue there is no conflicting overlap between the two concepts.

Michael Maddox
OK i made new class with just needed properties but I'll fill this class from my customized repository which will load all data from L2S classes and fill them to my Model Class then I'll get just the property i want. this what happens in Rob's Repository pattern.But i i depend on L2S classes i could select just what i want but that will lead me to break my layers down.
Wahid Bitar
+2  A: 

DeferringTheLoad

Remember that IQueryable defers all the loading up to the last responsible moment. you probably won't have to load all the data using the LINQ operators to get the data you want. ; )

EDIT: respecting the dependency in domain classes in views, I will say NO. use a ViewModel pattern for this. it's more maintainable, you could use AutoMapper to avoid the mapping problems, and are very flexible in composite views scenarios : )

UPDATE: According to the new question...The answer is yes you can. just as Rob Conery says, use projection ; ):

var query = from p in DataContext.Persons}
select new Persons
{
  firstname = p.firstname,
  lastname = p.lastname
});
SDReyes
+1  A: 

I decided to use sort of programming against Interfaces

I'll build my Model based on Interfaces and then I'll expand L2S classes to implement these interfaces. Then in my repository I'll return list of Interface rather than list of my model class.

E.G.

this is the Interface:

public interface IProduct
{
    int ID { get; set; }
    string Name { get; set; }
    .
    ..
    ...
    string Notes { get; set; }
}

then my model will look like this :

public class Product:IProduct
{

    #region IProduct Members

    public int ID { get; set; }
    public string Name{ get; set; } 
    .
    ..
    ...
    public string Notes { get; set; }
}

And because the "Linq To Sql" classes are partials I'll expand them to implement this Interface:

public partial class Product:IProduct
{

    #region IProduct Members
    .
    ..
    ...
    // if there is any deference between L2S class and my Model class should considered here
    #endregion
}

then my Repository will return IQueryable<IProduct> rather than IQueryable<Product> like this :

public class ProductRepository
{
    public IQueryable<IProduct> GetAllProducts()
    {
        ...
        ....
        var list = from p in db.Products
                   select (IProduct)p;
        return list;
    }
}

So please tell me your opinion is this will be fine ?? Or you prefer something better ??.

Wahid Bitar
+13  A: 

Yes, it's possible if you're using Linq to SQL. All you need to do is use Projection to pull out the data you want into an object of your choosing. You don't need all this decoration with interfaces and whatnot - if you use a model specific to a view (which it sounds like you need) - create a ViewModel class.

Let's call it ProductSummaryView:

public class ProductSummaryView{
   public string Name {get;set;}
   public decimal Price {get;set;}

}

Now load it from the repo:

var products= from p in _repository.GetAllProducts
              where p.Price > 100
              select new ProductSummaryView {
                  Name=p.ProductName,
                  Price=p.Price

              }

This will pull all products where the price > 100 and return an IQueryable. In addition, since you're only asking for 2 columns, only two columns will be specified in the SQL call.

Rob Conery
Thanks very much Mr.Rob for your answer. And just more curiosity this ViewModel class should be in my Model class library and use L2S directly to fill it not using my Repository. Am I right ??. Thanks very much again
Wahid Bitar
Your ViewModel should be with the UI bits - not the model. This is an "adaptive" pattern to help the web app cope with the model data - you want to keep it where it's used.
Rob Conery