0

I would ask also for best practices and patterns for asp.net MVC, using this example:

I have a project. This is its architecture:

  1. Models
  2. Controler
  3. Views
  4. Tools

In models folder I have every single ViewModel in separate class field. All of table declarations (objects) I put in one file (AccountModels.cs or ForumModels.cs). I have a separate file for EF context (MyAppContext.cs).

Controler - here I have only Controler classes. But maybe part of code will be better example and suggestion what can I improve:

private AppContext db = new AppContext ();

[HttpPost]
[Authorize]
public ActionResult AddGun(GunModel model)
{
    if (ModelState.IsValid)
    {
        Gun gunToAdd = new Gun
        {
            Tilte = model.Tilte,
            AuthorID = UserTools.getUser(User.Identity.Name).UserId,
            AddDate = DateTime.UtcNow,
            Content = model.Content,
            CategoryID = model.CategoryID,
            CategoryName = GunsTools.getCategoryName(model.CategoryID)

        };

        db.Guns.Add(gunToAdd);
        db.SaveChanges();

        return RedirectToAction("Details", new { ID = gunToAdd.ID });

    }

return RedirectToAction("Index");

}

This is a part of controler with AddGun Action. Others ActionResults are similar - generaly I use lambda expression on my db context to get values etc.

Views - Views in separate folder, for Partial Views I set special prefix (for example - _NavigationPartial.cshtml or _CalculatorPartial.cshtml). Is there anything for improve here? Of course Views use ViewModels, not Models.

And at least - Tools. I've put here some classes and methods to prevent from repeating code. Here is some methods witch returns from database some objects or just strings, like GetUser(..) or GetCategoryName(..). Is it a good practise at all?

I think that many young MVC developers have the same project architecture (I personally saw it in a few companies) and many of them perhaps also wants to improve something in theirs projects to be a better programmers.

Regards

2
  • I think you should narrow this down and hone in on specific questions. Best practice questions are often closed or debated endlessly. Commented Sep 18, 2013 at 13:23
  • Thanks for respond. I had a problem how to ask for specific problems or practices, so I thinked that I will show example of my MVC app. I do not know patterns and practices, so I don't know what I should ask. Commented Sep 18, 2013 at 13:27

1 Answer 1

1

As noted in the comments above, it's hard to answer such a broad question, but I will make a couple of observations


  • It's not a great idea to use your database entities directly in your Controller, because that tightly couples your controller to the database and can lead to data being exposed to your view that really doesn't belong there.

Instead you should have a separate data layer that abstracts away the details of what a Gun entity is (from the perspective of the database) and what a Gun Model is (from the perspective of the View)

A randomly selected but good SO question about this is found here


  • You could consider using Dependency Injection (DI) for your controller

so that you have a constructor that looks like this:

readonly AppContext _db;
public GunController(AppContext db)
{
    if (db==null) {throw new ArgumentNullException("db is null");}
   _db=db;
}

You'll need to use a DI Container to this up. A randomly selected (but really good) article can be found here


Above all, keep asking questions, but try to keep them more specific i.e about specific areas of your app - and in no time you will have a really good picture of what is and isn't good practice!

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for respond. You should have a separate data layer - ..where I will do all of operations on models? Am I right? Is this a good example of usage this pattern? Click here. Shall I also use here some DI Container? Which is the best? As I understand, on example above is everything what I need - separate data layer and DI, yes? So many questions, but after reading your post I better understand it. Thanks once again. Regards!
@whoah, yes that's a pretty good example. You don't need to use DI initially but in the long term it will help. I'd recommend getting your layers sorted out first, and then it will become cleared how to use DI to help you decouple them. Personally I find NInject to be excellent for DI, but you should check out some of the others, they may click with you :)
Lovely! Big thanks again :) Ok, so firstly I will try only with DAL's (data application layers - is it a proper name?) for my buissnes service without DI - is it a good idea? Then I will add it to my controlers - and here is my question. In my controler, shall I use inferface, like ILibraryService _libraryService; (I think it is for DI) or maybe just LibraryService _LibraryService; (I think that this is correct :)). And when is good moment to start using DI with data layers? Or maybe I confuse something? Regards! :)
@whoah - DAL = Data Access Layer :) And yes, it's a good place to start! You don't need to use interfaces right away - and in particular when you are just learning the concepts, don't worry about it. Now you will need to use interfaces when you go to set things up for DI but you can just use Visual Studio to extract the interface from your existing classes and then refactor the various layers to accept by interface.
@whoah - once the patterns work for you you'll just end up always organising that way because they just make sense - I would say that yes, always try to use them because it's like a discipline. You never know when your little project might grow larger! I would also say that for your Business Services and Repos, create separate projects (class libraries) for these - it sets you on the path to decoupling much faster!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.