C# - Use a referenced library from an included project - c#

for my C# projects, I created a "Helper-Project" that handles some common issues, which I use in my other projects. For the specific solution, I include my "Helper-Project" and use the public classes defined within.
In my "Helper-Project", I referenced some external libraries, which I want to use in my specific working-project (that includes the "Helper-Project") as well. Is there a way to access those referenced libraries of the "Helper-Project" from the "Working-Project" as well, or do I have to include them in the "Working-Project" as well?
Here is schematic display of the structure:
solution
working-project
helper-project
references
library-that-I-want-to-use-in-working-project-as-well
Thanks in advance,
Frank

You will have to reference them in the working project.
If for some reason you absolutely cannot reference those libraries in the working project and the methods you need from the libraries are few, you can (but shouldn't) create wrapper methods in the helper project:
string wrapperMethod() {
return libraryMethod();
}

Related

Creating 2 sets of .Net C# plugins for 2 diffferent external applications which have the same API except namespace

I need to implement 2 set of plugin libraries with exactly the same code for 2 different external applications (I have no control of the external applications). Both applications expose .net classes with exactly the same API in different namespaces.
MyLibrary1 references VendorLibrary1
MyLibrary2 references VendorLibrary2
Both VendorLibrary1 and VendorLibrary2 expose SomeClass (class has the same name) with exactly the same methods.
How can I avoid the need to maintain 2 sets of identical source files (project files, references, cs files etc) which only differ in the using statements and references?
I already have 3 solution ideas which I consider too complicated so I am looking for something simpler/more elegant way of achieving this.
Solution 1
Somehow create wrappers around the vendor libraries which allows switching the library using DI or some other means. The vendor libraries expose 100+ classes and most of them I am using so this is a considerable work and wrapping everything and using wrapped types in method signatures can quickly make this very complicated.
Solution 2
Use #if compiled defines in the using statements. Eg.
#if VENDOR1
using Vendor1NameSpace;
#else
using Vendor2NameSpace;
#endif
Put all classes in one shared project and create 2 libraries, one library defines VENDOR1 and references VendorLibrary1 and the other defines VENDOR2 and references VendorLibrary2
I would need to create 2 sets of of all of my libraries, one for VENDOR1, one for VENDOR2
I think from this point on I need to maintain the using statements manually, can not rely on Resharper.
Solution 3
Implement the library against VendorLibrary1 and use a script which duplicates the solution. While duplicating the solution project references and namespaces in using statements are changed from VendorLibrary1 to VendorLibrary2, Vendor1NameSpace to Vendor2NameSpace etc.
I have created a simple code generator which implements solution 3. Code is developed against Vendor1 libraries and the code generator duplicates the library (project files with new name and source files) for Vendor2. It replaces referenced dlls in the project file and replaces namespaces in the source files. Works well.

How to correctly resolve DLL references through class libraries without adding the reference to the calling project

TL:DR How do I reference an assembly only in a class library rather than both the library and the calling project?
I am building a utility library in C# in order to promote code reuse. In this instance, I am wanting to do something things with a TFS server and need to reference several assemblies from the TFS side of things:
Microsoft.TeamFoundation.Client
Microsoft.VersionControl.Client
Microsoft.WorkItemTracking.Client
I include these references in the class library called, say, Utility. I then proceed to wrap objects in those assemblies in helper objects. There are no errors, intellisense works correctly, and so forth.
When I want to use that class library in another project inside the same solution, say, TestCLI, I add a reference to the Utility project by selecting the project from the solution references tab. I can then use the Utility classes without issue, until I go to build.
When I build the solution, it throws an error such as:
The type 'Microsoft.TeamFoundation.VersionControl.Client.BranchObject' is defined in an assembly that is not referenced. You must add a reference to assembly 'Microsoft.TeamFoundation.VersionControl.Client, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
In the past, I have worked around this issue by adding the same references to the calling project (TestCLI) in addition to the class library (Utility). I feel that this is defeating one of the purposes of having a class library and that I've missed a step in order to not have to worry about library references in my calling project.
Is there some way to resolve these dependencies without including the references in both the class library and the calling project? Am I structuring my solutions incorrectly? Am I thinking about class libraries in the incorrect manner?
The references are required because you are exposing objects from the other libraries, and then to use these classes the final program needs the references.
To avoid this you must hide the external objects, through a wrapper, a copy of the class or anything else, it depends primarily on what and why you are exposing those objects.

Visual Studio solution - issue with referenced projects and object accesibility

this might be a bit dummy question but I'm confused...
I have a simple C# solution in VS with 3 projects
UI
CORE
DAL
Now, I've added 'DAL' as a reference inside 'CORE' so now I can see and use my DB methods.
However, since I want to send one of the 'CORE' classes to my INSERT method which is inside 'DAL' (to insert the full object) I cannot see or access it, and I also can't add a circular reference and add 'CORE' to 'DAL'.
public void InsertOrUpdateResultData(MyObject _obj)
MyObject is from 'CORE' project.
Any help is appreciated.
Thanks
You can extract the common classes inside a separate project and reference it from the CORE and DAL. Eventually you can create DTOs.
If you encounter such situation you are probably doing something wrong. I would suggest to rethink structure of your solution. Maybe it would be worth to create another library that can be included in both CORE and DAL projects.
If you're using the same class across multiple projects, as is your case here.. have you thought about using a shared project? You can add the shared project to all other projects you need. Based on what you've stated, it would probably be a better solution.
http://dailydotnettips.com/2015/07/28/using-shared-project-across-multiple-applications-in-visual-studio-2015/

Bind several classes into one .dll file in C#

A beginner's question. It is c#.
Let's say I have three classes in one project named Employee, Department, Address. For some reason, I would like to have a .dll file (let's name it test.dll) to have all three classes included that I can call it from some other project using syntax like "test.Employee emp1 = new test.Employee();"
That is my idea. Is this possible? If yes, how should I do that? Do I have to create a class library project to do so? I know nothing about a class library project. So I may need further help with that.
If the answer is no, how do I add references to those classes from other solutions?
Thanks.
Create a class library project.
Give it a proper namespace
Write your library classes, then compile to a dll
Then add a reference to that dll in the other project you want to use it in
add a using statement to include the reference in your code files.
Pretty straightforward

