Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to use a generic with Entity Framework

Let me start with, I am not sure if this is possible. I am learning generics and I have several repositories in my app. I am trying to make an Interface that takes a generic type and converts it to something that all of the repositories can inherit from. Now on to my question.

public interface IRepository<T>
{
    IEnumerable<T> FindAll();
    IEnumerable<T> FindById(int id);
    IEnumerable<T> FindBy<A>(A type);
}

Is it possible to use a generic to determine what to find by?

public IEnumerable<SomeClass> FindBy<A>(A type)
{
    return _context.Set<SomeClass>().Where(x => x. == type); // I was hoping to do x.type and it would use the same variable to search.
}

To clarify a little better I was considering to be a string, int or whatever type I wanted to search for. What I am hoping for is I can say x.something where the something is equal to the variable passed in.

I can set any repository to my dbcontext using the

public IDbSet<TEntity> Set<TEntity>() where TEntity : class
{
    return base.Set<TEntity>();
}

Any Suggestions?

like image 775
Robert Avatar asked Dec 19 '12 13:12

Robert


People also ask

How do I create a generic entity?

Create an instance directly by supplying the generic type information with the entity. For example the following code shows how to create a response containing the result of a method invoked via reflection: Method method = ...; GenericEntity<Object> entity = new GenericEntity<Object>( method. invoke(...), method.

Where do we use generic in C#?

Generics allow you to define the specification of the data type of programming elements in a class or a method, until it is actually used in the program. In other words, generics allow you to write a class or method that can work with any data type.

Is generic code faster or slower than non generic code?

cmd/compile: generic functions are significantly slower than identical non-generic functions in some cases #50182.

Can we have generic method in non generic class in C#?

Yes, There are two level where you can apply generic type . You can apply generic type on Method level as well as Class level (both are optional).


1 Answers

If you use Expression<Func<T, bool>> instead of A like this:

public interface IRepository<T>
{
    ... // other methods
    IEnumerable<T> FindBy(Expression<Func<T, bool>> predicate);
}

You can query the type using linq and specify the query in the code which calls the repository class.

public IEnumerable<SomeClass> FindBy(Expression<Func<SomeClass, bool>> predicate)
{
    return _context.Set<SomeClass>().Where(predicate);
}

And call it like this:

var results = repository.FindBy(x => x.Name == "Foo");

And given that it's a generic expression, you don't have to implement it in each repository, you can have it in the generic base repository.

public IEnumerable<T> FindBy(Expression<Func<T, bool>> predicate)
{
    return _context.Set<T>().Where(predicate);
}
like image 65
Trevor Pilley Avatar answered Oct 21 '22 21:10

Trevor Pilley