Home > Mobile >  Dotnet core genric type parameter inherited mode
Dotnet core genric type parameter inherited mode

Time:12-03

I would like to ask how can I achive such a signature in dotnet? I have these three methods

public IEnumerable<Position> GetOpenPositions(IEnumerable<AllianceOpenPosition> allianceOpenPositions, uint allianceId, IEnumerable<agile_shared.RequestModel.OpenPosition> orgOpenPositionList)
public IEnumerable<Position> GetOpenPositions(IEnumerable<SquadOpenPosition> squadOpenPositions, uint squadId, IEnumerable<agile_shared.RequestModel.OpenPosition> orgOpenPositionList)
public IEnumerable<Position> GetOpenPositions(IEnumerable<TribeOpenPosition> tribeOpenPosition, uint tribeId, IEnumerable<agile_shared.RequestModel.OpenPosition> orgOpenPositionList)

As you can see the only difference in between the signature is the type of list received as input. All three models are united under a base abstract class called OpenPositionBase

I would like to create an interface in order to restrict the object to have the GetOpenPositions method implemented.

I have tried to create something like this

public IEnumerable<Position> GetOpenPositions(IEnumerable<OpenPositionBase> tribeOpenPosition, uint tribeId, IEnumerable<agile_shared.RequestModel.OpenPosition> orgOpenPositionList)

Common base class

public class OpenPositionBase
    {
        public uint OpenPositionId { get; set; }
    }

AllianceOpenPosition

public class AllianceOpenPosition : OpenPositionBase
    {
        public AllianceOpenPosition()
        {
            Alliance = new Alliance();
            LeadRole = new LeadRole();
        }

        public uint Id { get; set; }

        public uint AllianceId { get; set; }

        public uint LeadRoleId { get; set; }
    }

This class is the implementation of the IPositionConverter interface where I am looking for to add the GetOpenPositions method in a general way so in the SquadPositionConverter class I could use this method as GetOpenPositions(IEnumerable<SquadOpenPosition> allianceOpenPositions, uint allianceId, IEnumerable<agile_shared.RequestModel.OpenPosition> orgOpenPositionList)

public class AlliancePositionConverter : IPositionConverter
    {

        public IEnumerable<Position> GetOpenPositions(IEnumerable<AllianceOpenPosition> allianceOpenPositions, uint allianceId, IEnumerable<agile_shared.RequestModel.OpenPosition> orgOpenPositionList)
        {
            return allianceOpenPositions.Where(w => w.AllianceId == allianceId)
                                        .Select(s => new Position(orgOpenPositionList.FirstOrDefault(wh => wh.Id == s.OpenPositionId))
                                        {
                                            Id = s.Id.ToString(),
                                            AgileRole = s.LeadRole.Name,
                                            AgileRoleRank=s.LeadRole.Rank
                                        }).ToList();
        }

and all the rest of the classes have a similar structure.

and than change in each object the param to the inherited model. Unfortunately it gives and error.

Any possible solution other than create all three and implement only the one needed?

CodePudding user response:

not really sure what you're exactly after but:

You can make the method generic itself:

public IEnumerable<Position> GetOpenPos<T>(IEnumerable<T> list, uint tribeId, IEnumerable<agile_shared.RequestModel.OpenPosition> orgOpenPositionList) where T : OpenPositionBase

Then you can work with your list as if it was an open position base

or you can create a generic interface:

public interface IGetOpenPos<T> where T : OpenPositionBase
{
    public IEnumerable<Position> GetOpenPos(IEnumerable<T> list, uint tribeId, IEnumerable<agile_shared.RequestModel.OpenPosition> orgOpenPositionList);
}

public class AlianceOpenPosition : OpenPositionBase, IGetOpenPos<AlianceOpenPosition>
{
    public IEnumerable<Position> GetOpenPos(IEnumerable<AlianceOpenPosition> list, uint tribeId, IEnumerable<agile_shared.RequestModel.OpenPosition> orgOpenPositionList)
    {
        // implement
    }
}
  • Related