Creating reusable code modules (e.g. C++ style classes, headers) in C#

In C++, you can create usable code modules by creating a class, and giving out header and implementation files to the developers who want to use your class.
I want to do this in C# but I have little experience with the C# language. Basically I need to create a class that can be reused by another C# programmer in Visual Studio 2010. I know that referencing DLLs is one way to use other peoples' classes. Do I need to create a DLL to achieve what I want to accomplish? Or are there other, better ways?
For example, let's say I create a Cow class that can "moo". In C++, someone who uses my class would just include Cow.h, instantiate a Cow object myCow, and then call myCow.moo(). How can I achieve this simple task in C#?
Thanks for your time and patience.
Yes, just create Class Library project and share the resulted dll's.
Other developers will just need to add a reference to your dll and after that they're free to use any public objects from your library.
It is the standard to create a dll to distribute reusable code.
You could look into old school COM objects, but I would steer clear of them and just use a well organized class library.
Of course you can always share your source files, but the recommended .Net way of distributing reusable code is though dlls. This allows developers using any .Net language to use your code (they don't have to use the same language as your project).
It also makes it easier to maintain the project. If you share source code then it will likely be more difficult to distribute updates than if you just needed to update a single dll. If you have multiple projects referencing the same dll, they can all reference it from the same location and whenever the dll is updated, all the projects that use it will automatically use the updated dll the next time they compile the project. You can also update the dll without having to recompile the projects that use it (though you can't change the names/signatures of anything that is being used by the project).

Categories