Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# : Extending Generic class

Tags:

c#

generics

partial class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
}

My generic repository implements a common set of methods for TEntity like

public TEntity Get(int id)
{
    return _context.Set<TEntity>()
        .Find(id);
}

public TEntity Get(Expression<Func<TEntity, bool>> predicate)
{
    return _context.Set<TEntity>()
}

which I can access like

Repository<User>().Get();

Many repositories does the same set of operation, so it is beneficial but now I want to extend Repository<User> to support some additional behavior.

partial class Repository<User> : IRepository<User> 
{
    public user DoMagicFunction()
    {
    }
}

so that I can use the repository like

Repository<User>().DoMagicFunction();

how can I extend the same generic class for Some Tentity to extend new behaviour instead of modifying it.

I could have done the same like creating another UserRepository to support new feature, but the accessor would become

UserRepository.DoMagicFunction();

but I want it to be like

Repository<User>().DoMagicFunction();
like image 492
Shekhar Pankaj Avatar asked Sep 21 '17 13:09

Shekhar Pankaj


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Full form of C is “COMPILE”.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 Answers

You can use an extension method:

public static class ExtensionMethods {

    public static User DoMagicFunction(this Repository<User> repository) {
        // some magic
        return null; //or another user
    } 

}

This will thus add the function in a syntactically nice way to Repository<User> objects.

In case you want to support it not only for Users, but for subclasses of Users as well, you can make the function generic:

public static class ExtensionMethods {

    public static TEntity DoMagicFunction<TEntity>(this Repository<TEntity> repository)
        where TEntity : User {
        // some magic
        return null; //or another TEntity
    } 

}
like image 116
Willem Van Onsem Avatar answered Sep 26 '22 01:09

Willem Van Onsem