Generic function correct usage?
Generic function correct usage?
I'm new this site, I have a repository pattern, if you check below code, here the return type & parameter T is defined which will implemented by calling methods so at run time it will be replaced by SomeClass
and the parameter will be changed like it will be string, int or bool, however below will not compile however I need to make it work please help and alternative will work too.
SomeClass
public interface IBaseRepository
Task<T> Add<T>(T value,string typeName);
public class Test: IBaseRepository
public Task<SomeClass> Add<SomeClass>(string value, string typeName)
throw new NotImplementedException();
public Task<SomeClass> Add<SomeClass>(int value, string typeName)
throw new NotImplementedException();
public Task<SomeClass> Add<SomeClass>(bool value, string typeName)
throw new NotImplementedException();
Redundant Approach:
public interface IBaseRepository
Task<T> AddData<T>(string namespaceName,string typeName);
Task<T> AddData<T>(int namespaceName, string typeName);
public abstract class BaseRepo : IBaseRepository
public async Task<T> AddData<T>(string namespaceName, string typeName)
return JsonConvert.DeserializeObject<T>(namespaceName.ToString());
public async Task<T> AddData<T>(int namespaceName, string typeName)
return JsonConvert.DeserializeObject<T>(namespaceName.ToString());
public class Testing : BaseRepo
public async Task<Testing> Add(string namespaceName, string typeName)
return await AddData<Testing>(namespaceName,"");
public async Task<Testing> Add1(int namespaceName, string typeName)
return await AddData<Testing>(namespaceName, "");
@Md.AbdulAlim Could you please give sample code to understand?
– harsh
Aug 19 at 9:07
3 Answers
3
public interface IBaseRepository<E>
Task<T> Add<T>(E value,string typeName);
public class Test: IBaseRepository<SomeDefinedClass>
public Task<SomeClass> Add<SomeClass>(SomeDefinedClass value, string typeName)
throw new NotImplementedException();
Here is a solution for the problem, but there is still a problem in terms of how you are using it for repository pattern.
that's what I have done in my edited question, I have to write 3 different functions, what if I want to have Someclass as return type & "OtherClass" reference as argument which I would be passing dynamically? Note : There will be 100's of classes so I won't be creating 100's of function right ? eg: Task<T> Add<T>(OtherClass value,string typeName); Task<T> Add<T>(AnotherClass value,string typeName);
– harsh
Aug 19 at 10:29
are you always going to have that three add method? in one Test class?
– Luminous_Dev
Aug 19 at 10:44
see my edited post, you can potentially do this thing too
– Luminous_Dev
Aug 19 at 10:47
Does this look more like what you're after?
public interface IBaseRepository<R>
Task<T> Add<T>(R value, string typeName);
public class Test<R> : IBaseRepository<R>
public Task<T> Add<T>(R value, string typeName)
throw new NotImplementedException();
You only have to implement the method once. You just have to create a new Test<R>
for each R
you want to run.
Test<R>
R
I am sure that it will give you error that interface is not implemented.
As per your interface
Task Add(T value,string typeName);
Here if you look at correctly then Generic Type T must be in return Type Task and same way it should be input of Add function.
Now look at implementation
None of your method satisfy interface. For example
public Task Add(string value, string typeName)
Here T is SomeClass so your first argument of method must be of Type SomeType.
Update 1: I would do something like this.
public interface IBaseRepository<T,U,V>
Task<V> AddData(T namespaceName, U typeName);
public class SomeClass
public class Test : IBaseRepository<string, string, SomeClass> , IBaseRepository<int,string,SomeClass>
public Task<SomeClass> AddData(int namespaceName, string typeName)
throw new NotImplementedException();
public Task<SomeClass> AddData(string namespaceName, string typeName)
throw new NotImplementedException();
So what is the better approach?
– harsh
Aug 19 at 9:08
I have updated another Redundant approach, which is something I don't want.
– harsh
Aug 19 at 9:15
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Generic interface implementation should also generic. You should try to call generic method by different type of values.
– Md. Abdul Alim
Aug 19 at 8:15