0
I have this interface base repo with genric type T:
and this interfaces heritance from the IRepositoryBase
and this the class implementation of IRepositoryBase
and this the implementaion of class1 & class2
& in this methode i need to return one of Repo
But inside the brackets in IRepositoryBase it takes one type of class1 or class2 so in my case I need to return by params a.
How to use union or something else to set the multiple types in barckets and choose the return by conditon in params a?
I have this interface base repo with genric type T:
C#:
public interface IRepositoryBase<T> where T : class
{
void Add(T obj);
}
and this interfaces heritance from the IRepositoryBase
C#:
public interface IClass1Repository : IRepositoryBase<Class1>
{
}
public interface IClass2Repository : IRepositoryBase<Class2>
{
}
and this the class implementation of IRepositoryBase
C#:
public class RepositoryBase<T> : IRepositoryBase<T> where T : class
{
private readonly Context _context;
public RepositoryBase(Context context)
{
_context = context;
}
public void Add(T obj)
{
_context.Set<T>().Add(obj);
}
}
and this the implementaion of class1 & class2
C#:
public class class1Repository : RepositoryBase<Students>, IClasse1Repository
{
public class1Repository (Context context) : base(context)
{
}
}
C#:
public class class2Repository : RepositoryBase<Students>, IClasse2Repository
{
public class2Repository (Context context) : base(context)
{
}
}
& in this methode i need to return one of Repo
C#:
private IRepositoryBase<> GetRepository(string a)
{
var scope = _serviceScopeFactory.CreateScope();
switch (a)
{
case "class1":
return scope.ServiceProvider.GetService<IClass1Repository>();
case "class2":
return scope.ServiceProvider.GetService<IClass2Repository>();
}
return null;
}
But inside the brackets in IRepositoryBase it takes one type of class1 or class2 so in my case I need to return by params a.
How to use union or something else to set the multiple types in barckets and choose the return by conditon in params a?
Last edited by a moderator: