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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:21:14+00:00 2026-06-10T18:21:14+00:00

I have 2 projects : The first project is a structure project which i

  • 0

I have 2 projects : The first project is a structure project which i read from an xml file.
This project is used in other solutions
The second project(1 of the other solutions) work on the structure project with foreach running on components list:

namespace FriendProject.Workers
{
    public class Worker
    {
        static void Main(string[] args)
        {
            foreach (Component component in ComponentList)
            {
                DoWork(component);
            }
        }
    }
}    

Today the DoWork method does the following:

public void DoWork(Component component)
{
   // Doing work on component properties
   // Zip component files with open source Zipper
   if (component is DBComponent)
   {
      // work on DBComponent properties
    }
}  

Now if you are familiar with design patterns then you can see that there is an injection point here and the following should be done :

public class Component
    {
        public virtual void DoWork()
        {
            // Do work
        }
    }

    public class DBComponent : Component
    {
        public override void DoWork()
        {
            base.DoWork();
            // injection point - work on DBComponent properties
        }
    }

    public class Operator
    {
        static void Main(string[] args)
        {
            foreach (Component component in ComponentList)
            {
                component.DoWork();
            }
        }
    }

The problem is that the project which holds the Component and DBComponent is a structure project which is used in other solutions and with other projects and I need to add the open source Zip dll to the project and it becomes more tightly coupled to the current project(“FriendProject”) and less usable. Not to talk about that the other projects will never use these methods(DoWork in Component and DBComponent)

Is there a better solution without changing much the design? Should I add an adpater?
If so please provide and example.
Thanks to all

Edit: Short Question
2 projects :
One is a manager project which acts on the second project.
Second is a structure project(read data from xml) which is reused with other projects.
I want to add methods and refernces(since of polymorphism) in the structure project(second project). However it feels wrong since the other projects that uses it will never use those methods and the added references.
Is there a better solution for how to do it?

