Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

asp.net core - Interface implementation generic method c#

I wanted to make some basic controller. In this controller I would like to have common functions (Create, Edit, Delete,...) - each controller for different model and then BasicController would work with interface.

I wanted to have interface which will be implemented in each model to have access to common functions:

public interface IModel
    {
        public int Id { get; set; }

        public Task CreateAsync<T>(string userName, T model) where T: IModel;
        public Task<bool> EditAsync<T>(string userName, T model) where T: IModel;

        public bool CreateValidation();
        public bool EditValidation();
    }

Then I implement the Interface in one Model:

public class TestResult : IHasFile, IIsReserved, IModel
    {
        [Key]
        public int Id { get; set; }

        public async Task CreateAsync<T>(string resolverGID, T model) where T: TestResultModel
        {
            //do stuff

        }

        public async Task<bool> EditAsync<T>(string resolverGID,T model) where T: TestResultModel
        {
            //do stuff
        }
        ...
    }
public class TestResultModel : TestResult
    {
        [Display(Name = "ATTACHMENTS")]
        public List<Microsoft.AspNetCore.Http.IFormFile> File { get; set; }
        [Display(Name = "USED_STANDARDS")]
        public string[] UsedStandardsAsArray { get; set; }
    }

I suppose, that TestResultModel has to implement IModel as well when it is based on TestResult, but I get error:

Error CS0425 The constraints for type parameter 'T' of method 'TestResult.EditAsync(string, T)' must match the constraints for type parameter 'T' of interface method 'IModel.EditAsync(string, T)'. Consider using an explicit interface implementation instead.

I have tried to change the interface to:

public interface IModel
    {
        public int Id { get; set; }

        public Task CreateAsync<T>(string userName, T model) where T: class;
        public Task<bool> EditAsync<T>(string userName, T model) where T: class;

        public bool CreateValidation();
        public bool EditValidation();
    }

but still not working.

Do you know how to handle this issue? What am I doing wrong?

Many thanks for hints.

EDIT: tried also:

public interface IModel
    {
        public int Id { get; set; }

        public Task CreateAsync(string userName, IModel model);
        public Task<bool> EditAsync(string userName, IModel model);

        public bool CreateValidation();
        public bool EditValidation();
    }

and then:

public async Task CreateAsync(string resolverGID, TestResultModel model)
        {
            //do stuff

        }

        public async Task<bool> EditAsync(string resolverGID,TestResultModel model)
        {
            //do stuff
        }
        ...
    }

but then Interface is not implemented...

question from:https://stackoverflow.com/questions/65829419/interface-implementation-generic-method-c-sharp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...