Skip to content

Prototype #8

@lukasz-strus

Description

@lukasz-strus

Prototype

The prototype pattern is genereted based on one attribute:

  • [Prototype] - this attribute should be applied to the class that is about to become a prototype. The class must be a partial class.
using DesignPatternCodeGenerator.Attributes.Prototype;

namespace Samples.Prototype;

[Prototype]
public partial class Person
{
    public string? Name { get; set; }
    public string? LastName { get; set; }
    public Address? PersonAddress { get; set; }
    public Contacts? PersonContacts { get; set; }
}

public class Address
{
    public string? HouseNumber { get; set; }
    public string? Street { get; set; }
    public string? City { get; set; }
}

public class Contacts
{
    public string? PhoneNumber { get; set; }
    public string? Email { get; set; }
}

Using the code above, the generator will generate a partial class with an implemented prototype with the ShallowCopy() and DeepCopy() methods.

// <auto-generated/>
using DesignPatternCodeGenerator.Attributes.Prototype;

namespace Samples.Prototype
{
    public partial class Person
    {
        public Person ShallowCopy()
        {
            return (Person)this.MemberwiseClone();
        }

        public Person DeepCopy()
        {
            Person clone = (Person)this.MemberwiseClone();

            clone.PersonAddress = new Address()
            {
	    	HouseNumber = PersonAddress.HouseNumber,
		Street = PersonAddress.Street,
		City = PersonAddress.City
            };

	    clone.PersonContacts = new Contacts()
            {
	    	PhoneNumber = PersonContacts.PhoneNumber,
		Email = PersonContacts.Email
            };

            return clone;
        }
    }
}

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions