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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:39:19+00:00 2026-05-14T06:39:19+00:00

I built a .NET ASMX web service connecting to an SQL Server database. There

  • 0

I built a .NET ASMX web service connecting to an SQL Server database. There is a web service call GetAllQuestions().

 var myService = new SATService();
 var serviceQuestions = myService.GetAllQuestions();

I saved the result of GetAllQuestions to GetAllQuestions.xml in the local application folder

Is there any way to fake the web service call and use the local xml result?

I just want to take the contents of my entire sql table and have the array of objects with correlating property names automatically generated for me just like with LINQ to SQL web services.

Please keep in mind that I am building a standalone Monotouch iPhone application.

  • 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-14T06:39:20+00:00Added an answer on May 14, 2026 at 6:39 am

    Use dependency injection.

    //GetSATService returns the fake service during testing     
    var myService = GetSATService(); 
    var serviceQuestions = myService.GetAllQuestions();
    

    Or, preferably, in the constructor for the object set the SATService field (so the constructor requires the SATService to be set. If you do this, it will be easier to test.

    Edit: Sorry, I’ll elaborate here. What you have in your code above is a coupled dependency, where your code creates the object it is using. Dependency injection or the Inversion of Control(IOC) pattern, would have you uncouple that dependency. (Or simply, don’t call “new” – let something else do that – something you can control outside the consumer.)

    There are several ways to do this, and they are shown in the code below (comments explain):

    class Program
    {
        static void Main(string[] args)
        {
            //ACTUAL usage
            //Setting up the interface injection
            IInjectableFactory.StaticInjectable = new ConcreteInjectable(1);
    
            //Injecting via the constructor
            EverythingsInjected injected = 
                new EverythingsInjected(new ConcreteInjectable(100));
    
            //Injecting via the property
            injected.PropertyInjected = new ConcreteInjectable(1000);
    
            //using the injected items
            injected.PrintInjectables();
            Console.WriteLine();
    
            //FOR TESTING (normally done in a unit testing framework)
            IInjectableFactory.StaticInjectable = new TestInjectable();
            EverythingsInjected testInjected = 
                new EverythingsInjected(new TestInjectable());
            testInjected.PropertyInjected = new TestInjectable();
            //this would be an assert of some kind
            testInjected.PrintInjectables(); 
    
            Console.Read();
        }
    
        //the inteface you want to represent the decoupled class
        public interface IInjectable { void DoSomething(string myStr); }
    
        //the "real" injectable
        public class ConcreteInjectable : IInjectable
        {
            private int _myId;
            public ConcreteInjectable(int myId) { _myId = myId; }
            public void DoSomething(string myStr)
            {
                Console.WriteLine("Id:{0} Data:{1}", _myId, myStr);
            }
        }
    
        //the place to get the IInjectable (not in consuming class)
        public static class IInjectableFactory
        {
            public static IInjectable StaticInjectable { get; set; }
        }
    
        //the consuming class - with three types of injection used
        public class EverythingsInjected
        {
            private IInjectable _interfaceInjected;
            private IInjectable _constructorInjected;
            private IInjectable _propertyInjected;
    
            //property allows the setting of a different injectable
            public IInjectable PropertyInjected
            {
                get { return _propertyInjected; }
                set { _propertyInjected = value; }
            }
    
            //constructor requires the loosely coupled injectable
            public EverythingsInjected(IInjectable constructorInjected)
            {
                //have to set the default with property injected
                _propertyInjected = GetIInjectable();
    
                //retain the constructor injected injectable
                _constructorInjected = constructorInjected;
    
                //using basic interface injection
                _interfaceInjected = GetIInjectable();
            }
    
            //retrieves the loosely coupled injectable
            private IInjectable GetIInjectable()
            {
                return IInjectableFactory.StaticInjectable;
            }
    
            //method that consumes the injectables
            public void PrintInjectables()
            {
                _interfaceInjected.DoSomething("Interface Injected");
                _constructorInjected.DoSomething("Constructor Injected");
                _propertyInjected.DoSomething("PropertyInjected");
            }
        }
    
        //the "fake" injectable
        public class TestInjectable : IInjectable
        {
            public void DoSomething(string myStr)
            {
                Console.WriteLine("Id:{0} Data:{1}", -10000, myStr + " For TEST");
            }
        }
    

    The above is a complete console program that you can run and play with to see how this works. I tried to keep it simple, but feel free to ask me any questions you have.

    Second Edit:
    From the comments, it became clear that this was an operational need, not a testing need, so in effect it was a cache. Here is some code that will work for the intended purpose. Again, the below code is a full working console program.

    class Program
    {
        static void Main(string[] args)
        {
            ServiceFactory factory = new ServiceFactory(false);
            //first call hits the webservice
            GetServiceQuestions(factory);
            //hists the cache next time
            GetServiceQuestions(factory);
            //can refresh on demand
            factory.ResetCache = true;
            GetServiceQuestions(factory);
            Console.Read();
        }
    
        //where the call to the "service" happens
        private static List<Question> GetServiceQuestions(ServiceFactory factory)
        {
            var myFirstService = factory.GetSATService();
            var firstServiceQuestions = myFirstService.GetAllQuestions();
            foreach (Question question in firstServiceQuestions)
            {
                Console.WriteLine(question.Text);
            }
            return firstServiceQuestions;
        }
    }
    
    //this stands in place of your xml file
    public static class DataStore
    {
        public static List<Question> Questions;
    }
    
    //a simple question
    public struct Question
    {
        private string _text;
        public string Text { get { return _text; } }
        public Question(string text)
        {
            _text = text;
        }
    }
    
    //the contract for the real and fake "service"
    public interface ISATService
    {
        List<Question> GetAllQuestions();
    }
    
    //hits the webservice and refreshes the store
    public class ServiceWrapper : ISATService
    {
        public List<Question> GetAllQuestions()
        {
            Console.WriteLine("From WebService");
            //this would be your webservice call
            DataStore.Questions = new List<Question>()
                       {
                           new Question("How do you do?"), 
                           new Question("How is the weather?")
                       };
            //always return from your local datastore
            return DataStore.Questions;
        }
    }
    
    //accesses the data store for the questions
    public class FakeService : ISATService
    {
        public List<Question> GetAllQuestions()
        {
            Console.WriteLine("From Fake Service (cache):");
            return DataStore.Questions;
        }
    }
    
    //The object that decides on using the cache or not
    public class ServiceFactory
    {
        public  bool ResetCache{ get; set;}
        public ServiceFactory(bool resetCache)
        {
            ResetCache = resetCache;
        }
        public ISATService GetSATService()
        {
            if (DataStore.Questions == null || ResetCache)
                return new ServiceWrapper();
            else
                return new FakeService();
        }
    }
    

    Hope this helps. Good luck!

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

Sidebar

Related Questions

We have built a .net (asp) web based solution (SQL Server database) for a
I'm building a traditional ASP.NET Web Service -- the style built using asmx. It's
I have built a regular .NET asmx service. How do i overload web methods
I have created a new VS2008 ASP.Net Web service project, with the default name
I am new to .NET. I need to create a Web Service based on
I am trying to hit the following web service with axis2: http://www.webservicex.net/geoipservice.asmx?WSDL I have
I have a ASP.NET web service ( .asmx ) with methods that receives int
I built a simple asp.net web service. It works when I put the URL
I have a web service built on .NET which returns XmlDocument. I'm reading it
I have a asp.net web site built with .net 2.0 framework. we recently purchased

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.