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
Related
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.
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.
I wrote some classes that I use with many different projects.
For example, I use Library.Controls.FlatButton.cs almost in every project.
The problem is when I add this as an "existing item"; the class gets created/copied in my soultion folder everytime. And each time I edit/update the contents of that class, I have to update all the Library.Controls.FlatButton.cs files in every project folder.
I need to be able to edit a single source of FlatButton class and when I compile/build a project (that uses the class file) gets updated to the new version of that class.
Question 1: Is there a way to do this?
I know that I can gather all these classes in a library project (Library.Controls) and add it to each application solution as a dependency.
Question 2: Is this the only way to work from a single source of common library files? And if I do; will all the classes in the Library.Controls namespace get compiled with every application, even if I've only used this FlatButton class in the project?
Hope this is clear for you..
thanks
I'd rather go with the approach of the shared library and add them as references to your client project.
If you don't want to do this. You could add the file as "Link". In Add existing item, select Add as Link instead.
Yes, a class library is the way to go and yes, since the whole class library will be referenced from your applications, all the classes will be available to it.
However, the fact that all the classes are available is not a bad thing, since they're in a separate class library it won't make your applications harder to understand (since the amount of code in those applications will stay the same), it might just be that you use up a little bit more hard drive space, though if you really worry about that you could put the class library in the GAC so that all apps reference the same copy of the library, though you'd better research this first to make sure that it's suitable for you.
Alternative way is to add FlatButton.cs file "As Link":
Scenario:
I have 2 Projects, MainApplication (which compiles to an exe) and ClassLibrary1.
Now MainApplication references or loads ClassLibrary1, but ClassLibrary1 has no idea about MainApplication.
But I want to use Settings (Dot.Net 2.0's Properties.Settings NOT appSettings) that are defined in MainApplication.
How do you achieve this?
I have seem PLENTY examples that use
System.Configuration.ConfigurationSettings.AppSettings.Get("SettingName");
This is NOT relevant to my situation as appSettings is old school and I am using the newer Properties.Settings mechanisms.
Your help is appreciated :)
I have done some investigating in code.
I can get the setting like this but it is really dirty:
((ClientSettingsSection)ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).SectionGroups["applicationSettings"].Sections["MainApplication.Properties.Settings"]).Settings.Get("Tester").Value.ValueXml.InnerText;
Maybe someone can provide a more elegant solition
Since the Settings class is defined on the Main project, you cannot directly access it from the class project because you would create a circular dependency.
What you would do is provide the Class library with a delegate it can call to dynamically retrieve the settings from the main project. Create a class that stores a delegate in the Class library and set this delegate to a method defined in the Main project. This methods would encapsulate the instructions needed to retrieve a setting.
And in my opinion, appSettings is not old school, it is just a way of representing configuration parameters that are not specific or customizable by a user.
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