Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用私有setter为类创建外部生成器?_C#_Design Patterns - Fatal编程技术网

C# 如何使用私有setter为类创建外部生成器?

C# 如何使用私有setter为类创建外部生成器?,c#,design-patterns,C#,Design Patterns,是否有某种方法可以从外部构建器类构建类,而不让它们的设置器公开?我不希望在没有必要验证的情况下允许类的无效实例存在 到目前为止,我找到的唯一方法是创建一个具有所需参数和验证的构造函数 我举了一个需要填充的属性很少的例子,但是想象一个有各种属性的例子 public class Computer { public string Motherboard { get; private set; } public string RamMemory { get

是否有某种方法可以从外部构建器类构建类,而不让它们的设置器公开?我不希望在没有必要验证的情况下允许类的无效实例存在


到目前为止,我找到的唯一方法是创建一个具有所需参数和验证的构造函数

我举了一个需要填充的属性很少的例子,但是想象一个有各种属性的例子

    public class Computer
    {
        public string Motherboard { get; private set; }
        public string RamMemory { get; private set; }
        public string Cpu { get; private set; }

        public Computer(string motherboard, string ramMemory, string cpu)
        {
            ValidateMotherboard(motherboard);
            ValidateRamMemory(ramMemory);
            ValidateCpu(cpu);

            Motherboard = motherboard;
            RamMemory = ramMemory;
            Cpu = cpu;
        }

        private void ValidateMotherboard(string motherboard)
        {
            if (motherboard == null)
                throw new ArgumentNullException(nameof(motherboard));
        }

        private void ValidateRamMemory(string ramMemory)
        {
            if (ramMemory == null)
                throw new ArgumentNullException(nameof(ramMemory));
        }

        private void ValidateCpu(string cpu)
        {
            if (cpu == null)
                throw new ArgumentNullException(nameof(cpu));
            else if (!cpu.ToLower().Contains("ryzen"))
                throw new ArgumentException("Only AMD Ryzen CPU's is good enough.");
        }
    }

    public class ComputerBuilder
    {
        private string _motherboard;
        private string _ramMemory;
        private string _cpu;

        public ComputerBuilder SetMotherboard(string motherboard)
        {
            _motherboard = motherboard;
            return this;
        }

        public ComputerBuilder SetRamMemory(string ramMemory)
        {
            _ramMemory = ramMemory;
            return this;
        }

        public ComputerBuilder SetCpu(string cpu)
        {
            _cpu = cpu;
            return this;
        }

        public Computer Build() => new Computer(_motherboard, _ramMemory, _cpu);
    }

我只希望有一个构建器,它可以轻松地构建具有太多参数的类。如何在不允许存在无效实例的情况下将验证放在构建器上?

“具有所需参数和验证的构造函数”是在C#中实现的。您可以嵌套类并设置受保护的内部访问器,因此只有父类可以设置它们,而子类本身是公共的,可以作为containerclass.subclass访问。但这是复杂的,可能很少有真实世界的用例来证明这一点。自动生成的强类型数据集类用于其datarow类。您可以定义包含所需字段的
ComputerParameters
结构,并将其传递给
Create
方法。类似于如何工作。EdPlunkett这是我目前使用的方式。一种DDD方法,其中构造函数或工厂方法验证或调用外部验证器来检查参数。CeeMcSharpface一开始听起来有点尴尬,但在某些情况下可以解决。吉米谢尔:我喜欢。我想到了类似的事情,将构建器传递给构造函数。类似于
公共计算机(ComputerBuilder)“具有所需参数和验证的构造函数”是在C#中实现的方法。您可以嵌套类并使
受保护的内部
设置访问器,因此只有父类可以设置它们,而子类本身是公共的,可以作为containerclass.subclass访问。但这是复杂的,可能很少有真实世界的用例来证明这一点。自动生成的强类型数据集类用于其datarow类。您可以定义包含所需字段的
ComputerParameters
结构,并将其传递给
Create
方法。类似于如何工作。EdPlunkett这是我目前使用的方式。一种DDD方法,其中构造函数或工厂方法验证或调用外部验证器来检查参数。CeeMcSharpface一开始听起来有点尴尬,但在某些情况下可以解决。吉米谢尔:我喜欢。我想到了类似的事情,将构建器传递给构造函数。类似于
公共计算机(ComputerBuilder)