0

I'm writing a C# application that will generate Dungeons & Dragons characters in bulk. However when I encountered the concept of a "bonus" to a character parameter I ran face first into a wall. Correctly constraining generic types for the method of the bonus escapes me. Here is an example of what I have:

public interface IAbilityScore : IBaseEntity
{
    string Name { get; set; }
}

public interface IBonus<T,K> where T : IBonusable<K>
{
    K Value { get; set; }
}

public interface IBonusable<T>
{
    T Value { get; set; }
}

public interface ICharacterAbilityScore : IBaseEntity, IBonusable<int>
{
    ICharacter Character { get; set; }
    IAbilityScore AbilityScore { get; set; }
    bool IsSaveProficient { get; set; }
}

The problem I face is that from a user perspective, I want the user to be able to add "This race gives a +2 bonus to Intelligence". Unfortunately, the ability score "Intelligence" won't have a Value property until it's associated with a character, thus making the IBonusable constraint an issue.

I would like to be able to constrain the value property of the IBonus to the same type as the value property of the IBonusable item without having to tie the bonus directly to a specific character (which would happen in my above implementation, with ICharacterAbilityScore inherting IBonusable).

Is there an interface-only method of accomplishing this, or will I have to add this constraint in when I define the concrete implementations?

Also, I plan on using code first EF to build out the database, if that affects the answer.

charles082986
  • 141
  • 1
  • 3
  • 12

1 Answers1

0

Am not sure if I understood your question correctly, but this per my understanding.

public interface IAbilityScore : IBaseEntity
{
    string Name { get; set; }
}

public interface IBonus<T>
{
    T Value { get; set; }
}

public interface IBonusable<T>
{
    T Value { get; set; }
}

public interface ICharacterAbilityScore<T> : IBaseEntity, IBonusable<T>,IBonus<T>
{
    ICharacter Character { get; set; }
    IAbilityScore AbilityScore { get; set; }
    bool IsSaveProficient { get; set; }
}
gvmani
  • 1,580
  • 1
  • 12
  • 20