How can I simplify C# code that sets multiple properties of an object?

可紊 提交于 2019-12-20 17:17:25

问题


I have code that looks like this:

itemView.Question.AnswersJSON = itemView.Answer.ToJSONString();
itemView.Question.Modified = DateTime.Now;
itemView.Question.ModifiedBy = User.Identity.Name

plus many more lines where I set values for the Question class that is inside the itemView.

I think the answer is "not possible" but just putting it out as a question in case anyone knows a way.

What I would like to do is to find a way to simplify this code without repeating itemView.Question in every line.


回答1:


If Question is a class (not a struct), then you could assign it to a local variable, and edit that:

Question q = itemView.Question;
q.AnswersJSON = itemView.Answer.ToJSONString();
q.Modified = DateTime.Now;
q.ModifiedBy = User.Identity.Name

You won't even have to assign q back to itemView.Question.

This is because classes in C# are reference types. If you assign an instance of a reference type to a local variable, or pass it to a function, then changes to that instance will be reflected everywhere you have a reference to that same instance.

Edit

Note that the situation might be a bit murky if Question is a property of itemView, rather than a field. Depending on how it is implemented, you might have to assign q back to Question. In such a case, this code is still much preferred to avoid calling the Question property's getter method repeatedly.




回答2:


Do you instantiate itemView.Question as part of your method?

If so you could do:-

itemView.Question = new ItemViewQuestion()
{
  AnswersJSON = itemView.Answer.ToJSONString(),
  Modified = DateTime.Now,
  ModifiedBy = User.Identity.Name
};



回答3:


One option is that you can convert your properties into methods that return 'this'.

Then you could write:

itemView.Question
    .AnswersJSON(itemView.Answer.ToJSONString())
    .Modified(DateTime.Now)
    .ModifiedBy(User.Identity.Name);

I've heard this style called 'fluent interface', and find it pretty handy. I sometimes create properties and a matching set methods returning 'this' called SetXXXX to compliment them.

The popular Rhino Mocks framework for unit testing uses it. More examples here: http://www.codeproject.com/Articles/99542/Guidelines-to-Fluent-Interface-design-in-C-Part-1




回答4:


If that Question is a class, then you could shorten the code a bit:

    var q = itemView.Question;
    q.AnswersJSON = itemView.Answer.ToJSONString();
    q.Modified = DateTime.Now;
    q.ModifiedBy = User.Identity.Name



回答5:


Depending on how much control you have over the Question class, separating the resposibility for setting that meta-data may be an idea:

class Question {
    ...
    public void SetAnswer(Answer answer) {
        this.AnswersJSON = answer.ToJSONString();
        this.Modified = DateTime.Now;
        this.Modified = User.Identity.Name; // or pass the user into SetAnswer()
    }
}

// in your UI code:
itemView.Question.SetAnswer(itemView.Answer);


来源:https://stackoverflow.com/questions/6729701/how-can-i-simplify-c-sharp-code-that-sets-multiple-properties-of-an-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!