How to remove Circular dependency in project reference - c#

For the new requirement, I was changing the existing solution structure. so, when the changing i was facing the issue of circular dependency reference project. there are 3 class library.
Book library contains Book.cs class. In this class there is a method GetMap in that i'm call Get Method from Common library.
CustomBook library contains CustomBook.cs which inherits Book class.
Common library which contains CallingCall.cs has GET Method in this class we are initializing the CustomBook class. To initialize, I'm adding the reference of Book library project to Common library. then i was facing the circular dependency.
Please help me out from this issue.
Thanks in Advance!

Common library cannot reference any other library. So, remove all the code that needs to reference something else into a new library, or move it to existing library, or move it to the main app. Or move all the referenced code into a big freaky Common library. These 4 options are pretty much all the options you have.

Related

Class access methods

How can I access a class which is in another project in C#. Please keep in mind that I cannot add any reference due to creating circular referencing.
Is there any way?
Not without some refactoring. Reflection could help but you still wouldn't be able to extend from that class or use it in any non runtime way.
I have found that this can happen with minimum refactoring
Identify the shared code
Extract it to a new shared project
Add the new project as a reference to all projects that need it

C# Integrating classes from Shared Projects with many projects

I'm splitting an old project up into it's component parts so that the individual components can be used by several other projects.
There are around 4 stages that are split up into components and 3 master projects which will reference these components.
There's a main class (located in the Shared Project, call it Master class for reference) that is consistent throughout that is shared.
I've setup a shared project and added the references, working fine.
The problem comes when trying to pass a variable using Class A (referenced from Project A's version of Master class) to a method in Component A's version of Master Class, I get compatibility issues.
(Along the lines of cannot implicitly convert MasterClassA to MasterClassA)
I know they're identical and I understand that they are essentially a separate instance of Master class, which is why they're not currently compatible.
I've looked into generics/reflection, changing to a Portable Library Class and seem to be going around in circles.
I know reflection is slow so I'd like to avoid it if possible.
Any guidance on best practice and how to resolve the issue?
Here's a crude diagram of the layout.
In case anyone comes across this, this answer resolved the issue for me;
Referencing shared project in several projects of solution
Essentially we create a class library that references the Shared Project, then reference the new project, not the Shared project.

How to access view models and models of project in another class library without adding reference to the class library

I am working on a WPF project using MVVM pattern. In solution, I have viewmodels,models,views and properties.As per requirment, I need to access the same classes(view models, models, properties) from another class library in same project. I do not want to add the reference to the class library as it is an exe file and a heavy component which has got so many classes which i do not require. So, is there any solution for this. How can i access same classes(view model,model,propeties) in a solution from another class library in same project?
Thanks & Regards
You have two choices. First one, is to refactor your exe in order to extract the reusable classes to a separate dll. Then, just add a reference to this new dll.
The other is to use reflection to access the members of the exe, which is the worst option, even worse than simply adding a reference to the big old exe.
I suggest breaking down your solution further in more projects, For example separate projects for Model, View and ViewModel or perhaps breaking down even further and then add reference only to the library you need. That way you are not exposing everything.
The other option is reflection which may be cumbersome to use and make your code ugly.

Splitting a method across two files in C#

Is it possible to split a method across two files in C#? I know partial methods are there, but they seemingly cannot do this.
Here's the scenario. I'm using an open-source library and need to add some customizations in one or more files. For example, I want to add two new fields to the class Employee and then initialize them in the InitializeFields() method of that class. Since the open-source project continues to evolve and new versions are released every now and then, I want to keep my customizations separate from the original project, to easily upgrade to newer versions of the library.
I have already split all the required classes into Orig.cs and Custom.cs using partial class syntax, and have added my custom fields in Custom.cs. Now the problem is that I have no way of splitting the InitializeFields() method, so that my custom code goes into Custom.cs file.
Please note that I cannot use inheritance to solve the problem. The open-source library would contain numerous references to Employee class and I cannot afford to change all of them.
When you do this, you're compiling the library yourself, right?
I understand you don't want to subclass Employee, because then all the library code that does new Employee() won't work. However, what if you rename Employee to EmployeeBase in Orig.cs, and provide the class Employee in Custom.cs? That way when the library code is compiled, new Employee() will reference your class, not the library one. Since you're compiling the library and your customizations in the same project, you can make this substitution.

vs2008 circular references (c#)

Here's the setup I have in a vs2008 solution:
Data layer in a project named MyProject.Data
Web application in a project named MyProject.Web
MyProject.Web has a reference to MyProject.Data
In MyProject.Web I have a class I use called "MySite.Utils"
I want to be able to use MySite.Utils in MyProject.Data but I can't because it would cause a circular reference.
One solution which is NOT possible is creating a third project and moving "MySite.Utils" in there because MySite.Utils actually uses MyProject.Data (thus it needs to reference it and another circular reference would be created)
What's the best/easiest way to fix this?
You need to move MySite.Utils to MyProject.Data by the sound of it
The best fix is to simplify things... for example, is that utility code data utility code, or ui utility code. Perhaps split it into 2 dlls; that might make things simpler.
After that, interfaces are a reasonable option, but you might need some IoC framework to provide the implementations. IoC is the most common way of getting around this type of problem. For example, you declare your ICustomerRepository in a reference assembly; everything references that. Your DAL implements the interface, but the utils project no longer needs to reference the DAL - just the interface assembly. Your DAL can now reference the utils - or it might just know about another interface IDataUtils (better to split it up more meaningfully, of course). The glue here is the IoC container, such as Castle Windsor.
Finally, and don't do this, but even though the IDE doesn't let you, it is possible to create circular references in .NET (via the command line tools); it is legal, but it gets very messy very quickly, and it is hard to repair a broken build. Don't go there!!
Defeat coupling with dependency injection.
Program to an interface.
MySite.Utils shouldn't reference any other project in your solution. Any classes that reference another solution within Utils should be moved into that solution it references.
I believe that the ONLY way to fix this would be to move all the inter-related functionality in one assembly so that there are no circular references. Sorry. :(
Perhaps think about changing the architecture somehow that this is not required?
Sounds like you could benefit (and enjoy!) from reading this...
http://www.amazon.com/Framework-Design-Guidelines-Conventions-Development/dp/0321246756

Categories

Resources