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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:07:53+00:00 2026-06-18T22:07:53+00:00

I’ve just read about the repository pattern in c#. It will provide an interchangeable

  • 0

I’ve just read about the repository pattern in c#. It will provide an interchangeable layer for how the actual data is accessed. That’s great. However, consider the following:

I have an XmlPersonRepository which will fetch XML data from a file and return it as a c# object (Person.cs POCO). Now I want the physical source of the XML data to be interchangeable too. Originally, it comes from a file, but it can also come from a web server or from a resource string. How would I implement this in the most reusable way? I don’t want to write something like XmlPersonFromFileRepository and a XmlPersonFromWebRepository, because it would mean code duplication. It would duplicate the code for converting the raw XML data into a c# object, but this code stays the same, no matter if i fetch XML from a file or from a web service. So it’s redundant to have the conversion code in both classes.

So in a nutshell, I want to have two abstraction layers: One layer which fetches the psysical XML data from whatever source, and another layer which converts this data into c# objects. Both should be interchangeable.

How can I implement this? And please tell me if this is a good idea or not, am I on the right track?

  • 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-18T22:07:55+00:00Added an answer on June 18, 2026 at 10:07 pm

    Simple. You just worded it in your question.

    You have two problems to solve:

    • Get XML data from a source
    • Convert XML data to C# Object

    To implement the first problem:
    – IXmlDataGetter, an interface getting a single XML data from a source.

    
    
        public interface IXmlDataGetter {
          XmlData GetData(XmlDataName name);
        }
    
    

    “XmlData” should be either a byte[] or a Stream (Because XML contains metadata with regards to the encoding of data, I think it should be kept at byte level) or be a DOM Tree (like XmlNode). You choose the solution that fits you best.

    “XmlDataName” is the way you identify a your stored data. Data has a name, however complex it may be. It may be just the string “PERSON” and the integer 25, being the id. The name of the data for the person with ID 25 may be the pair (“PERSON”, 25).

    This interface, can be implemented for DB:

    
    
        public class DBXmlDataGetter : IXmlDataGetter {
          XmlData GetData(XmlDataName name) {
            return ResultOfQuery("SELECT xml_text FROM " + name.first /* PERSON */ + " WHERE ID=" + name.second  /* 25 */); 
          }
        }
    
    

    This interface can also be implemented for a file:

    
    
        public class FileXmlDataGetter : IXmlDataGetter {
          XmlData GetData(XmlDataName name) {
            return ContentsOfFile(name.first /* PERSON */ + "_" + name.second /* 25 */ + ".xml"); 
          }
        }
    
    

    Of course “ResultOfQuery” and “ContentsOfFile” are just names of things I leave it to you to solve. And for the Web, build your URL from your XmlDataName the same way.

    Now. Second problem, converting XML to C# object. You may use an XMLDeserializer, or parse the data with an XMLReader and build your object explicitly. You just have to create a class that does te job and takes the appropriate strategy as a constructor parameter:

    
    
        public class XmlPersonRepository {
          private readonly IXmlDataGetter _getter;
    
          public PersonFetcher(IXmlDataGetter getter) {
            _getter = getter;
          }
    
          Person GetFromId(int id) {
            var xmlData = _getter.GetData(new XmlDataName("PERSON", id));
            return ConvertToPerson(xmlData); 
          }
        }
    
    

    I won’t get into the philosophical questions of IoC/Dependency injection here, but this is the basic pattern. The class that does the conversion just does the conversion. All it needs to perform the use case from end to end gets “injected” from “above”.

    You separated the responsibilities, now you’re free to get your XML data from a user copy/paste in a textbox if you wish.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this

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.