1

I have a 'window forms application' project and it has a reference to class library lets say LibA. Now LibA wants to access some data of this 'window forms application' project, how to do it now ? I cant add reference of this 'window forms application' project to LibA because of cylic dependency thing.

Uwe Keim
  • 36,867
  • 50
  • 163
  • 268
user2968369
  • 255
  • 4
  • 10

3 Answers3

2

You can not do both things referring LibA in 'window forms application' and referring 'window forms application' in LibA. You can add reference in one way only.

If you are in such a situation that means your project architecture is not well designed. So to over come this situation you should design it in such a manner so that you don't face this situation.

More over you can refer below link in which Cyclic Dependency and how to over come it has been explained very well

What is Circular dependency and how do we resolve it

SpiderCode
  • 9,664
  • 1
  • 19
  • 41
0

A Class library should never reference a Windows Forms (an application level proejct)

The issue you're facing is related to software design (or software architecture)

If the class library needs something from the Windows Forms it means that the windows forms project is doing something it is not responsible for.

You have two solutions and both imply redesign

  • Pass the code from the windows forms to your class library or
  • Create a new class library project, put the 'common' code there and reference it in both projects

I strongly advise you to read about system architecture

SOLID Design

Multitier applications. an example with asp.net

EDIT

Nevertheless, if you are aware of its dangerous, you could reference the windows forms dll itself from the output bin directory instead of the Project. Attention that is very bad code and done only in extremely rare cases.

Second choice, also not recommended, is to use reflection to access the windows forms dll. You can use Assembly.Load() and then invoke or get property values in run-time.

Given your (perhaps) level of expertise i'd strongly maintain the recommendation of learning more about system architecture.

Luis Filipe
  • 7,762
  • 6
  • 42
  • 72
  • "A Class library should never reference a Windows Forms" what if we need to update the button,checkbox etc.from another class while form already using that class methods? – Deniz Jul 03 '20 at 08:18
  • The idea is that a library has no dependency on UI. It doesn't know anything about buttons or checkboxes. I see 2 options: a) If you need your library to change something in the UI you can create an interface next to the library and make it a dependency of the class. The UI code will implement that interface and pass it to the library b) use events – Luis Filipe Jul 03 '20 at 17:06
0

Here's quick and dirty a trick to bypass cyclic dependency issues (nevertheless, I agree with SpiderCode to say that this should not happen with a good architecture).

  • In your assembly LibA, define:

    public interface IMyUi
    {
        // put whatever functions you need to access from LibA
        string GetData();
    }
    
    public static class MyUiProvider
    {
        public static IMyUi MyUi;
    }
    
  • In your assembly LibB, just make the class you want implement IMyUi, and assign MyUiProvider.MyUi on program load

Once this is done, you can use your UI via MyUiProvider.MyUi.GetData().

NB: I put that as static to have quick example of the pattern. This might not be a good practice: static keeps your UI in gc roots, and it forbids you to have multiple instances... but you can transpose the idea to instance injection of the interface.

NB 2: this pattern is known as "inversion of control"

Olivier
  • 5,088
  • 1
  • 25
  • 42
  • For better understanding this is called 'dependency injection' which enables you to do 'inversion of control' – Luis Filipe Nov 08 '13 at 10:54
  • thanks Luis and Olivier for such elaborate responses. really appreciate it :). I am looking into your proposed solutions – user2968369 Nov 08 '13 at 12:05