本文介绍了如何在C#属性声明中使用Fluent样式语法糖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前从未使用过流利的代码样式.因此,这是我第一次尝试使用C#属性声明来开发流畅的样式,但是却遇到错误.谁能帮我吗?

I never used the fluent code style before. So this is hte first time I tried to develop something in the fluent style with a C# property declaration, but I get an error. Can anyone help me?

public class MailTemplate
{
    string _MailBody = "";
    public MailTemplate MailBody
    {
        get { return _MailBody; }
        set { _MailBody = value ; }
    }

    string _Subject = "";
    public MailTemplate Subject
    {
        get { return _Subject; }
        set { _Subject = value; }
    }

    string _MailFrom = "";
    public MailTemplate MailFrom
    {
        get { return _MailFrom; }
        set { _MailFrom = value; }
    }
}

请帮助我如何分配或初始化邮件正文,以后也可以使用相同的属性名称进行读取.我认为在流畅的样式开发中不能使用属性.这里需要一些照明.

Please help me how I could assign or initialize the mail body and later also can read with same property name. I think a property cannot be used in case of fluent style development. Need some light here.

推荐答案

用于MailTemplate类的流畅的生成器界面如下所示:

A fluent builder interface for the MailTemplate class would look something like this:

public class MailTemplateBuilder
{
    string _body;
    string _subject;
    string _sender;

    public MailTemplateBuilder WithBody(string body)
    {
        _body = body;
        return this;
    }

    public MailTemplateBuilder WithSubject(string subject)
    {
        _subject = subject;
        return this;
    }

    public MailTemplateBuilder WithSender(string sender)
    {
        _sender = sender;
        return this;
    }

    public MailTemplate Build()
    {
        return new MailTemplate(_sender, _subject, _body);
    }
}

用法如下:

var template = _builder.WithBody("body")
                       .WithSubject("subject")
                       .WithSender("sender")
                       .Build();


另一种方法是使用扩展方法:


Another approach would be to use extension methods:

public static class MailTemplateBuilder
{
    public static MailTemplate WithBody(this MailTemplate item, string body)
    {
        item.MailBody = body;
        return item;
    }

    public static MailTemplate WithSubject(this MailTemplate item, string subject)
    {
        item.MailSubject = subject;
        return item;
    }

    public static MailTemplate WithSender(this MailTemplate item, string sender)
    {
        item.MailFrom = sender;
        return item;
    }
}

用法现在看起来像这样:

Usage would now look like this:

var template = new MailTemplate().WithBody("body")
                                 .WithSubject("subject")
                                 .WithSender("sender");

请注意:
在这两种情况下,MailTemplate类都不会被此流畅接口的代码污染.这将是一个简单的类:

Please note:
In both cases, the MailTemplate class is not polluted with code for this fluent interface. It would be a simple class:

public class MailTemplate
{
    string _mailBody = "";
    string _subject = "";
    string _mailFrom = "";

    public string MailBody
    {
        get { return _mailBody; }
        set { _mailBody = value ; }
    }

    public string Subject
    {
        get { return _subject; }
        set { _subject = value; }
    }

    public string MailFrom
    {
        get { return _mailFrom; }
        set { _mailFrom = value; }
    }
}

因此,在使用提供的任何一种流畅接口创建该实例之后,只需访问以下属性即可简单地读取值:

So, after you created that instance with any one of the provided fluent interfaces, you can simply read the values by accessing the properties:

var body = template.MailBody;

这篇关于如何在C#属性声明中使用Fluent样式语法糖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 01:50