C
C#2y ago
shivam51

❔ What is the use of datatype after function name?

I have a function signature that looks like this:
c#
public static IServiceCollection AddMongoRepository<T>(this IServiceCollection services, string collectionName) where T : IEntity
c#
public static IServiceCollection AddMongoRepository<T>(this IServiceCollection services, string collectionName) where T : IEntity
What is the use of <T> after the function name?
10 Replies
Thinker
Thinker2y ago
That's a type parameter It essentially lets you pass a type as a parameter to a function
Thinker
Thinker2y ago
Generic Methods - C# Programming Guide
Learn about methods declared with type parameters, known as generic methods. See code examples and view additional available resources.
Thinker
Thinker2y ago
T in this case is constrained to implementing the IEntity interface. I.e. when you call AddMongoRespository, the type you specify in the <> has to implement IEntity.
shivam51
shivam51OP2y ago
This function is actually a part of generic service, one of the lines inside the function is :
services.AddSingleton<IRepository<T>>(provider =>
services.AddSingleton<IRepository<T>>(provider =>
So if I need to use <T> anywhere inside the function its important to pass a type parameter while function call?
public static IServiceCollection AddMongoRepository<T>(this IServiceCollection services, string collectionName) where T : IEntity
{
services.AddSingleton<IRepository<T>>(provider =>
{
var database = provider.GetService<IMongoDatabase>();
return new MongoRepository<T>(database, collectionName);
});
return services;
}
public static IServiceCollection AddMongoRepository<T>(this IServiceCollection services, string collectionName) where T : IEntity
{
services.AddSingleton<IRepository<T>>(provider =>
{
var database = provider.GetService<IMongoDatabase>();
return new MongoRepository<T>(database, collectionName);
});
return services;
}
public static IServiceCollection AddMongoRepository(this IServiceCollection services, string collectionName)
{
services.AddSingleton<IRepository<IEntity>(provider =>
{
var database = provider.GetService<IMongoDatabase>();
return new MongoRepository<IEntity>(database, collectionName);
});
return services;
}
public static IServiceCollection AddMongoRepository(this IServiceCollection services, string collectionName)
{
services.AddSingleton<IRepository<IEntity>(provider =>
{
var database = provider.GetService<IMongoDatabase>();
return new MongoRepository<IEntity>(database, collectionName);
});
return services;
}
so both of these functions are right. Is this correct understanding?
Thinker
Thinker2y ago
The second one adds an IRepository of specifically IEntity. The first adds an IRepository of a specific type (which still implements IEntity).
shivam51
shivam51OP2y ago
so both are correct syntax right? so in the second case I could simply call the function without specifuying the type in <>
Thinker
Thinker2y ago
In the second one, if you call services.GetService<IRepository<SomeEntity>>() then it'll throw an exception. It'll only work if you do services.GetService<IRepository<IEntity>>()
shivam51
shivam51OP2y ago
yes, that makes sense.
Thinker
Thinker2y ago
Since I assume the point of IRepository is to contain entities of a specific type, the first one is correct.
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server