cdboy
cdboy

Reputation: 21

How to use interface with generic object?

I'm getting trouble in using the interface I create. I try to implement it but there an error occur. Any answer is appreciated. Thanks in advance.

Here the actual Interface I want to implement.

namespace CRDM.Core.Models
{
  [Table("cities")]
  public class City : ICity<CountryState>
  {
  }

  [Table("country_states")]
  public class CountryState : ICountryState<Country>
  {        
  }

  [Table("countries")]
  public class Country : ICountry
  {       
  }
}

namespace CRDM.Core.Abstractions.Entities
{
 public interface ICity <TState> :
    where TState : ICountryState<ICountry>
 {
    TState StateReference { get; set; }
 }

 public interface ICountryState<TCountry> :
    where TCountry : ICountry
 {

 }

 public interface ICountry
 {
 }
}

I successfully implement the Country and CountryState class, but there an error in the implementation of City. Here the error message.

The type CRDM.Core.Models.CountryState cannot be used as type parameter TState in the generic type or method ICity<TState>.

There is no implicit reference conversion from CRDM.Core.Models.CountryState to CRDM.Core.Abstractions.Entities.ICountryState<CRDM.Core.Abstractions.Entities.ICountry>.

Upvotes: 1

Views: 80

Answers (2)

Mick
Mick

Reputation: 6864

You probably need to ask yourself what are you trying to achieve and are you simply making this more complicated than it needs to be. If you want to access these entities using interfaces but make them work with entity framework I would do...

namespace CRDM.Core.Models
{
    using CRDM.Core.Abstractions.Entities;

    [Table("cities")]
    public class City : ICity
    {
        public CountryState StateReference { get; set; }
        ICountryState ICity.StateReference
        {
            get
            {
                return StateReference;
            }
            set
            {
                StateReference = (CountryState)value;
            }
        }
    }

    [Table("country_states")]
    public class CountryState : ICountryState
    {
        public Country Country { get; set; }
        ICountry ICountryState.Country
        {
            get
            {
                return Country;
            }
            set
            {
                Country = (Country)value;
            }
        }
    }

    [Table("countries")]
    public class Country : ICountry
    {
    }
}

namespace CRDM.Core.Abstractions.Entities
{
    public interface ICity
    {
        ICountryState StateReference { get; set; }
    }

    public interface ICountryState
    {
        ICountry Country { get; set; }
    }

    public interface ICountry
    {
    }
}

Upvotes: 0

Access Denied
Access Denied

Reputation: 9501

Try doing it this way:

namespace CRDM.Core.Models
{

    public class City : ICity<CountryState,Country>
    {
        public CountryState StateReference { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
    }


    public class CountryState : ICountryState<Country>
    {
    }


    public class Country : ICountry
    {
    }
}

namespace CRDM.Core.Abstractions.Entities
{
    public interface ICity<TState,TCountry>        
       where TCountry: ICountry
       where TState : ICountryState<TCountry>
    {
        TState StateReference { get; set; }
    }

    public interface ICountryState<TCountry>        
       where TCountry : ICountry
    {

    }

    public interface ICountry
    {
    }
}

Or this way:

    namespace CRDM.Core.Models
    {
        public class City : ICity<CountryState>
        {
            public CountryState StateReference { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
        }

        public class CountryState : ICountryState<ICountry>
        {
        }

        public class Country : ICountry
        {
        }
    }

    namespace CRDM.Core.Abstractions.Entities
    {
        public interface ICity<TState> 

           where TState : ICountryState<ICountry>
        {
            TState StateReference { get; set; }
        }

        public interface ICountryState<TCountry> 

           where TCountry : ICountry
        {

        }

        public interface ICountry
        {
        }
    }

Upvotes: 1

Related Questions