Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Public parameterless constructor required in order to use generic ValueOf<TValue, TThis> #13

Open
ricardovicentini opened this issue Feb 27, 2021 · 3 comments

Comments

@ricardovicentini
Copy link

I think the requirement of a parameterless constructor should be fixed in order to avoid the creation of invalid objects which is an antipattern regarding DDD

@Paul-Williams
Copy link
Contributor

Do you mean there should be a parameterless instance constructor?
Currently the only parameterless constructor is a private static. This is run once per type TThis and is required to construct the factory method for creating instances using ValueOf<TThis>.From(TValue).

@Ergamon
Copy link

Ergamon commented Jul 19, 2021

I think he means the problem that the library does not prohibit this:

public class Email : ValueOf<string, Email>
{
}

var email = new Email();

So later on, when you start using the type, you will get some NullReference exceptions.

I am not sure if the expectation of everyone playing nice and using the factory method is a good one.

Having a mandatory constructor of T instead would make the wrting of the classes a little bit more ugly, but would get rid of this problem.

@SteveDunn
Copy link

I had a very similar library to this one, with the major difference being that validation wasn't optional as it was required in the constructor, e.g.

public class Age : ValueObject<int>
{
    public Age(int value) : base(value, x => x < 0 : "negative age" : string.Empty) { }
}

The base class would call the provided delegate and throw if it returned anything other than an empty string.

The down side is two heap allocations, one for the instance itself, and one for the delegate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants