user2153675
user2153675

Reputation: 103

using DTOs of ValueObjects to instantiate a Entity

My question is about the following scenario. There is an Artist entity

` public class Artist : Entity
{
    public Contact artistContact { get; private set; }

    internal Artist() { }

    public Artist(Guid _artistId, Contact _artistContact): base(_artistId)
    {
        artistContact = new Contact(_artistContact);
    }

    public Artist(Contact _artistContact) : base()
    {
        artistContact = new Contact(_artistContact);

    }

    public void MakeArt(){} // methods and so on
}`

And this Artist entity has a Value Object Contact that holds his name, email and so on.

    `public class Contact : ValueObject<Contact>
{
    readonly string name;
    readonly string email;

    public string Name { get { return name; } }
    public string Email { get { return email; } }

    public Contact(string _name, string _email)
    {
        name = _name;
        email = _email;
    }

}`

So my Questions is : Is it ok to

  1. Create a DTO of Value Object Contact
  2. Mapp the DTO to Contact given by the Factory (factory for ValueObjects)
  3. And at last call ArtistFactory.Create(Contact)

The factories use Activator.CreateInstance(), so the creation of objects with a constructor(parameters of a constructor) is done via Activator.

I am attempting to ensure that an Artist entity can not be created without a Contact, and am not sure is this the right way to handle this situatuion. Any adivce on how to approach this matter is welcome.

Upvotes: 1

Views: 434

Answers (1)

Dariss
Dariss

Reputation: 1258

You're complicating your domain and overengineering simple object creation.

Find proper name

Firstly entities are not created from sky. Maybe you can find better name for your ArtistFactory create method. For example register. It exactly says, what is going on there.

One factory

Secondly you need to ask your business, does Contact object, has any sense to exist outside of Customer?
I will make assumption, that it can't exist outside of Customer.
Your model is wrong then, because you're letting people create Contact outside of Artist. You will want to place creation of Contact inside of ArtistFactory.

Next thing, you should think, if email isn't another VO? Maybe you will want to reuse it somewhere else.

Do not create wrong domain objects

Next thing you shouldn't allow to create wrong object in your domain layer. Now with your implementation you do allow for example to create email with such string 'fdsfsdssfsf' and empty name. I guess it is not what you expect. You create VO with public constructor and private setters. Setters should have validation inside. For example to check, if your email is correct.

Simple declaration.

Create method declaration should use native types. You will call create method from outside of the domain, so mapping should be as simple as it can be.
Instead of pushing information about Contact to the client by providing create (Contact contact), you should have create (string name, string email) in your Artist Factory.

Upvotes: 1

Related Questions