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/
Related
I'm learning C# and I don't know exactly how to make a class visible to all projects inside the Solution. Basically I have many projects, all Windows Forms, all of them have some similarity, so they can share a lot of classes. I think a good approach is put all the shared classes in the Solution folder and make all the apps get the classes from there. Is this a good approach? How to do it? Another way to do it, I think, is to put all the shared classes in one app and the others get that references.
If that is not a good approach, which one is? How to do it?
I'm using Visual Studio 2019. This is how my Solution looks like, but I can't share the classes between the projects:
Create a Class Library project within your solution and put your shared classes in there (right click on your solution in the Solution Explorer, Add, New Project). Then for each of the projects that you want to access the classes from, right click on Dependencies, Add Project Reference and tick the Class Library project you created.
You create a class library project and in there you would want to add folders where you can name them according to what the classes would do so it is easier to maintain. Then you would add your classes in the corresponding folder and in the project that you want to use the class you can add a reference to the project and the class would be accessible as long as it was a public class.
I'm new to programming so I might not be using some of the correct terminology. I'm running into an issue with InteliSense when calling a C# class from another project within the same solution. It's not suggesting a using statement and is instead trying to get me to create a new class inside of the current project which is not what I want. I am having to go in and add a refernce to the project and then add the using statement in order to get access to the class.
I looked at some of the documentation online and nothing has helped so far. InteliSense appears to configured correctly to suggest using statements. It provide suggestions just fine. I've been able to create lists and then use it to add the proper using statement along with some other things. Just doesn't want to work with anything inside the solution. I've been following a couple of different tutorials including, .net core 2.1 and 3.1, inside MVC and Razor page projects along with a couple just straight C# console apps. It doesn't work in any of them when I start adding multiple projects to the solution and try using classes from outside the current project.
I am having to go in and add a refernce to the project and then add the using statement in order to get access to the class.
That is the correct behavior. In order for ProjectB to use classes defined in ProjectA, you must first add a reference to ProjectA. Just having the projects in the same solution is not sufficient.
The purpose of having multiple projects in the same solution is simply for grouping related code. The projects may or may not actually depend on each other. For example, a web application may have a separate projects for the actual web UI (the pages themselves), a data access layer, unit tests, maybe some class libraries for shared code used by multiple projects, and maybe even console applications (or some other project type) for performing backend administrative tasks. In this scenario, the web UI and console applications may have references to the data access layer project and/or the class libraries. The unit test project will have a reference to the web UI project, and so on. The dependencies are one-way - you may not have circular references (the unit test project has the web UI project as a dependency, but not the other way).
I'm writing a program in C# that has two classes, Foo and Bar. I have two executables that will use them, GenerateFooBar and SearchFooBar. My question is how should my solution be partitioned? From what I can gather, everything can live in one solution and each executable should have it's own project. Should Foo and Bar
share a project?
each have their own projects?
go with one of the other projects?
something else?
Thanks for the help! I'm coming from Java if that's helpful.
In your solution, create project called for example Common, and put all your classes and businbes logics there. Set project type as Class Library
Next add your other projects and in them Add Reference to Solution Common project to use it.
If multiple users are meant to work with the shared library, the best solution will be to create the local nugget
Create a shared project and put Foo and Bar into it. Reference that project from both executable projects.
If you later have many developers depending on the shared library you might make it into a Nuget package that you publish and have them depend on that.
From what I can gather, everything can live in one solution and each
executable should have it's own project.
Yes, you are right.
how should my solution be partitioned?
Totally depends on you and how you would like to manage it.
You could create one project, multiple projects, each separated projects, etc.
But, to reuse code as much as we can and to give a more structured way. Here is what you can do.
Make one Project which will hold the ApplicationServices, Helpers, etc eg: Foo and Bar.
Make Another Project which will hold the application itself (Could be two projects one for search and one for generate). You can add reference of service project to your foo and bar projects and to any other project you create in future.
So in the end you will have 2 or 3 projects (depending on you)
1- ApplicationServices
2- Generators/GeneratorServices/GenerateApplications/anyname
3- Search/SearchServices/SearchApplications/anyname
I have three parts of a project, one is Core(bussiness) project, one is webproject and the last is the test project.
I want to call some classes that I saved in one folder from core project to webproject without calling the dll of the core project. Since core project is already refereed into the webproject. So it will call the cycling if the references and not allow me to do that.
How I can use classes from core project to web project without call the DLL?
I Have already tried 'using', The code is following. It's not working for me.
using BlogEngine.Core.API.PageML; // I need to call all classes from this folder
using BlogPage = BlogEngine.Core.API.PageML;
namespace admin.Settings
{
public partial class DownloadInsertPage : System.Web.UI.Page{
protected void BtnPageMLInsertClick(object sender, EventArgs e)
{
var reader = new PageReaders(); // the class, I want to call from the core project
}
}
Thanks
Your question is unclear ("since web project is already refereed into the webproject" for example) but you should really make the web project have a reference to the core project, but not the other way round. The core business logic shouldn't know about your web "view" onto it, but the web project should absolutely know about the core business classes.
If you have classes in the web project which you're currently referring to from the core project, you should probably move them to the core project - or to a third project which both of the other projects refer to, if they don't logically belong in the core project itself.
Your model may benefit from contract interfaces - that would help you redesign your current model in order to avoid circular references. Here's some posts about this:
a design to avoid circular reference in this scenario
Resolving Circular References (C#)
Now, I may have misunderstood your question, and you'd rather benefit more from learning about add-on models. In that case, this post may be of some help:
C# Plugin Architecture with interfaces share between plugins
You cannot.
If you need these classes to be accessible in both assembly, you should create a third one that would be referenced by both of them although this should be reserved to cross-cutting concerns only (e.g. logging logic).
Thank you guys for your valuable comments..
I want to share, what was the problem and how I resolved it.
I build the core project after created some classes into the PageML folder and build it without calling this folder into the web project. and update the reference(dll) of core project in the webproject. and it's working for me.
Thank you All
Keep clam and do coding.
:) have a nice time
I have a MVC4 project which is going to be a large system, I want to split the database management into its own project so in my solution I will have MyMVC4 and MyMVC4_Data
I will add MyMVC4_Data as a reference to the main MyMVC4 project. I believe there will be multiple projects in the future so splitting the data makes sense.
My question is, what sort of project template will be sufficient for the database stuff, all it will have is Linq and manager classes for each table to insert delete etc. I don't think it would be necessary to include a new MVC4 project as the overheads will be too big for what is needed. The project must be referable by the main project
Any suggestions would be appreciated
Thanks
I'd use a Class Library project. From the docs:
You can use the Class Library template to quickly create reusable classes and components that can be shared with other projects.