Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8808077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:31:50+00:00 2026-06-14T02:31:50+00:00

I need some suggestions on how to build a fluent interface acting as a

  • 0

I need some suggestions on how to build a fluent interface acting as a Builder, responsible for returning different concrete types depending on the methods called.

Imagine that I need to create one of the following types using my ProductBuilder (fluently): Product, ServiceProduct, PackagedProduct (both derived from Product).

I’m thinking of using a fluent syntax like this (other suggestions are more than welcome):

To create a Product:

var product = new ProductBuilder()
   .Create("Simple product")
   .WithPrice(12.5)

To create a ServiceProduct

var product = new ProductBuilder()
   .Create("Service product")
   .WithPrice(12.5)
   .AsServiceProduct()
       .ServiceProductSpecificMethods...()

And PackagedProduct with a call to AsPackagedProduct() instead of AsServiceProduct() etc. You get the idea.

I haven’t found a sample that shows best practices on this. Only samples where the final build returns the same type.

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:31:52+00:00Added an answer on June 14, 2026 at 2:31 am

    I see two options here.

    If there are a finite number of products that are fixed, and not designed to be extended, then just create a Create method for each product:

    var product = new ProductBuilder()
       .CreateSimple()
       .WithPrice(12.5);
    
    var product = new ProductBuilder()
       .CreateService()
       .WithPrice(12.5)
       .ServiceProductSpecificMethods...();
    

    If you don’t want (or can’t have) ProductBuilder to know all of the types of products, then I would use generics:

    public class Product {}
    public class SimpleProduct : Product {}
    public class ServiceProduct : Product {}
    
    var product = new ProductBuilder<SimpleProduct>()
       .WithPrice(12.5);
    

    Here’s a starting place for the design to follow:

    public class Product
    {
        public decimal Price { get; set; }
    }
    public class SimpleProduct : Product { }
    public class ServiceProduct : Product
    {
        public string Service { get; set; }
    }
    
    public class ProductBuilder<T> where T : Product, new()
    {
        private List<Action<T>> actions = new List<Action<T>>();
    
        public T Build()
        {
            T product = new T();
            foreach (var action in actions)
            {
                action(product);
            }
    
            return product;
        }
        public void Configure(Action<T> action)
        {
            actions.Add(action);
        }
    }
    
    public static class ProductExtensions
    {
        public static ProductBuilder<T> WithPrice<T>(this ProductBuilder<T> builder, decimal price)
            where T : Product
        {
            builder.Configure(product => product.Price = price);
            return builder;
        }
    
        public static ProductBuilder<T> WithService<T>(this ProductBuilder<T> builder, string service)
                where T : ServiceProduct
        {
            builder.Configure(product => product.Service = service);
            return builder;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some asp.net pagination code samples. I would like suggestions on open source
I need to build a custom designed bar chart that displays some simple data.
I have a page I need to build out where depending on the selection
I'm working on application and I need some suggestions which is the best way
I need a little information/help/suggestions how can I build my application so I can
I am looking for some suggestions to build a query to join table A
I need to build some kind of product search and i'm not sure, which
I'm starting a Computer Vision project, and need to build an interface between a
Need some regular expressions help. So far I have my code working to allow
Need some help... I have jasperserver 4.1 installed on my ubuntu. It runs via

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.