C
C#3mo ago
Mekasu0124

How do I use models from Project A in Project B?

Lets say I have a models folder with a few models in it inside of Project A and I've created another project under the same .sln called Project B. I instead of having to create duplicate models between both projects, I want to use the models with Project A's models folder inside of Project B. I tried
using ProjectA.Models;
using ProjectA.Models;
but I get a resolve error basically saying it can't find it, but the folder structure is
Project/
- ProjectA/
- Models/
- ProjectB/
- Program.cs // use ProjectA.Models here
Project/
- ProjectA/
- Models/
- ProjectB/
- Program.cs // use ProjectA.Models here
. Thanks in advance.
16 Replies
Angius
Angius3mo ago
Add a reference to the project Then import the namespace with using
Mekasu0124
Mekasu01243mo ago
ok I won't go that route then. If I reference each project to each other, it creates a circle problem. I'll figure that part out later
Angius
Angius3mo ago
You can pull the common elements to another, Shared project
Mekasu0124
Mekasu01243mo ago
there isn't a shared project unfortunately. thanks though ❤️
Pobiega
Pobiega3mo ago
But you can make a shared library and ref it from A and B.
Mekasu0124
Mekasu01243mo ago
oh ok so like have multiple projects then
root/
- Project Dealing With Database
- Project Dealing With Data Validation and Error Handling
- Main Project Running It All
root/
- Project Dealing With Database
- Project Dealing With Data Validation and Error Handling
- Main Project Running It All
type thing yea?
Pobiega
Pobiega3mo ago
yeah, sorta you'd have one (or more, but eh) "runner" projects - a console app, avalonia, whatever. some kind of executable project type and several class library projects, separated by whatever makes sense for you
Mekasu0124
Mekasu01243mo ago
so what would the file structure
Pobiega
Pobiega3mo ago
.
├── MyProj.sln
├── .gitignore
├── .editorconfig
└── src/
├── MyProj.Common/
│ ├── MyProj.Common.csproj
│ └── Models/
│ └── Stuff.cs
├── MyProj.Desktop/
│ ├── MyProj.Desktop.csproj
│ └── Program.cs
└── MyProj.Infrastructure/
├── MyProj.Infrastructure.csproj
└── Database.cs
.
├── MyProj.sln
├── .gitignore
├── .editorconfig
└── src/
├── MyProj.Common/
│ ├── MyProj.Common.csproj
│ └── Models/
│ └── Stuff.cs
├── MyProj.Desktop/
│ ├── MyProj.Desktop.csproj
│ └── Program.cs
└── MyProj.Infrastructure/
├── MyProj.Infrastructure.csproj
└── Database.cs
something like this would be very common
Mekasu0124
Mekasu01243mo ago
ok cool. So like what would each section be about? Example: MyProj I know is the root of the project. Inside of src/ there's Common, Desktop, and Infrastructure. I presume that Desktop is where I would do all of my normal coding and such with the Views and ViewModels, but I'm not sure what would go in Common or Infrastructure. I'm using MongoDB local host db for testing and building and then I'll be using it for production, but with a different cluster, so like would my Database calls of GET, PUT, UPDATE, DELETE in the Infrastructure part?
Pobiega
Pobiega3mo ago
Anything related to the database would go in infrastructure While common would be the stuff shared between other projects, usually a bunch of classes or interfaces that are used everywhere But if you don't need multiple projects, don't use it A classic example would be if you want to have a desktop client and a console client, or of you also plan to make a server Then you must have multiple projects, and a shared library between them would solve a lot of issues
Mekasu0124
Mekasu01243mo ago
so to make sure I understand this part correctly. I should manually create the root folder along with the git ignore and the editor config and a src folder and then open that in rider, and inside of the src folder, I would create new projects as shown above? ok so the first project that I'm applying this methodology to is a Diary application. It'll be using an online database and it'll have a console version as well. The desktop app will be avalonia if that makes a difference ok so I've got the project setup like you showed. How do I share the projects? add a new reference in the dependencies? yea that's it. Ok so would I reference Infrastructure and Common inside Desktop? Or what's the combo? and would I create another project for the console application too and then build the avalonia app inside the Desktop folder? nvm lol honestly. This is a bit like wow haha but I'm going to build them as separate applications, and they'll reference the same database and be two separate applications with separate downloads
Pobiega
Pobiega3mo ago
Yeah, you'd so exactly as the tree but just add a fourth one called Cli or Console or whatever. And you would publish it as it's own executable
Mekasu0124
Mekasu01243mo ago
ohhhhh ok. so create them like above, but distribute as separate executables. idk why I didn't think of that lmao ty! ok so with referencing the other projects in the main projects would I reference them by referencing Infrastructure and Common in both Desktop and CLI? so I could put all of my models/helper classes/etc that would be used between both Desktop and CLI in the Common project and I put all database stuff in the Infrastructure project since Common and Infrastructure are shared between Desktop and CLI and then I'd reference Infrastructure and Common in both the Desktop and CLI applications
Pobiega
Pobiega3mo ago
Yep
Mekasu0124
Mekasu01243mo ago
bet. sweet. gonna give that a go now. Thanks!