3

Is there any way to move the Parse method into the abstract class ? I tried multiple ways (links at the bottom), but I am still hitting one or another roadblock.

public class AnimalEntityId : EntityId<AnimalEntityId>
{
    public AnimalEntityId()
        : base()
    {
    }

    private AnimalEntityId(string value)
        : base(value)
    {
    }

    public static AnimalEntityId Parse(string value)
    {
        return new AnimalEntityId(value);
    }
}


public abstract class EntityId<TEntityId>
{
    private readonly System.Guid value;

    protected EntityId(string value)
    {
        this.value = System.Guid.Parse(value);
    }

    protected EntityId()
    {
        this.value = System.Guid.NewGuid();
    }
}

Tried these suggestions with no luck:

Thanks in advance!

Community
  • 1
  • 1
Zygimantas
  • 8,547
  • 7
  • 42
  • 54
  • To clarify: you're looking for a method `public static TEntityId Parse(string value) {...}` on the abstract class? Is this method the only reason that `EntityId<>` is abstract? Because I'm not really seeing a reason for it to be both generic *and* abstract, at least in this reduced example. – Marc L. Feb 26 '16 at 03:14

3 Answers3

1

No, you cannot write a template constraint such as new(string) instead of simply new(). You'll have to leverage reflection to get it to work:

public abstract class EntityId<TEntityId>
    where TEntityId : EntityId<TEntityId>
{
    private readonly System.Guid value;

    protected EntityId(string value)
    {
        this.value = System.Guid.Parse(value);
    }

    protected EntityId()
    {
        this.value = System.Guid.NewGuid();
    }

    public static TEntityId Parse(string value)
    {
        return (TEntityId)Activator.CreateInstance(typeof(TEntityId), new object[] { value });
    }
}

Assuming you make the constructor accessible (instead of it currently being private). Note the constraint where TEntityId : EntityId<TEntityId> - which will ensure we'll only return subclasses of EntityId

Rob
  • 26,989
  • 16
  • 82
  • 98
  • 1
    Thank you Rob, very clean solution. Because I will not be creating hundreds of objects in a loop, after thinking for a few moments, I decided to go with reflection, as you recommended because this way I can provide the most easily readable API for other developer (or myself after few months), who will use this class library. Having a simple AnimalEntityId.Parse("ID") is a huge pros for me. I also started adding changes to access non-public constructors, and realized, that I have the same code as suggested by @dasblinkenlight – Zygimantas Feb 26 '16 at 10:09
1

If you don't mind using reflection, you can move Parse into the abstract type like this:

public static TEntityId Parse(string val) {
    var constr = typeof(TEntityId).GetConstructor(
        // Since the constructor is private, you need binding flags
        BindingFlags.Instance | BindingFlags.NonPublic
    ,   null
    ,   new[]{ typeof(string) }
    ,   null);
    if (constr == null) {
        throw new InvalidOperationException("No constructor");
    }
    return (TEntityId)constr.Invoke(new object[] {val});
}

Demo.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

How about making value a private mutable field/property and actually setting it from the Parse method?

(Curiously recurring generic parameter removed from EntityId for simplicity)

public class SimpleAnimalEntityId : EntityId
{
    // Implicit parameterless constructor.
}

public class ParametrizedAnimalEntityId : EntityId
{
    // Parametrized constructor only.
    public ParametrizedAnimalEntityId(int ignored)
    {
    }
}

public abstract class EntityId
{
    // Simple scenario: derived type has a parameterless constructor.
    public static TEntity Parse<TEntity>(string value)
        where TEntity : EntityId, new()
    {
        Guid id = Guid.Parse(value);

        return new TEntity { value = id };
    }

    // Advanced scenario: derived type constructor needs parameters injected.
    public static TEntity Parse<TEntity>(string value, Func<TEntity> constructor)
        where TEntity : EntityId
    {
        Guid id = Guid.Parse(value);
        TEntity entity = constructor();

        entity.value = id;

        return entity;
    }

    private Guid value;

    protected EntityId()
    {
        value = Guid.NewGuid();
    }
}

Now you can handle any constructor from your Parse method:

string id = Guid.NewGuid().ToString();
SimpleAnimalEntityId simple = EntityId.Parse<SimpleAnimalEntityId>(id);
ParametrizedAnimalEntityId parametrized = EntityId.Parse(id, () => new ParametrizedAnimalEntityId(42));
Kirill Shlenskiy
  • 9,367
  • 27
  • 39
  • Kirill, thanks, really interesting approach. I decided to use reflection in this situation, because in that case I can keep the API more cleaner: AnimalEntityId.Parse("ID") instead of EntityId.Parse(id). In your suggestion, I feel that implementation details, which should not be so visible to the user are leaking. – Zygimantas Feb 26 '16 at 10:15