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 5978869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:34:44+00:00 2026-05-22T21:34:44+00:00

I am writing a fluent interface that is used like the following: xmlBuilder .CreateFrom()

  • 0

I am writing a fluent interface that is used like the following:

xmlBuilder
    .CreateFrom()
    .DataSet(someDataSet) //yes I said Dataset, I'm working with a legacy code
    .IgnoreSchema()
    .Build

The IgnoreSchema() method, could be WithSchema() or WithDiffGrame() in its stead. These map to the DataSet’s WriteXml() method that accepts the following enum:

  • XmlWriteMode.WriteSchema
  • XmlWriteMode.DiffGram
  • XmlWriteMode.IgnoreSchema

My fluent API is calling what amounts to a factory object of sorts that will create the XML from the Dataset. I have an abstract type that has the core functionality and then 3 derived types that reflect the various states implementing the WriteXmlFromDataSet method (I believe this approach is called the State pattern). Here is the abstract base class:

public abstract class DataSetXmlBaseFactory : IDataSetXmlFactory
{     
    ...    

    protected abstract void WriteXmlFromDataSet(XmlTextWriter xmlTextWriter);

    public XmlDocument CreateXmlDocument()
    {
        XmlDocument document = new XmlDocument();
        using (StringWriter stringWriter = new StringWriter()) 
        {
            using (XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter)) 
            {
                WriteXmlFromDataSet(xmlTextWriter);
                string content = stringWriter.ToString();
                document.LoadXml(content);
                return document;
            }
        }
    }
}

This works of course, but when I go to use this code with Dependency Injection, I run into trouble with the methods in my fluent interface mentioned at the start. The following is the implementation of those methods:

public IXmlBuild<T> WithSchema()
{
    var xmlFactory = new DataSetXmlWithSchemaFactory(this.DataSet);
    return GetIXmlBuild(xmlFactory);
}
public IXmlBuild<T> IgnoreSchema()
{
    var xmlFactory = new DataSetXmlIgnoreSchemaFactory(this.DataSet);
    return GetIXmlBuild(xmlFactory);
}
public IXmlBuild<T> WithSchemaAndDiffGram()
{
    var xmlFactory = new DataSetXmlWithDiffGramFactory(this.DataSet);
    return GetIXmlBuild(xmlFactory);
}
private static IXmlBuild<T> GetIXmlBuild(IDataSetXmlFactory xmlFactory)
{
    string content = xmlFactory.CreateXmlDocument().InnerXml;
    return new clsXmlDataSetBuild<T>(content);
}

Right now I’m not using Dependency Injection (DI) as I’m newing up the dependent IDataSetXMLFactory objects. If I changed the code to utilize DI, how would the class know which implementation of IDataSetXmlFactory to use? If I understand DI correctly, that decision would need to be made higher up the call stack (specifically at the Composition Root), but up there the code wouldn’t know which exact implementation is needed. If I used the DI container to resolve (locate) the needed implementation in the above methods, then I would be using the DI container as a Service Locator, which is considered an anti-pattern.

At this point, it would be much easier to just pass an enum to the xmlFactory.CreateXmlDocument() method on the IXmlDataSetFactory instance. This certainly is much easier and is less code, but I’m sure this problem has been faced before with State patterns and DI. What is the way to deal with this? I’m new to DI and have started reading Dependency Injection in .NET, but have yet to read anything on this specific problem.

Hopefully, I’m just missing a small piece to the puzzle.


Update (based on Mark Seemann’s answer)

What would the Semantic Model for the interface below look like? Examples would be appreciated.

public interface IXmlBuilder<T>
{
    IXmlSourceContent<T> CreateFrom();
}

public interface IXmlSourceContent<T>
{
    IXmlOptions<T> Object(T item);
    IXmlOptions<T> Objects(IEnumerable<T> items);
    IXmlDataSetOptions<T> DataSet(T ds);
    IXmlBuild<T> InferredSchema();
}

public interface IXmlOptions<T> : IXmlBuild<T>
{
    IXmlBuild<T> WithInferredSchema();
}

public interface IXmlDataSetOptions<T> : IXmlDataSetSchema<T>
{
    IXmlDataSetSchema<T> IncludeTables(DataTableCollection tables);
    IXmlDataSetSchema<T> IncludeTable(DataTable table);
}

public interface IXmlBuild<T>
{
    XmlDocument Build();
}

public interface IXmlDataSetSchema<T>
{
    IXmlBuild<T> WithSchemaAndDiffGram();
    IXmlBuild<T> WithSchema();
    IXmlBuild<T> IgnoreSchema();
}

In addition to IDataSetXMLFactory mentioned above, I also have the following extension methods:

static class XmlDocumentExtensions
{    
    [Extension()]
    public static void InsertSchema(XmlDocument document, XmlSchema schema)
    {
       ...    
    }    
}

static class XmlSchemaExtensions
{    
    [Extension()]
    public static string ToXmlText(XmlSchema schema)
    {
       ...    
    }    
}

and these classes:

public class XmlFactory<T>
{
    ...

    public XmlFactory(IEnumerable<T> objects)
    {
        this.Objects = objects;
    }

    public XmlDocument CreateXml()
    {
        // serializes objects to XML
    }
}

public class XmlSchemaFactory<T> : IXmlSchemaFactory<T>
{
    public XmlSchema CreateXmlSchema()
    {
        // Uses reflection to build schema from type
    }
}
  • 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-05-22T21:34:44+00:00Added an answer on May 22, 2026 at 9:34 pm

    It seems to me that you are discovering the limits of defining a Fluent API in terms of the object model targeted by the API. As Jeremy Miller points out, it’s often better to let the Fluent API build a Semantic Model which can then be used to construct the desired object graph.

    This is an experience I share, and which I find helps bridge the apparent gap between Fluent APIs and DI.


    Based on the original Fluent API presented, the Semantic Model might be something as simple as this:

    public class MySemanticModel
    {
        public DataSet DataSet { get; set; }
        public bool IgnoreSchema { get; set; }
        // etc...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was working on (for fun) writing a script that would recognize palindromes. So
Writing a Jabber client that uses Twisted Words. I would like to know an
Writing a JSP page, what exactly does the <c:out> do? I've noticed that the
Writing something like this using the loki library , typedef Functor<void> BitButtonPushHandler; throws a
Writing documentation in html requires some code examples. What to do with characters that
Writing some drag&drop code, I'd like to cancel the click events in my mouseup
Writing some classes for a Framework extension, and I have the following code: public
Writing a simple example from Odersky's book resulted in the following problem: // AbstractElement.scala
Writing a sound control applet with GUI that communicates with a device via USB.
Writing an app that will include the ability to decompress zip and rar files.

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.