Search code examples
c#fluent

How to use Fluent style syntactic sugar with c# property declaration


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.


Solution

  • 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);
        }
    }
    

    Usage would look like this:

    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");
    

    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
    {
        public string MailBody { get; set; } = "";
        public string Subject { get; set; } = "";
        public string MailFrom { get; set; } = "";
    }
    

    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;