Inheritance issue in C #

I am refactoring some code and want the classes a little higher in the inheritance chain to be a bit more strict with their parameters. Since I'm not sure I'm explaining this correctly, here is what I have:

public interface ISvdPredictor
{
    List<string> Users { get; set; }
    List<string> Artists { get; set; }
    float PredictRating(ISvdModel model, string user, string artist);
    float PredictRating(ISvdModel model, int userIndex, int artistIndex);
}

ISvdPredictoruses ISvdModel:

public interface ISvdModel
{
    float[,] UserFeatures { get; set; }
    float[,] ArtistFeatures { get; set; }
}

Now I want to implement another variation:

public interface IBiasSvdPredictor : ISvdPredictor
{
    float PredictRating(IBiasSvdModel model, string user, string artist);
    float PredictRating(IBiasSvdModel model, int userIndex, int artistIndex);
}

Uses IBiasSvdModelthat comes from ISvdModel:

public interface IBiasSvdModel : ISvdModel
{
    float GlobalAverage { get; set; }
    float[] UserBias { get; set; }
    float[] ArtistBias { get; set; }
}

IBiasSvdPredictorwill not work with ISvdModel.

The problem is that during implementation IBiasSvdPredictorI would have to implement 2 pairs of PredictRating methods. One of ISvdPredictor, and the other of IBiasSvdPredictor. What do I need to do to be able to simply implement those from IBiasSvdPredictor?

generics, PredictRating BiasSvdPredictor IBiasSvdModel where. , . , , .

EDIT: - , . https://github.com/gligoran/RecommendationSystem. BSc.

+5
6

generics .

public interface ISvdModel
{
    float[,] UserFeatures { get; set; }
    float[,] ArtistFeatures { get; set; }
}

public interface IBiasSvdModel : ISvdModel
{
    float GlobalAverage { get; set; }
    float[] UserBias { get; set; }
    float[] ArtistBias { get; set; }
}

public interface ISvdPredictor<in TSvdModel>
    where TSvdModel : ISvdModel // Require that TSvdModel implements ISvdModel
{
    List<string> Users { get; set; }
    List<string> Artists { get; set; }

    float PredictRating(TSvdModel model, string user, string artist);
    float PredictRating(TSvdModel model, int userIndex, int artistIndex);
}

// I would actually avoid declaring this interface. Rather, see comment on the class.
public interface IBiasSvdPredictor : ISvdPredictor<IBiasSvdModel> { }

class BiasSvdPredictor : IBiasSvdPredictor // Preferred : ISvdPredictor<IBiasSvdModel>
{
    // ...
    public float PredictRating(IBiasSvdModel model, string user, string artist) { }
    public float PredictRating(IBiasSvdModel model, int userIndex, int artistIndex) { }
}
+4

interface PredictRating. , . .

abstract, interface. PredictRating virtual, , . .

, . N , PredictRating, .

 public interface Demo
    {
        int PredictRating(int param1);
    }

    public abstract class AbstractDemo : Demo
    {
        public virtual int PredictRating(int param1)
        {
            return param1 + 1;
        }
    }

    public class ClassDemo1 : AbstractDemo
    {
        //This guy uses AbstractDemo Predict Rating
        public override int PredictRating(int param1)
        {
            return base.PredictRating(param1);
        }
    }

    public class ClassDemo2 : AbstractDemo
    {
        //This guy overrides the predict rating behavior
        public override int PredictRating(int param1)
        {
            return param1 + 2;
        }
    }
+2

. , , . , .

public class Foo : IBiasSvdPredictor {
    public float PredictRating(IBiasSvdModel, string user, string artist) { .... }

    // this is an expicit implementation of ISvdPredictor method. You satisfy
    // the interface, but this method is not a public part of the class. You have to
    // cast the object to ISvdPredictor in order to use this method.
    float ISvdPredictor.PredictRating(ISvdModel model, string user, string artist) {
        this.PredictRating((IBiasSvdModel)model, user, artist);
    }
}

, , , ISvdModel IBiasSvdModel.

+1

, ISvdPredictor, .

0

2 PredictRating.

, . ?

IBiasSvdPredictor IBiasSvdModel PredictRating, IBiasSvdPredictor a ISvdPredictor ( a ISvdModel PredictRating) IBiasSvdPredictor from ISvdPredictor .

-, .

0

( ), , ISvdModel . , , . ISvdModel ( IBiasSvdModel) , ISvdPredictor. 2 , 2 .

, ; ISvdPredictor IBiasSvdPredictor , ISvdModel, IBiasSvdModel, 2 , , ISvdModel . , Inversion of Control, , Injection Dependency Injection, .

0

All Articles