1

I'd like to get some advice about how to set up the controllers for a new site.

This site will have Libraries within locations, and each library can have a set of books.

The URL structure will look like this:

/Library-Name/Books/Book-Name

The books may also have categories or genres:

/Library-Name/Category/Books/Book-Name

My question is, do I create just a Library controller with all the logic in there of adding/removing libraries as well as adding and removing books?

Or do I create a libraries controller and a books controller? If so how do I ensure that books are 'tied' to a specific library?

Sorry for the simple question, but none of the main tutorials seem to have a nested route like this.

Thanks, Robbie

3 Answers 3

2

LibraryName (maybe) makes sense (but some obvious location issues pop up), if you're using multiple physically separate libraries?

Note, you'll probably start to hit Routing hell with this method, but it's not impossible, so I'd research and define your structure first.

You would probably be better with something like the following URI structure:

/libraries/{LibraryName}/books/

Get a list of books in libraryName

/libraries/{LibraryName}/books/{BookISBN}

Get a book by it's ISBN number

/libraries/{LibraryName}/books?category=somecategory

Get a list of books in somecategory

With an Example route of:

    routes.MapRoute(
        name: "LibraryBooks",
        url: "libraries/{id}/books",
        defaults: new { controller = "Libraries", action = "GetBooks" }
    );

And it's method in a LibrariesController:

    public IEnumerable<IBook> GetBooks(string id)
    {
        return new IBook[] { new Book(), new Book() };
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, good answer - would I need a books controller at all?
Well... I'd forget all about MVC first, and think about your basic entity relationships (Libraries>BooksInLibaries>Books... etc.. etc..) This will probably dictate if you want multiple controllers and everything will become clearer. What the actual book methods above return is based on your need, but could just be references (ie, an ID) which you could then use in a link to the actual book. That link would then resolve to a book controller and a GetBook method where you display the complete book info (which is then your singular unique record).
2

Check out Areas.

If you're going to have code that is repeated in multiple controllers, consider creating separate classes and methods for that code so that you can follow the DRY principle.

4 Comments

i think for nested categories, area will too complicated. Because Area create different controllers and other folders tool. SO, why don't just trick the routes. And implement something in the routing
@DotNetDreamer While it may be a generic library app, I don't know exactly how he would like to structure it, so I gave him some generic pointers which he can use when he decides on the design.
Good to know, but doesn't help me work out what controllers I need
0

You want to display books, so you need a BookController and not a LibraryController in my opinion. The BookController fetches a specific book (or list of books) based on the library and / or category that are passed to the BookController as parameters.

The logic to retrieve data should be placed in a separate class library if you want to set things up properly (divide UI, business logic, data access)

1 Comment

That makes sense, although I also need to display and search for libraries, so I'm leaning towards both controllers

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.