business layer code outside of Controllers folder gives compilation error - c#

I've read in other posts that you can put the business logic in a VS2013 MVC project "anywhere", which I take to mean "possibly outside the Controllers folder".
However, when I create an App_Code folder in my project, and put business logic classes in it, (with the build property set to Compile, not Content) I get a compilation error, on the following, and the code editor intellisense won't recognize the the .Caching in the following:
using System.Runtime.Caching;
If I move the class back into a subfolder of Controllers, no problem, the .Caching appears in intellisense and no compilation error occurs.
Any explanations of why this might be so, and how I might adjust my project to allow business logic classes to operate correctly outside the Controllers folder, would be appreciated.

The App_Code folder contains code that is compiled at runtime.
See the following reference: http://msdn.microsoft.com/en-us/library/t990ks23.aspx
If you have business logic, it's probably best to put it into an external (from the webiste) library to increase reuse and enable better compartmentalization.

Add a business layer class library project to your solution, and then add reference to this business layer class library project to your MVC project.

First off, there's no such ting as App_Code folder for MVC.
Why?
Well, the App_Code concept is meant to work with Website Projects, however, MVC are Web Application Projects which is a framework or implementation of a pattern to work closer to the HTTP protocol and web requests and separate concerns into presentation (views), request handlers (controllers) and models. So, don't expect it to work with App_Code
Suggestion
Create/Add a Class Library project in the same VS Solution where you can keep all your business logic and then reference this project in your MVC app

Related

VS 2019 C# intellisense not suggesting references from within solution

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).

Referencing App_Code Class From DLL

I'm working on a web site project. I have inherited a C# class file that I would like to put in a DLL, but the class references classes that reside in the App_Code folder. Is is possible to access App_Code classes from a DLL?
Thanks
ASP.NET websites - as opposed to ASP.NET web projects - are compiled when executed. So when you build them you won't see a .dll output in a bin folder that you could reference from anywhere.
But it wouldn't make sense to use a website as class library anyway. If it contains classes that you need, it would make sense to pull them out of the webiste. That means
create a new class library project
move the class files from the website into the new project
make whatever adjustments are needed to for it to compile.
How well this works may depend on how tightly the code is coupled with the website project in which it lives. For example, a class might look for values in HttpContext.Current. If that's the case, the class won't work outside of a website as-is. You'd need to modify it to get inputs differently. (Without seeing the code, it's hard to know whether this will be trivially easy or tedious and frustrating.)
Hopefully if the classes aren't too coupled to the ASP.NET environment you can remove them from your existing website and even that website can use your new class library.

How to call a class from a project to another project without call the DLL as reference in C#?

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

Use view from other MVC project

I have a few MVC3 projects that uses the same partial views. These views contain parts of forms that i can easily reuse in other MVC projects. is there a way to centralize Views. Like, in another project perhaps? So i can add the assembly of the project and easily call the views i need?
Otherwise i have to copy the reusable views folder every time i start a new project. And when i make a change to one of those views, then i have to change it in all the other projects too. Would be better if i could put it in one place and reference to it somehow.
Is this possible?
The easiest solution is to simply map a shared virtual folder in your views folder, using IIS's virtual folder feature. That way all apps that use the same views will actually point to the same physical folder.
You may have some challenges with deployment, and you could make the shared folders part of their own project. But you will need to deal with that both on the local dev machine and the server.

MVC 3, EF4 Custom Remote Validation with separate model assembly project

I have setup an MVC3 EF4 project with Model and the Repository broken off in to separate assembly projects. The basic validation for a required property etc. works fine, but if i need to do any remote validation say to check if a user is already in a group, etc. The remote validation does not recognize the controller within the Model project.
[Remote("IsUID_Available", "Validation")]
When I try to add a reference to the main project inside the Model project it says it would cause a circular dependency and doesn't allow it to be added.
Do I need to move my Models out the separate assembly and into the main project or is there another way to do remote validation with the Models being in a separate assembly.
Also what is the best practice here. I've read several articles that say putting the models in a separate assembly is the best practice but if you can't use half the validation functionality of MVC what is the point. I've also noticed most of the Microsoft MVC samples show the models just in the main project and not broken off into an assembly.
Turns out this wasn't an issue after all. Remote validation can be used as specified in the code above with the model in a separate assembly.
It was ReSharper that was giving an error in visual studio that the controller was unknown and marking it with the red underline but when actually compiled and tested the remote validation works.

Categories