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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:44:38+00:00 2026-05-12T13:44:38+00:00

In one of my current applications, I need to get customer data from a

  • 0

In one of my current applications, I need to get customer data from a remote service (CRM) via Webservice / SOAP. But I also want to cache the data in the mysql database, so that I dont need to connect to the webservice for the next time (data does not change often, remote service is slow and has bandwidth limit – so caching is OK / a must).

I am quite sure about the technical part of this task, but I am not so sure about how to implement this clean and transparent in my web app.

All my other data comes from a single mysql database, so I repositories which return lists or single entities queried from the database with NHibernate.

My ideas so far:


1 all in one

Use a CustomerRepository, which looks for the customer by Id, if successfull, then return it, else call the webservice and save the retrieved data to the database.

Controller looks like this:

class Controller
{
    private CustomerRepository Rep;

    public ActionResult SomeAction(int id)
    {
        return Json(Rep.GetCustomerById(id));
    }
}

Repository in pseudo / simple code like this:

class CustomerRepository 
{
    public Customer GetCustomerById(int id)
    {
        var cached = Database.FindByPK(id);
        if(cached != null) return cached;

        var webserviceData = Webservice.GetData(id);
        var customer = ConvertDataToCustomer(webserviceData);

        SaveCustomer(customer);

        return customer;
    }
}

Although the above looks somewhat straightforward, I think the CustomerRepository class will grow quite large and ugly. So I dont like that approach at all.

A repository should only load data from the database, that should be its “contract” in my app at least.


2 sepereate and sticked together in the controller

Use seperate classes for the repository (db access) and webservice (remote access) and let the controller do the work:

Controller looks like this:

class Controller
{
    private CustomerRepository Rep;
    private Webservice Service;

    public ActionResult SomeAction(int id)
    {
        var customer = Rep.GetCustomerById(id);
        if(customer != null) return Json(customer);

        var remote = Service.GetCustomerById(id);
        Rep.SaveCustomer(remote);

        return Json(remote);
    }
}

Although this looks a bit better, I still dont like to put all that logic in the controller, because error handling if the service does not return data is omitted and could probably clutter things a bit more.

Maybe I could make another service layer used by the Controller, but the code would be quite the same, but in another class.

Actually I would like to have my Controller use a single Interface/Class, which encapsulates that stuff, but I dont want one class which “does it all”: accessing the repository, accessing the webservice, saving the data… it feels somewhat wrong to me…


All ideas so far are / will probably become quite bloated with caching code, error handling, etc. I think.

Maybe I could clean up a few things using AOP?

How would you implement stuff like the above?

Technical frameworks used: ASP.NET MVC, Spring.NET for DI, NHibernate as ORM, mysql as database, the remote service is accessed via SOAP.

  • 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-12T13:44:38+00:00Added an answer on May 12, 2026 at 1:44 pm

    You can implement your architecture cleanly with the Decorator pattern.

    Let us imagine that you have an interface called ICustomerRepository.

    You first implement ICustomerRepository (let’s call it WsCustomerRepository) based on the web service, and don’t worry about the database at all in this implementation.

    You then implement another ICustomerRepository (MySqlRepository) that only talks to your database.

    Finally, you create a third ICustomerRepository (CachingRepository) that implements the caching logic you describe. It starts by querying the ‘local’ Repository, but if it doesn’t get a result from that, it queries the ‘remote’ Repository and inserts the result in the ‘local’ Repository before it returns the result.

    Your CachingRepository could look like this:

    public class CachingRepository : ICustomerRepository
    {
        private readonly ICustomerRepository remoteRep;
        private readonly ICustomerRepository localRep;
    
        public CachingRepository(ICustomerRepository remoteRep, ICustomerRepository localRep)
        {
            this.remoteRep = remoteRep;
            this.localRep = localRep;
        }
    
        // implement ICustomerRepository...
    }
    

    As you can see, the CachingRepository doesn’t even have to know about the concrete web service and database, but can concentrate on its Single Responsibility: Caching

    This gives you a clean separation of responsibility, and as far as your Controllers are concerned, they only talk to an ICustomerRepository instance.

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

Sidebar

Related Questions

I need to get the current time from one of internet time server in
I have a navigation bar and I need to always keep the current one
One of the data structures in my current project requires that I store lists
does any one know how to get the current motherboard, processor or HD temperature
How does one style links for the current page differently from others? I would
Is there any way to get HttpContext.Current.Request.Url.Host and HttpContext.Current.Request.ApplicationPath in one call? Something like
One of the functionalities in my current flex application requires me to maintain a
I want to make pipe or queue in Python between one process (current) and
In one of my current side projects, I am scanning through some text looking
For one of my current iOS project, some business logic has been provided to

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.