1

i am using generic repository to develop my own BLL with entity framework.Generic Repository But all generic repository doesn't have inner join. Lokk below :

 public interface IRepository
{
    IQueryable<T> List<T>() where T : class;
    T Get<T>(int id) where T : class;
    void Create<T>(T entityTOCreate) where T : class;
    void Edit<T>(T entityToEdit) where T : class;
    void Delete<T>(T entityToDelete) where T : class;
}

How to convert above code to :

 public interface IRepository
{
    IQueryable<T> List<T>() where T : class;
    T Get<T>(int id) where T : class;
    void Create<T>(T entityTOCreate) where T : class;
    void Edit<T>(T entityToEdit) where T : class;
    void Delete<T>(T entityToDelete) where T : class;
void InnerJoin<T>(T entityName, TNew entityName2) where T : class, where TNew : class;
}

or i think that we can use Fluent interfacve pattern like that:

    public List<MyWorker> ListByID( int ID)
{
    using (var Ctx = new DomainRepository<Worker>(new ProposalsEntities()))
         return Ctx.Find<Worker>(q => q.ID== ID).ToList().InnerJoin(XEntity,x=>x.ID=q.ID).InnerJoin(YEntity,y=>y.ID=q.ID);
}

Yuo can give another advise to achive this fantastical question. How can i write above join code in Generic Repository?

Penguen
  • 16,836
  • 42
  • 130
  • 205
  • You should go back and accept correct answers to your previous questions. 50% is abysmal, especially considering the first handful of your question I checked had objectively correct answers (by your own admission in comments even). – quentin-starin Feb 05 '12 at 02:20
  • if answer is correct , i will check it! Dont involve question without answer or advise. – Penguen Feb 05 '12 at 10:30
  • 1
    You have many questions with correct answers that are not accepted: http://stackoverflow.com/questions/820155/how-do-i-convert-hhmmss-to-hhmm-in-sql-server http://stackoverflow.com/questions/510778/add-or-sum-of-hours-like-1330000020-133020-but-how http://stackoverflow.com/questions/2272755/how-can-i-learn-my-client-ip-with-net http://stackoverflow.com/questions/785799/how-can-i-export-a-gridview-datasource-to-a-datatable-or-dataset http://stackoverflow.com/questions/3069143/how-can-i-use-listdictionary http://stackoverflow.com/questions/985535/stack-overflow-error-on-color-changer-function – quentin-starin Feb 05 '12 at 19:10

2 Answers2

4

This is not possible with your generic repository. It is not a good idea to abstract the relational model of the database to much. It prevents you from using database-specific features like ... joins.

You can fix your repository by adding an additonal method:

IQueryable<T> Query<T>() { return dataContext.GetTable<T>(); }

(This example is for Linq 2 Sql).

You need to be able to provide callerd with a composable query, not a list. Callers can then write:

from w in repo.Query<Worker>()
join e in repo.Query<XEntity>() on ...

If the comment is permitted: The generic repository pattern is not a good idea. It does little help but does great damage.

Either directly use the DataContext/EntityContext/Session directly or use specialized repositories.

usr
  • 168,620
  • 35
  • 240
  • 369
  • can we do that with string values : void InnerJoin(T entityName, string entityName2) where T : class – Penguen Feb 05 '12 at 10:28
0

A repository shouldn't expose join functionality. The domain object that the repository provides might be composed of data that's persisted in multiple tables but the domain layer shouldn't be aware of that fact or, more generally, of the format the data takes in the persistence layer. Some data sources may not even be able to provide join functionality.

Yuli Bonner
  • 1,189
  • 8
  • 12