【问题标题】:How to auto generate poco class (object) from a method parameters?如何从方法参数自动生成 poco 类(对象)?
【发布时间】:2016-07-09 09:07:35
【问题描述】:

有没有什么捷径可以从现有的带参数的方法自动生成 poco 类?

 public void RegisterUser(string userName, string fullName, 
string tel, string mobile, string website, string address, string blah){
    //--
    }

通过这种方法,我应该能够自动生成我的类,如下所示:

class UserRegisterDetail{

public string UserName {get;set;}
public string FullName {get;set;}
public string Tel {get;set;}
public string Mobile {get;set;}
public string Website {get;set;}
public string Address {get;set;}
public string Blah {get;set;}
}

我使用 resharper 和重构,但我无法生成简单的 poco 类。

更新:

使用“从参数中提取类”选项生成以下内容:

    public class RegisterUserParams
        {
            private string userName;
            private string email;
            private string fullName;
            private string jobTitle;
            private string department;
            private string tel;
            private string mobile;
            private string switchboard;
            private string fax;
            private string address1;
            private string address2;
            private string address3;

            public RegisterUserParams(string userName, string email, string fullName, string jobTitle, string department, string tel, string mobile, string switchboard, string fax, string address1, string address2, string address3 )
            {
                this.userName = userName;
                this.email = email;
                this.fullName = fullName;
                this.jobTitle = jobTitle;
                this.department = department;
                this.tel = tel;
                this.mobile = mobile;
                this.switchboard = switchboard;
                this.fax = fax;
                this.address1 = address1;
                this.address2 = address2;
                this.address3 = address3;

            }
public string UserName
            {
                get { return userName; }
            }

            public string Email
            {
                get { return email; }
            }

            public string FullName
            {
                get { return fullName; }
            }

            public string JobTitle
            {
                get { return jobTitle; }
            }

            public string Department
            {
                get { return department; }
            }

            public string Tel
            {
                get { return tel; }
            }

            public string Mobile
            {
                get { return mobile; }
            }

            public string Switchboard
            {
                get { return switchboard; }
            }

            public string Fax
            {
                get { return fax; }
            }

            public string Address1
            {
                get { return address1; }
            }

            public string Address2
            {
                get { return address2; }
            }

            public string Address3
            {
                get { return address3; }
            }         
        }

【问题讨论】:

  • 您是否尝试过“从参数中提取类”重构?顺便说一句,在 C# 中,它是 POCO,而不是 pojo。
  • 您不希望一个方法的参数超过 3 个。它被认为是一个糟糕的设计 - 阅读 Robert C. Martin Clean Code Page 40。
  • 这就是我试图自动生成 poco 类来传递对象而不是传递太多参数的原因
  • 我确实尝试过“从参数中提取类”,但它确实生成了一个具有默认构造函数和我在问题中更新的字段的类
  • 由于它们都是字符串,您可以在这里使用params 关键字。

标签: c# visual-studio-2015 resharper-8.0


【解决方案1】:

在我看来,您应该只使用您想要的属性作为参数来创建类,并将它的实例作为参数传递。比方法有这么多参数要好。

例如:

public void RegisterUser(UserRegisterDetail pInstanceOfMyClass)
{ ... }

你在哪里 UserRegisterDetail 是你想要传递的那些属性。

【讨论】:

    猜你喜欢
    • 2014-01-04
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多