我可以在不使用 'new' 的情况下构建组合 类 吗?

Can I construct composed classes without using 'new' so much?

我有一个基本的类型组合:

class A{
    public A(B1 b1, B2 b2){
        this.b1 = b1;
        this.b2 = b2;
    }
    public B1 b1 {get; private set;}
    public B2 b2 {get; private set;}
}

class B1{
    public B1(C1 c1, C2 c2, C3 c3){
        this.c1 = c1;
        this.c2 = c2;
        this.c3 = c3;
    }
    public C1 c1 {get; private set;}
    public C2 c2 {get; private set;}
    public C3 c3 {get; private set;}
}
class B2{
    public B2(C1 c1, C2 c2, C3 c3){
        this.c1 = c1;
        this.c2 = c2;
        this.c3 = c3;
    }
    public C1 c1 {get; private set;}
    public C2 c2 {get; private set;}
    public C3 c3 {get; private set;}
}

class C1{}
class C2{}
class C3{}

是否有更简洁的方法来构建 A

public static void Main()
{
    A a = new A(new B1(new C1(), new C2(), new C3()), new B2(new C1(), new C2(), new C3()));
}

不用那么多new我能实现同样的构造吗?
我可以对我的构造函数做些什么来使这成为可能吗?

如果在 A 的默认构造函数中创建了新的 B,而在 B 的默认构造函数中创建了 C,您可以将其简化为 A a = new A();

class A{
    public A() : this(new B1(), new B2())
    {
    }

    public A(B1 b1, B2 b2){
        this.b1 = b1;
        this.b2 = b2;
    }
    public B1 b1 {get; private set;}
    public B2 b2 {get; private set;}
}

class B1{
    public B1() : this(new C1(), new C2(), new C3())
    {
    }

    public B1(C1 c1, C2 c2, C3 c3){
        this.c1 = c1;
        this.c2 = c2;
        this.c3 = c3;
    }
    public C1 c1 {get; private set;}
    public C2 c2 {get; private set;}
    public C3 c3 {get; private set;}
}
class B2{
    public B2() : this(new C1(), new C2(), new C3())
    {
    }

    public B2(C1 c1, C2 c2, C3 c3){
        this.c1 = c1;
        this.c2 = c2;
        this.c3 = c3;
    }
    public C1 c1 {get; private set;}
    public C2 c2 {get; private set;}
    public C3 c3 {get; private set;}
}

class C1{}
class C2{}
class C3{}

public static void Main()
{
    A a = new A();
}

在组合类型时要考虑的替代方法是使用依赖容器,特别是如果您想避免硬编码new以获得更好的依赖注入。

例如,使用 Autofac 你会做:

var builder = new ContainerBuilder();

builder.RegisterType<A>();
builder.RegisterType<B1>();
builder.RegisterType<B2>();
builder.RegisterType<C1>();
builder.RegisterType<C2>();
builder.RegisterType<C3>();

var container = builder.Build();

var a = container.Resolve<A>();

对于您的简单示例来说,这似乎有些过分,但如果您确实在构建一个复杂的对象图,则需要考虑这一点。

不,没有 new 关键字创建新对象,您可以做的是在构造函数中使用可选参数。

class A
{

//Proper Immutable property works only c# 6 
      public B1 B1 { get; }
      public B2 B2 { get; }
      public A(B1 b1 = null, B2 b2 = null)
      {
           B1 = b1??new B1();
           B2 = b2??new B2();
      }
}

但这对于不可变对象来说意义不大,最好将它们保留为空,因为它会无缘无故地使用内存。