C
C#8mo ago
kT

❔ Accessing class library objects from shared project

I have one solution (.sln) which consists of 2 projects: 1) Class Library project 2) Shared project. I have some Repository class in the Class Library project. In the Shared project, I want to refer to this repository type but it is not possible to add a class library project reference to Shared project. How can I access repository class in my shared project then?
19 Replies
IsNotNull
IsNotNull8mo ago
What reason exists where you can't add a reference to project 1? Is it a technical or legal reason, or something else? Please explain.
kT
kT8mo ago
Stack Overflow
C# Using references/class library in a shared project
I have just learned and created a shared project in visual studio 2017. I have noticed that the shared project did not have the "Reference" to refer to other resources (other projects, class librar...
kT
kT8mo ago
Shared projects do not have references, do not have NuGet packages or anything
IsNotNull
IsNotNull8mo ago
Oh, you mean that abomination of a feature that lets you build arbitrary things in a directory without actually having a project?
kT
kT8mo ago
If we click Add -> Reference -> Project Reference in a shared project, then 0 projects appear (so I can't add reference to the class library project) However, I came to idea that I will create interface of my repository in shared project, and implement this interface in class library project. So that I can access an interface in shared project not sure if it's good design though
IsNotNull
IsNotNull8mo ago
Why are you using a shared project?
kT
kT8mo ago
That's a good question 😄 I just develop the existing project and I need to add functionality in already existing shared project
IsNotNull
IsNotNull8mo ago
A shared project is not useful on its own. Its just a pile of code Something else has to refer to the shared project
kT
kT8mo ago
In this shared project we have some basic mapping logic but now I need to extend this mapping and I need to use some code from our repository which is stored in separate class library project
IsNotNull
IsNotNull8mo ago
So what you need to do is find the project that refers to the shared project and add a reference to the class library as well
kT
kT8mo ago
maybe it just shouldn't be stored in shared project
IsNotNull
IsNotNull8mo ago
A shared project can't have dependencies, because its not a project. Its just files that could be included somewhere else If I understand it correctly, nothing should go in a shared project unless it is stand alone, reusable code that doesn't depend on any other projects
kT
kT8mo ago
So what you need to do is find the project that refers to the shared project and add a reference to the class library as well
class library project has already added reference to the shared project. And it works that way. But I need to do vice versa - shared project needs to have reference to class library but it's not technically possible yes, I think this code shouln't be in a shared project then
IsNotNull
IsNotNull8mo ago
If class library has a reference to the shared project then it would be impossible for the shared project to refer to the class library even if that was possible in another way, that would be a circular dependency It wouldn't be able to reason about which one to build first If A depends on B and B depends on A, then neither can be built In my opinion nothing should be in a shared project. I'd rather link in individual .cs files into each project from a common directory (a few levels 'up' maybe).
kT
kT8mo ago
so what's the purpose of these shared projects? some models / static class only? Without any logic?
IsNotNull
IsNotNull8mo ago
If you have a lot of shared code that doesn't share any build or configuration details and may need to be added to arbitrary projects AND you don't want to take the time to link each file to multiple projects...I guess I still wouldn't use them. I've seen them abused horrible in multiple enterprise projects Though usually as ASP.NET 'directory based' projects, not using a 'shared project'
Jimmacle
Jimmacle8mo ago
you can't even use shared projects in recent .NET versions can you PepeHmmm thought MS finally killed them, or it's a VS thing and i didn't notice them missing when i went rider
IsNotNull
IsNotNull8mo ago
I'm not sure. I'm just glad the 'projectless' style of development is slowly dying. Its lead to so many terrible enterprise systems and abuses. I think enterprises liked them (in the ASP.NET) realm because they could dump in new .aspx files to fix visual issues or to do monkey patching, or as a cheap way to do post deploy customization. It means your compile step is meaningless for determining correctness of the system though. Rubbish (I don't mean anything bad aimed at you or you our your employer kT, I've just had bad experiences and venting). Uh, back to your original problem...that one might require some redesign. If your shared project is already referenced by class lib, you can't use class lib by the shared project. How much code does the repo use from the shared project? Could you just move the code it depends on into the class lib? If this is a bigger project, you might want to ask someone who has been on it for a while why it is that way, and if there is anything that can be done about it. If you have a shared project, I'm guessing something other than class lib references it as well? @kT
Accord
Accord8mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts