Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to code a good Offline-Online Dispatcher
    primarykey
    data
    text
    <p>Let's assume that I have this scenario: I have got 2 repositories of information, and I want to access both, but it would be nice to leave the task of deciding which repo to use to <strong>common class</strong>. </p> <p>The goal is to accomplish this with something <strong>similar</strong> to the code I've wrote below, but this sounds pretty bad:</p> <pre><code>where TOnline : class where TOffline : class where TContract : class </code></pre> <p>Sure I can ommit that, but bassically what I'm asking is what to do in order to stop using reflection and go <strong>typed</strong>. Maybe any <em>design-pattern</em> recomendation?</p> <p>Code (if you copy/paste this on a console app replacing the <code>Program</code> class you should be able to run the example)</p> <pre><code>using CustomerDispatcher = DispatcherProxy&lt;CustomerOnline, CustomerOffline, ICustomer&gt;; public interface ICustomer { string Get(int id); } public class CustomerOnline : ICustomer { public string Get(int id) { // Get From intranet DB return "From DB"; } } public class CustomerOffline : ICustomer { public string Get(int id) { // Get From local storage return "From local storage"; } } public class DispatcherProxy&lt;TOnline, TOffline, TContract&gt; where TOnline : class where TOffline : class where TContract : class { public TContract Instance { get; set; } public bool IsConnected { get; set; } public DispatcherProxy() { // Asume that I check if it's connected or not if (this.IsConnected) this.Instance = (TContract)Activator.CreateInstance(typeof(TOnline)); else this.Instance = (TContract)Activator.CreateInstance(typeof(TOffline)); } } class Program { static void Main(string[] args) { var customerDispatcher = new CustomerDispatcher(); Console.WriteLine("Result: " + customerDispatcher.Instance.Get(1)); Console.Read(); } } </code></pre> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload