Answer the question
In order to leave comments, you need to log in
LINQ to Entity Framework - the base class for working with the database? (C#)?
Good day. Most of the time I wrote in PHP, but now it became necessary to deal with ASP.NET MVC
. And the problem I have is the following.
Working with EF, we can easily create such methods in the class:
public class SiteContext : DbContext
{
public DbSet<Problem> Problems { get; set; }
}
public class BaseDBProxy
{
protected SiteContext db = new SiteContext();
}
public class ProblemsProxy : BaseDBProxy
{
public Problem GetById(int id)
{
return db.Problems.Where(e => e.Id == id).FirstOrDefault<Problem>();
}
}
public class BaseDBProxy
{
protected SiteContext db = new SiteContext();
public АВТОПОДСТАНОВКА_НУЖНОЙ_МОДЕЛИ GetById(int id)
{
return db.АВТОПОДСТАНОВКА_НУЖНОЙ_МОДЕЛИ.Where(e => e.Id == id).FirstOrDefault<[АВТОПОДСТАНОВКА_НУЖНОЙ_МОДЕЛИ]>();
}
}
Answer the question
In order to leave comments, you need to log in
Haven't used EF for a long time, but I think it should work:
public class BaseDBProxy
{
protected SiteContext db = new SiteContext();
public TEntity GetById<TEntity>(int id)
{
return db.Set<TEntity>.Where(e => e.Id == id).FirstOrDefault();
}
}
public abstract class RepositoryBase<TEntity> where TEntity : class
{
private Set<TEntity> _set;
public RepositoryBase(DbContext context)
{
_set = context.Set<TEntity>();
}
public TEntity GetById(int id)
{
return _set.Where(e => e.Id == id).First();
}
public TEntity TryGetById(int id)
{
return _set.Where(e => e.Id == id).FirstOrDefault();
}
}
Paulskit correctly said that the author is trying to write the Repository pattern. And Lam correctly wrote how to make a GenericRepository.
An example of a generalized repository, by the way, is here www.asp.net/mvc/tutorials/getting-started-with-ef-...
I used it in my first project. In fact, personally in my understanding, this is not very good.
Now your method returns an object by its ID. I foresee that the next method would be to get a group of objects and return an IQueryable. This approach unwittingly forces you to write the code for selecting objects somewhere (in the controller, in the service layer), but not in the repository, which leads to confusion. In my opinion, it is better to make not a generalized repository, but a unique repository for each entity necessary for the program to work.
Let's say you have a BlogPost class. You want to be able to load this object by ID, load all of these objects into memory in general, load only part of these objects into memory, load objects with a certain condition, etc. To do this, we can create a BlogPostRepository and write these methods in it. Then your program will become more logical and understandable. When working with blogs, you will not need to each declare a generic repository and build requests like
var repository = new GenericRepository<BlogPost>();
var blogs = repository.GetAll().Skip(100).Take(10).OrderByDescending(c => c.Created);
var repository = new BlogPostRepository();
var blogs = repository.GetTenTopBlogs();
It's not clear what you are trying to achieve. How should the program know that by ID you want to get Problem, and not something else?
It seems to me that you are trying to implement the repository pattern
I achieve reduction of the code, removal of the general actions in the ancestor. In all the examples I met, a repository was created, but it played the role of an interface - all methods in all descendants had to be re-implemented.
In PHP one could do something like this:
public class BaseDBProxy
{
protected SiteContext db = new SiteContext();
protected $class_name="Problems";
public GetById(int id)
{
return db.$class_name.Where(e => e.Id == id).FirstOrDefault<$class_name>();
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question