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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:59:22+00:00 2026-06-11T12:59:22+00:00

Ok, first I’ll describe my goal , then what I’ve coded , finally questions

  • 0

Ok, first I’ll describe my goal, then what I’ve coded, finally questions.

Goal

To have a generic class that manage multiple contracts that , and is able to find out wheter it’s an online or offline situation, on the very moment when an operation is being made. There’s a really easy way of doing it: a class for each Online-Offline pair that implement the contract and check on every each method wheter if it’s online or not, and makes the right call. And that’s exactly what I want to avoid.

Just FYI, behind the scenes it would be an Online scenario connected to WCF services and an Offline scenario connected to a client local database.

FYI 2: I’ve tried to accomplish this avoiding Interception and AOP stuff, but I found a dead end. You can see this post where I implement what seems to be a good solution, but stablishes if it’s connected or not on the contructor, but real-world scenario needs this check at Operation level, not constructor level.

Code

It’s ready to run & test: just copy/paste on a new console application.

using System;
using System.Reflection;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;

namespace ConsoleApplication1
{
    public class Unity
    {
        public static IUnityContainer Container;

        public static void Initialize()
        {
            Container = new UnityContainer();

            Container.AddNewExtension<Interception>();
            Container.RegisterType<ILogger, OnlineLogger>();
            Container.Configure<Interception>().SetInterceptorFor<ILogger>(new InterfaceInterceptor());
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Unity.Initialize();

            var r = new Router<ILogger, OnlineLogger, OfflineLogger>();

            try
            {
                r.Logger.Write("Method executed.");
            }
            catch (CantLogException ex)
            {
                r.ManageCantLogException(ex);
            }

            Console.ReadKey();
        }
    }

    public class Router<TContract, TOnline, TOffline>
        where TOnline : TContract, new()
        where TOffline : TContract, new()
    {
        public TContract Logger;

        public Router()
        {
            Logger = Unity.Container.Resolve<TContract>();
        }

        public void ManageCantLogException(CantLogException ex)
        {
            // Is this an ugly trick? I mean, the type was already registered with online.
            Unity.Container.RegisterType<TContract, TOffline>();
            Logger = Unity.Container.Resolve<TContract>();

            var method = ((MethodBase)ex.MethodBase);
            method.Invoke(Logger, ex.ParameterCollection);
        }
    }

    public interface ILogger
    {
        [Test]
        void Write(string message);
    }

    public class OnlineLogger : ILogger
    {
        public static bool IsOnline()
        {
            // A routine that check connectivity
            return false;
            // Should I perform a "lock" here to make this tread safe?
        }

        public void Write(string message)
        {
            Console.WriteLine("Logger: " + message);
        }
    }

    public class OfflineLogger : ILogger
    {
        public void Write(string message)
        {
            Console.WriteLine("Logger: " + message);
        }
    }

    [System.Diagnostics.DebuggerStepThroughAttribute()]
    public class TestAttribute : HandlerAttribute
    {
        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            return new TestHandler();
        }
    }

    public class TestHandler : ICallHandler
    {
        public int Order { get; set; }
        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            Console.WriteLine("It's been intercepted.");

            if (!OnlineLogger.IsOnline() && input.Target is OnlineLogger)
            {
                Console.WriteLine("It's been canceled.");
                throw new CantLogException(input.MethodBase, input.Inputs);
            }
            return getNext()(input, getNext);
        }
    }

    public class CantLogException : Exception
    {
        public MethodBase MethodBase { get; set; }

        public object[] ParameterCollection { get; set; }

        public CantLogException(string message)
            : base(message)
        {
        }

        public CantLogException(MethodBase methodBase, IParameterCollection parameterCollection)
        {
            this.MethodBase = methodBase;

            var parameters = new object[parameterCollection.Count];

            int i = 0;
            foreach (var parameter in parameterCollection)
            {
                parameters[i] = parameter;
                i++;
            }

            this.ParameterCollection = parameters;
        }
    }
}

Questions

  1. Is this design that I present thread-safe?

  2. Is it a performance disgrace? The exception handling the Online-Offline situation and reflection there makes me fell I’m doing all wrong.

  3. I think this is a common requeriment, isn’t there any api/fwk/whatever that does this Online-Offline management? Kind of feel like I’m reinventing the weel here.

  4. This is a long shot question: I really don’t want client (Program class on this example) to know about the exception, isn’t there any other way to cancel method execution but through an exception on the interceptor? Any other approach is welcomed too.


I’m not interested on paid third-party stuff, so sadly things like PostSharp aren’t options for me.

  • 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-11T12:59:23+00:00Added an answer on June 11, 2026 at 12:59 pm

    I would like to suggest another possible approach. It’s rather different than what you’re planning, but might work out better.

    The basic idea is to split your app into two parts. The first part is what you’re describing – the UI. However, it’s written assuming you’re offline. It stores everything into a local database/file/memory store.

    You then have another part of the program, probably on another thread (or possibly in another process) which is responsible for the synchronization. It looks at the data store, waiting for changes, and if online it writes them to the server. When updates come down from the server the data store will need to raise events to tell the main program something has changed, but it would have had to do that anyway. If you’re offline, the synchronizer doesn’t do anything, but all the data changes are being safely squirreled away automatically until you are back online later.

    The nice thing about this approach is you never need to worry about the online/offline switch anywhere except the part of the code specifically dealing with synchronization. By just assuming you’re offline you’ve already set up the default case. You can get rid of all the interception / AOP stuff completely.

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

Sidebar

Related Questions

First of all I have seen that there are many questions about unrecognized selector
First you should know I have looked into many questions and none of them
First, I have a dbcontext factory which is defined public class DatabaseFactory : Disposable,
First, I have a list of QWidget s that I won't know the length
First off I have an app that provides the user information about the amount
First off, I am using Windows XP. I have multiple hard drives and it
First I will show you example tables that my issue pertains to, then I
First of all, I'm uploading multiple images that goes through a for loop by
first take a look on this picture from localScope app : i have 2
First i selected images from photo library to ALAsset Library and after that i

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.