-1

i got few line of code where configure method is calling and passing just the string value but ConfigureWith function expect delegate. can anyone help me to understand how ConfigureWith() method will work. thanks

MailTemplate
.ConfigureWith(mt => mt.MailBody = "hello world")
.ConfigureWith(mt => mt.MailFrom = "rdingwall@gmail.com")
.DoSomeOtherStuff()
.Build();

The implementation for this would be:

public class MailTemplate
{
// regular auto properties
public string MailFrom { get; set; }
public string MailBody { get; set; }

public MailTemplate ConfigureWith(Action<MailTemplate> func)
{
    func(this);
    return this;
}


}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Thomas
  • 33,544
  • 126
  • 357
  • 626

2 Answers2

3

As written it seems entirely pointless, you may as well just set the property on the MailTemplate directly.

Typically in a fluent builder like this you would save the Actions being passed in with each ConfigureWith call and then execute them later.

It might help if you explain in more detail what you hope to achieve with the fluent syntax you are creating. As written it also will not compile since the first call expects a static method. Can you show the real code?

You might also want to look at other answers on StackOverflow regarding fluent methods (e.g. Tips for writing fluent interfaces in C# 3)

Community
  • 1
  • 1
Ian Mercer
  • 38,490
  • 8
  • 97
  • 133
  • i am not interested abou fluent rather i want to know how ConfigureWith() function works when string data pass like "hello world" can u plzz explain. thanks – Thomas Dec 10 '12 at 08:15
0

I see that the example above is from an answer to a different question you asked earlier. I'm still not entirely sure what you're trying to do, and as Ian Mercer suggested, it's pretty pointless as written. But if you're just trying to understand what it does, then let's first get a working example:

using System;

namespace ScratchApp
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var mailTemplate = BuildMailTemplate(
                mt => mt.MailBody = "hello world", 
                mt => mt.MailFrom = "rdingwall@gmail.com");
        }

        private static MailTemplate BuildMailTemplate(
            Action<MailTemplate> configAction1, 
            Action<MailTemplate> configAction2)
        {
            var mailTemplate = new MailTemplate();
            mailTemplate.ConfigureWith(configAction1)
                .ConfigureWith(configAction2)
                .DoSomeOtherStuff()
                .Build();
            return mailTemplate;
        }
    }

    public class MailTemplate
    {
        public string MailFrom { get; set; }
        public string MailBody { get; set; }

        public MailTemplate DoSomeOtherStuff()
        {
            // Do something
            return this;
        }

        public MailTemplate Build()
        {
            // Build something
            return this;
        }

        public MailTemplate ConfigureWith(Action<MailTemplate> func)
        {
            func(this);
            return this;
        }
    }
}

This is as pointless as before, but it builds. What's happening when you're calling .ConfigureWith() is that instead of passing a normal value, you're passing it a function. In the example above, I'm actually declaring the functions as parameters that get passed into the BuildMailTemplate() method, and which in turn get executed when the template is being built and configured. You can get a feel for how it works by stepping through the code, line-by-line (e.g., F11 in Visual Studio), and setting breakpoints in the lambda expressions themselves, and then looking at the call stack.

If you're confused about the syntax for lambdas - arrow syntax is indeed a bit complex when you're first getting used to it - then feel free to check out the MSDN article on it, or just Google "c# lambdas" to your heart's delight.

Community
  • 1
  • 1
Ken Smith
  • 20,305
  • 15
  • 100
  • 147