Results 1 to 9 of 9

Thread: Using Interfaces to connect libraries

  1. #1

    Thread Starter
    Hyperactive Member jasonwucinski's Avatar
    Join Date
    Mar 2010
    Location
    Pittsburgh
    Posts
    452

    Using Interfaces to connect libraries

    Hello,
    I have one project with multiple libraries. The problem I have is that I can’t access some libraries from others, as when I add a reference I get an error about circular dependencies. I assume I need to create an interface but I am not really sure how to structure it. My references are setup like this:

    1. WebApp (This is an ASP.Net web site) - Has Reference to BusinessLayer Class Library and Utilities Class Library
    2. Data Class Library (This library provides abstract database functionality) - has no references
    3. BusinessLayer Class Library (provides a business logic layer) - has references to Data Class Library, DataLayer Class Library, and Utilities Class Library
    4. DataLayer Class Library (provides a layer that directly interfaces with the database i.e. CRUD commands) - has references Data Class Library and Utilities Class Library
    5. Utilities Class Library (general library that are used across all layers) - has no references

    For the most part this is fine. But, I have a class in the Utilities Class Library that needs to reference functions and properties in the WebApp and BusinessLayer Class Libraries. I cannot add a reference to these projects, as that would create a circular dependency. So, how would I go about setting up the Interface and the correct references?
    if i was able to help, rate my post!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Using Interfaces to connect libraries

    Quote Originally Posted by jasonwucinski View Post
    For the most part this is fine. But, I have a class in the Utilities Class Library that needs to reference functions and properties in the WebApp and BusinessLayer Class Libraries.
    In that case, you design is faulty. The utility library should have to be aware of the application or else it's not a utility library. Depending on specifics, it might be appropriate for the utility library to accept a delegate to a method that it can invoke and then have the app or business layer pass in a delegate to one of their own methods. Direct calls are just wrong though.

  3. #3

    Thread Starter
    Hyperactive Member jasonwucinski's Avatar
    Join Date
    Mar 2010
    Location
    Pittsburgh
    Posts
    452

    Re: Using Interfaces to connect libraries

    Thank you for your response. So, what would be the best way to set this up? Here is how I currently have my references:

    Name:  Capture.png
Views: 125
Size:  7.6 KB

    In this diagram, the arrows denote references from one library to another. So, would I keep this design and add delegates? Or, would there be some sort of "middleware" between the Utility Class and all the others, to act as an interface
    if i was able to help, rate my post!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Using Interfaces to connect libraries

    That looks reasonable as you have drawn the diagram. Can you provide a specific example of where you feel you need to call a method from another assembly in the utility library, so we can get a feel for what the best option might be?

  5. #5

    Thread Starter
    Hyperactive Member jasonwucinski's Avatar
    Join Date
    Mar 2010
    Location
    Pittsburgh
    Posts
    452

    Re: Using Interfaces to connect libraries

    Yes. There are two examples of where a class in Utilities needs to access a class in Business Layer and Web App:

    1. In the Utilities Class, I need to access the connection string that is used in the DatabaseLayer. In that layer, there is a class responsible for providing a database connection and object, which is used in DatabaseCrudd layer and Business Layer. But, in my Utilities Class, I have build a function that can be used across the entire app to determine someones roles. But, to work, the Utilities class needs some properties from the Database layer

    2. I am using .Net's Identity framework. But, I had to create my own class that inherits from IdenityDdContext (which takes in its own connection string, so I can switch connections when needed). This class is located in the Web App, in the APP_Start folder. A class in the Utilities project needs to instantiate that class, but it would require a reference to it, creating the circular logic.

    If you would like specific code samples, let me know and I can add that. Does that help?
    if i was able to help, rate my post!

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Using Interfaces to connect libraries

    1. Generally speaking, your connection string should be in the config file of the web app anyway. That's where your data layer should be getting it form and that's where your utility library would get it from too.

    2. Sounds like that class perhaps shouldn't be in the web app then. It could be in another library that can then be referenced by the web app and the utility library.

    Without seeing the code it is impossible to say for sure but it may well be that your utility library just isn't well designed. Maybe some of what it contains belongs in one or other of the other assemblies or maybe it should be more than one library itself.

  7. #7

    Thread Starter
    Hyperactive Member jasonwucinski's Avatar
    Join Date
    Mar 2010
    Location
    Pittsburgh
    Posts
    452

    Re: Using Interfaces to connect libraries

    Hello,

    I do not store a connection string. Instead, I store the port number, db name, ip address, etc. I have an abstracted class (the data class) that has a function that pulls these parameters to create the data string. I do this because that class is shared among multiple projects, with different RDMS. You are right, I could just create the same function in the Utility class, but that seems redundant. But, these were just examples. To be honest, I would just like to learn how to create an Interface to resolve circular dependencies. These were just examples to illustrate the need.
    if i was able to help, rate my post!

  8. #8
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,900

    Re: Using Interfaces to connect libraries

    You are right, I could just create the same function in the Utility class, but that seems redundant.
    Just to be clear, the suggestion is not that you replicate the function in the utilities class, it's that you move it. It would still exist in only one place so there would be no redundancy.

    To be honest, I would just like to learn how to create an Interface to resolve circular dependencies.
    While I agree with JM (you would be better revisiting the design) I'll take a punt at explaining with a fairly simple example.

    • Lets say Class A in Assembly A depends on Method B in Class B in Assembly B and Class B in Assembly B depends on Method A in Class A in Assembly A. Visual Studio will allow you to create the first of these reference but the second will be detected as a circular dependency.
    • Introduce Assembly C which contains Interface A (which describes Method A) and Interface B (which describes Method B)
    • Because a class depends on an interface and not the other way round, Assembly C does not need to reference Assembly A or B. In fact it doesn't need to reference anything
    • You can now introduce references from Assembly A and Assembly B to Assembly C. These will not be circular.
    • Method A can now implement Interface Method A. Method B can implement Interface Method B
    • Class A can have an instance of Interface B and can therefore call it to call a concrete Method B. Class B can hove an instance of Interface A and call it to call a concrete Method A




    Here's a link that explains further. And just to back Jm up further, it contains this quote: "Note: Circular dependency is a sign of bad design and tight coupling. So when you get into a circular dependency situation rather than fixing it directly by interfaces or events, question yourself why you landed in this situation."
    Last edited by FunkyDexter; Jul 12th, 2018 at 09:49 AM.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Using Interfaces to connect libraries

    Quote Originally Posted by jasonwucinski View Post
    I do not store a connection string. Instead, I store the port number, db name, ip address, etc.
    That's splitting hairs. It's still the data that the connection string is constructed from and you can store it in the config file and have any library read it from there.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width