代码之家  ›  专栏  ›  技术社区  ›  Jason Coyne

是否可以将C对象初始值设定项与工厂方法一起使用?

  •  25
  • Jason Coyne  · 技术社区  · 17 年前

    我有一个类,上面有一个静态工厂方法。我想调用工厂来检索类的一个实例,然后进行额外的初始化,最好是通过C对象初始值设定项语法:

    MyClass instance = MyClass.FactoryCreate()
    {
      someProperty = someValue;
    }
    

    VS

    MyClass instance = MyClass.FactoryCreate();
    instance.someProperty = someValue;
    
    6 回复  |  直到 8 年前
        1
  •  27
  •   eglasius    17 年前

    不可以。或者,您可以接受lambda作为参数,这也使您能够完全控制在哪个部分调用“创建”过程。你可以这样称呼它:

    MyClass instance = MyClass.FactoryCreate(c=>
       {
           c.SomeProperty = something;
           c.AnotherProperty = somethingElse;
       });
    

    创建将类似于:

    public static MyClass FactoryCreate(Action<MyClass> initalizer)
    {
        MyClass myClass = new MyClass();
        //do stuff
        initializer( myClass );
        //do more stuff
        return myClass;
    }
    

    另一个选项是返回一个构建器(使用隐式强制转换运算符到myClass)。您可以这样称呼:

    MyClass instance = MyClass.FactoryCreate()
       .WithSomeProperty(something)
       .WithAnotherProperty(somethingElse);
    

    检查 this 为建筑工人

    这两个版本都是在编译时检查的,并具有完全的IntelliSense支持。


    需要默认构造函数的第三个选项:

    //used like:
    var data = MyClass.FactoryCreate(() => new Data
    {
        Desc = "something",
        Id = 1
    });
    //Implemented as:
    public static MyClass FactoryCreate(Expression<Func<MyClass>> initializer)
    {
        var myclass = new MyClass();
        ApplyInitializer(myclass, (MemberInitExpression)initializer.Body);
        return myclass ;
    }
    //using this:
    static void ApplyInitializer(object instance, MemberInitExpression initalizer)
    {
        foreach (var bind in initalizer.Bindings.Cast<MemberAssignment>())
        {
            var prop = (PropertyInfo)bind.Member;
            var value = ((ConstantExpression)bind.Expression).Value;
            prop.SetValue(instance, value, null);
        }
    }
    

    它介于编译时检查和未检查之间。它确实需要一些工作,因为它强制对赋值使用常量表达式。我认为其他任何事情都是答案中已有方法的变化。记住,你也可以使用普通的作业,如果你真的需要的话,考虑一下。

        2
  •  3
  •   Hans Vonn    8 年前

    您可以使用如下扩展方法:

    namespace Utility.Extensions
    {
        public static class Generic
        {
            /// <summary>
            /// Initialize instance.
            /// </summary>
            public static T Initialize<T>(this T instance, Action<T> initializer)
            {
                initializer(instance);
                return instance;
            }
        }
    }
    

    您可以这样称呼它:

    using Utility.Extensions;
    // ...
    var result = MyClass.FactoryCreate()
                    .Initialize(x =>
                    {
                        x.someProperty = someValue;
                        x.someProperty2 = someValue2;
                    });
    
        3
  •  2
  •   Paul Stovell    17 年前

    + 1的“不”。

    以下是匿名对象方法的替代方法:

    var instance = MyClass.FactoryCreate(
        SomeProperty => "Some value",
        OtherProperty => "Other value");
    

    在这种情况下 FactoryCreate() 会是这样的:

    public static MyClass FactoryCreate(params Func<object, object>[] initializers)
    {
        var result = new MyClass();
        foreach (var init in initializers) 
        {
            var name = init.Method.GetParameters()[0].Name;
            var value = init(null);
            typeof(MyClass)
                .GetProperty(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase)
                .SetValue(result, value, null);
        }
        return result;
    }
    
        4
  •  1
  •   Andy White    17 年前

    不可以,对象初始值设定项只能用于对构造函数的“new”调用。一个选项可能是向工厂方法添加一些额外的参数,以便在工厂内创建对象时设置这些值。

    MyClass instance = MyClass.FactoryCreate(int someValue, string otherValue);
    
        5
  •  1
  •   configurator    17 年前

    就像大家说的,不。

    已建议将lambda作为参数。
    一种更优雅的方法是接受匿名的,并根据对象设置属性。即

    MyClass instance = MyClass.FactoryCreate(new {
        SomeProperty = someValue,
        OtherProperty = otherValue
    });
    

    不过,这要慢得多,因为对象必须反射到所有属性上。

        6
  •  0
  •   Trap    17 年前

    不,这是你只能做的“内联”。工厂的所有功能都可以为您返回一个引用。