Call factory from the constructor to get a new version of "this"

I can get around this back ... I have a class that looks like a document and another class that looks like a template. They both inherit from the same base class, and I have a method for creating a new document from a template (or from another document, the method that is in the base class). Therefore, if I want to create a new document from a template, I simply create an instance of the template and call GetNewDoc ():

Document doc = mytemplate.GetNewDoc();

In the Document class, I have an empty constructor that creates a new empty document, as well as another constructor that accepts the document identifier so that I can load the document from the database. However, I also need a constructor that accepts a template identifier. So I can do

Document doc = New Document(TemplateID)

Since the template class already has the ability to return the document, I would like the constructor to do something like

Template temp = new Template(TemplateID);
this = temp.GetNewDoc();

Of course, I cannot do this, since "this" is read-only - and it seems weird anyway. I have a feeling that I am very stupid here, so feel free to scream :)

The fact is that the object in question is quite complex with several sets of child objects and the persistence of the database over several tables, so I do not want to duplicate too much code. Although, I think, I could just get a new document from the template, and then copy the fields / properties, since the collections should follow quite easily - it just looks like duplication.

More complex code example:

using System;
using System.Collections.Generic;
using System.Text;

namespace Test
{
class Program
{
    static void Main(string[] args)
    {
        // This just creates the object and assigns a value
        Instance inst = new Instance();
        inst.name = "Manually created";
        Console.WriteLine("Direct: {0}", inst.name);

        //This creates a new instance directly from a template
        MyTemplate def = new MyTemplate();
        Instance inst2 = def.GetInstance(100);
        Console.WriteLine("Direct from template: {0}", inst2.name);

        Instance inst3 = new Instance(101);
        Console.WriteLine("Constructor called the template: {0}", inst3.name);
        Console.ReadKey();

    }
}

public class Instance
{
    public string name;

    public Instance(int TemplateID)
    {
        MyTemplate def = new MyTemplate();
        //If I uncomment this line the build will fail
        //this = def.GetInstance(TemplateID);
    }

    public Instance()
    {
    }
}

class MyTemplate
{
    public Instance GetInstance(int TemplateID)
    {
        Instance inst = new Instance();
        //Find the template in the DB and get some values
        inst.name = String.Format("From template: {0}", TemplateID.ToString());
        return inst;
    }
}
}

code>

+3
source share
3

- , , .

, int? factory:

public static Instance CreateInstance(int id)
{
    MyTemplate def = new MyTemplate();
    return def.GetInstance(id);
}

- . ( SO - .)

+7

'this' . , factory . factory, createDocument(), , :

public class Factory {
   public static Document createBlankDocument();
   public static Document createDocument( DocumentId id );
   public static Document createDocumentFromTemplate( TemplateId id );
}
+2

, factory, , (- , ).

factory : " , ".

, , , , , , new; ( ):)

0

All Articles