Fluent Interface Design in .NET: Listing 4

Full EmailMessage fluent API implementation.

EmailMessage.Fluent.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VSMFluentInterfaces.Email.Fluent;
 
namespace VSMFluentInterfaces.Email
{
    public partial class EmailMessage : IFluentEmailMessage
    {
        private IFluentEmailSender _sender;
 
        public EmailMessage(IFluentEmailSender sender)
        {
            _sender = sender;
        }
 
        public IFluentEmailMessage From(string fromAddress)
        {
            FromAddress = fromAddress;
            return this;
        }
 
        public IFluentEmailMessage To(string toAddress)
        {
            ToAddress = toAddress;
            return this;
        }
 
        public IFluentEmailMessage Saying(string message)
        {
            Message = message;
            return this;
        }
 
        public IFluentEmailMessage WithSubject(string subject)
        {
            Subject = subject;
            return this;
        }
 
        public IFluentEmailSender Done()
        {
            return _sender;
        }
    }
}

About the Author

Eric Vogel is a Senior Software Developer for Red Cedar Solutions Group in Okemos, Michigan. He is the president of the Greater Lansing User Group for .NET. Eric enjoys learning about software architecture and craftsmanship, and is always looking for ways to create more robust and testable applications. Contact him at vogelvision@gmail.com.