Edit :
Removed the structure project code the shorten the question. this code was irrelavent since its classes(Component and DBComponent) appear next.

  • 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-10T18:21:16+00:00Added an answer on June 10, 2026 at 6:21 pm

    Easy (and with three different GOF design patterns).

    Since we can’t do anything with the components, we’ll have to use the bridge pattern.

    Let’s define handlers:

    public interface IHandlerOf<T> where T : Component
    {
        void DoWork(T component);
    }
    

    So now we can create a handler type for each component type that we want to handle. A DB component handler would look like this:

    public class DbComponentHandler : IHandlerOf<DbComponent>
    {
        public void DoWork(DbComponent component)
        {
            // do db specific information here
        }
    }
    

    But since we don’t really want to keep track of all handlers we’ll want to create a class that does it for us. We ultimately want to invoke the code just as in your example:

    foreach (Component component in ComponentList)
    {
        handler.DoWork(component);
    }
    

    But let’s make it a bit cooler:

    //maps handlers to components
    var service = new ComponentService();
    
    // register all handlers in the current assembly
    service.Register(Assembly.GetExecutingAssembly());
    
    // fake a component
    var dbComponent = new DbComponent();
    
    // the cool part, the invoker doesn't have to know
    // about the handlers = facade pattern
    service.Invoke(dbComponent);
    

    The service with makes it possible looks like this:

    public class ComponentService
    {
        private readonly Dictionary<Type, IHandlerInvoker> _handlers = new Dictionary<Type, IHandlerInvoker>();
    
        public void Register(Assembly assembly)
        {
            foreach (var type in assembly.GetTypes())
            {
                if (type.IsInterface)
                    continue;
    
                foreach (var interfaceType in type.GetInterfaces())
                {
                    if (!interfaceType.IsGenericType || interfaceType.GetGenericTypeDefinition() != typeof(IHandlerOf<>))
                        continue;
    
                    var componentType = interfaceType.GetGenericArguments()[0];
                    var instance = Activator.CreateInstance(type);
                    var method = instance.GetType().GetMethod("DoWork", new[] { componentType });
    
                    _handlers[componentType] = new ReflectionInvoker(instance, method);
                }
            }
        }
    
        public void Register<T>(IHandlerOf<T> handler) where T : Component
        {
            _handlers[typeof (T)] = new DirectInvoker<T>(handler);
        }
    
        #region Nested type: DirectInvoker
    
        private class DirectInvoker<T> : IHandlerInvoker where T : Component
        {
            private readonly IHandlerOf<T> _handler;
    
            public DirectInvoker(IHandlerOf<T> handler)
            {
                _handler = handler;
            }
    
            #region IHandlerInvoker Members
    
            public void Invoke(Component component)
            {
                _handler.DoWork((T) component);
            }
    
            #endregion
        }
    
        #endregion
    
        #region Nested type: IHandlerInvoker
    
        private interface IHandlerInvoker
        {
            void Invoke(Component component);
        }
    
        #endregion
    
        #region Nested type: ReflectionInvoker
    
        private class ReflectionInvoker : IHandlerInvoker
        {
            private readonly object _instance;
            private readonly MethodInfo _method;
    
            public ReflectionInvoker(object instance, MethodInfo method)
            {
                _instance = instance;
                _method = method;
            }
    
            #region IHandlerInvoker Members
    
            public void Invoke(Component component)
            {
                _method.Invoke(_instance, new object[] {component});
            }
    
            #endregion
        }
    
        #endregion
    
        public void Invoke(Component component)
        {
            IHandlerInvoker invoker;
            if (!_handlers.TryGetValue(component.GetType(), out invoker))
                throw new NotSupportedException("Failed to find a handler for " + component.GetType());
    
            invoker.Invoke(component);
        }
    }
    

    Do note that the interface (IHandlerOf<T>) is generic which means that we can’t store it directly in a Dictionary. Hence we use the Adapter pattern to store all handlers.


    Full example:

    public interface IHandlerOf<in T> where T : Component
    {
        void DoWork(T component);
    }
    
    
    public class ComponentService
    {
        private readonly Dictionary<Type, IHandlerInvoker> _handlers = new Dictionary<Type, IHandlerInvoker>();
    
        public void Register(Assembly assembly)
        {
            foreach (var type in assembly.GetTypes())
            {
                if (type.IsInterface)
                    continue;
    
                foreach (var interfaceType in type.GetInterfaces())
                {
                    if (!interfaceType.IsGenericType || interfaceType.GetGenericTypeDefinition() != typeof(IHandlerOf<>))
                        continue;
    
                    var componentType = interfaceType.GetGenericArguments()[0];
                    var instance = Activator.CreateInstance(type);
                    var method = instance.GetType().GetMethod("DoWork", new[] { componentType });
    
                    _handlers[componentType] = new ReflectionInvoker(instance, method);
                }
            }
        }
    
        public void Register<T>(IHandlerOf<T> handler) where T : Component
        {
            _handlers[typeof (T)] = new DirectInvoker<T>(handler);
        }
    
        #region Nested type: DirectInvoker
    
        private class DirectInvoker<T> : IHandlerInvoker where T : Component
        {
            private readonly IHandlerOf<T> _handler;
    
            public DirectInvoker(IHandlerOf<T> handler)
            {
                _handler = handler;
            }
    
            #region IHandlerInvoker Members
    
            public void Invoke(Component component)
            {
                _handler.DoWork((T) component);
            }
    
            #endregion
        }
    
        #endregion
    
        #region Nested type: IHandlerInvoker
    
        private interface IHandlerInvoker
        {
            void Invoke(Component component);
        }
    
        #endregion
    
        #region Nested type: ReflectionInvoker
    
        private class ReflectionInvoker : IHandlerInvoker
        {
            private readonly object _instance;
            private readonly MethodInfo _method;
    
            public ReflectionInvoker(object instance, MethodInfo method)
            {
                _instance = instance;
                _method = method;
            }
    
            #region IHandlerInvoker Members
    
            public void Invoke(Component component)
            {
                _method.Invoke(_instance, new object[] {component});
            }
    
            #endregion
        }
    
        #endregion
    
        public void Invoke(Component component)
        {
            IHandlerInvoker invoker;
            if (!_handlers.TryGetValue(component.GetType(), out invoker))
                throw new NotSupportedException("Failed to find a handler for " + component.GetType());
    
            invoker.Invoke(component);
        }
    }
    
    public class DbComponent : Component
    {
    }
    
    public class DbComponentHandler : IHandlerOf<DbComponent>
    {
        public void DoWork(DbComponent component)
        {
            // do db specific information here
            Console.WriteLine("some work done!");
        }
    }
    
    
    internal class Program
    {
        private static void Main(string[] args)
        {
            var service = new ComponentService();
            service.Register(Assembly.GetExecutingAssembly());
    
            var dbComponent = new DbComponent();
            service.Invoke(dbComponent);
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a directory structure that looks like this: project/ __init__.py foo/ __init.py__ first.py
This is my first windows application. I have published my project and here are
I have two CVS projects, which I maintain in Eclipse. I check out first
I have my first CQRS project which uses event sourcing and I was wondering
I have multiple projects with directory structure like this: /application/ /images/ /js/ /css/ /system/
First I will explain the structure of my project: I have a WCF-service public
First of all, I have read through many S.O. questions regarding this topic and
I am trying to write my first WCF project. I have created a project
I have 2 projects in my solution. The first one is a console application,
I think I have a big problem. I have a two projects solution. First

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.