Adding generic method to return the DataSource2 from LinqMetaData

Posts   
 
    
mihies avatar
mihies
User
Posts: 800
Joined: 29-Jan-2006
# Posted on: 27-Sep-2013 18:31:53   

I could use this notation sometimes

class LinqMetaData {
...
public DataSource2<TEntity> Entities<TEntity>
{
   get { /* massive if here */ }
}
...
}

Well, similar to GetQueryableForEntity but using generic type.

It comes handy when you deal with generic methods...

I know I could modify the code generation template or add mine but this feature could be useful to broad public. What do you think?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 27-Sep-2013 23:27:43   

You mean, you want to generate a massive if in that method? I have to check, but isn't it that the if isn't necessary because the type is known?

Frans Bouma | Lead developer LLBLGen Pro
mihies avatar
mihies
User
Posts: 800
Joined: 29-Jan-2006
# Posted on: 28-Sep-2013 11:19:53   

Yep, you already have a massive if in there: GetQueryableForEntity.

Imagine you have this:

interface IMyEntity {
 int Id { get; }
}

a method

EntityCollection2<TMyEntity> DoSomethingWith<TMyEntity>
  where TMyEntity: CommonEntityBase, IMyEntity
()
{
...
}

and you want to, let's say, do a query that returns TMyEntity collection based on its Id, like

from e in new LinqMetaData().Entities<TMyEntity>() where e.Id == SomeId select e;
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 30-Sep-2013 10:53:39   

Add a partial class to LinqMetaData and add:


public DataSource2<TEntity> Entities()
    where TEntity : class
{
    return new DataSource2<TEntity>(_adapterToUse, new ElementCreator(), _customFunctionMappings, _contextToUse); 
}

that should work, I think?

Frans Bouma | Lead developer LLBLGen Pro
mihies avatar
mihies
User
Posts: 800
Joined: 29-Jan-2006
# Posted on: 01-Oct-2013 10:11:28   

Excellent, much better to the giant if statement, that works with a slight modification - added generic parameter and prefix Get to method name simple_smile

public DataSource2<TEntity> GetEntities<TEntity>()
    where TEntity : class
{
    return new DataSource2<TEntity>(_adapterToUse, new ElementCreator(), _customFunctionMappings, _contextToUse);
}

Will you consider adding such method to the default template. Not a big deal though.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 03-Oct-2013 12:19:54   

Sure, I'll add it in v4.1, it's an easy helper.

Frans Bouma | Lead developer LLBLGen Pro