简体   繁体   中英

Constraint parameters, new()

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!

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

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.

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM