pmn
pmn

Reputation: 2257

What is meant by the term "DBset can be used when the type of entity is not known at build time"?

What is meant by the term

DBset can be used when the type of entity is not known at build time

in this sentence :

DBSet class represents an entity set that is use for create, read, update, and delete operations. A generic version of DBSet (DbSet) can be used when the type of entity is not known at build time.

Upvotes: 2

Views: 192

Answers (2)

mboldt
mboldt

Reputation: 1825

Let's imagine you want to create a repository for easier data access, but you don't want to create a separate repository for each DBSet (or table in your database) you have. Instead you can create a generic repository and once you initialize this repository-object, you can add the information which DBSet you want to address with this repository-object.

The repository would look something like this

public class Repository<T> where T : EntityObject
{
    public Repository(YourDBContext context) {
        _context = context;
        if(_context != null)
        {
            _dbSet = _context.Set<T>();
        }
    }

    /* add methods for insert, update, delete, etc... */

    private YourDBContext _context;
    private DBSet<T> _dbSet;
}

So for example you have the DbSets User and Comment. Instead of creating the repositories UserRepository and CommentRepository you can now use the generic repository for both DbSets:

using(YourDBContext context = new YourDBContext())
{
    Repository<User> userRepo = new Repository<User>(contex);
    userRepo.Insert(userEntity);
    Repository<Comment> commentRepo = new Repository<Comment>(context);
    commentRepo.Delete(commentEntity);
}

Of course if there is more to the repository pattern but this is only meant as an example. And to sum up your question what the sentence means: When you want to generalize which DbSet you want to address with a class, you can make the class generic and add the information (about which DbSet to address) later at runtime.

Upvotes: 1

Mihail Stancescu
Mihail Stancescu

Reputation: 4138

I don't know your exact context of that message but you can do that by using DbSet to use a generic type.

More information here.

Upvotes: 0

Related Questions