Home > Back-end >  How write input and output generic parameter for interface
How write input and output generic parameter for interface

Time:03-29

Help me plz with generics

I'm trying to make an interface with methods and I don't understand how to set input and output parameter for interfaces

i have a class with method

public TeachersDto[] MapToDto(Teachers[] entities, Dictionary<string, ParameterData> data)

public Teachers[] MapDto(TeachersDto dto, Dictionary<string, ParameterData> Data)

i try

public interface ITeachersMapper<RInput, Dictionary<string, RInput2>, TOutput>
{
    TDto[] MapToDto(TEntity[] entities, Dictionary<string, TInput> data);

    TEntity[] MapDto(TDto[] dtos, Dictionary<string, TInput> data);

}

How write input and output generic parameter for interface?

CodePudding user response:

It's unclear how you are trying to use this interface since your interface doesn't match with your implementation that you give in the question, but I think this is what you are looking for.

Keep in mind that generic parameters can be called whatever you want, but T is just convention, so name your generic parameters something to make it obvious what they represent.

Also, you can't have a mixture of a specific type like Dictionary with a generic type. You can have a generic type with type constraints using where, but most likely you just want a generic type to define the ParameterData and don't need any of the Dictionary stuff in the generic specification.

I changed your classes to be just Teacher and TeacherDto since classes usually take a singular form.

    public interface IEntityMapper<TEntity, TParameterData, TDto>
    {
        TDto[] MapToDto(TEntity[] entities, Dictionary<string, TParameterData> data);
        TEntity[] MapDto(TDto[] dtos, Dictionary<string, TParameterData> data);
    }
    
    public class TeacherMapper : ITeacherMapper<Teacher, ParameterData, TeacherDto>
    {
        public TeacherDto[] MapToDto(Teacher[] entities, Dictionary<string, ParameterData> data)
        {
            //..mapping to DTO here
        }
        public Teacher[] MapDto(TeacherDto[] dto, Dictionary<string, ParameterData> Data)
        {
            //..mapping to domain here
        }
    }
  • Related