Solved

One object/table or two? - Loan/Return model (ASP.NET MVC 5)

Posted on 2015-01-05
4
69 Views
Last Modified: 2016-06-22
Hi all

Just looking for a bit of general advice (what would you do)...

I'm building a fairly standard MVC 5 website with C# in VS 2013.4, using EF6.
User and Book models are setup, with some extra support tables used to generate Select Lists on Create/Edit views.

I'm now at the stage where I need to decide how I'm going to implement my Loan / Return model and logic. These two events occur on different days, and at any given time I'd like to be able to generate views for books that are loaned out (and not yet returned). Books are all unique.

Potential option one -
have separate model classes (and tables) for Loans and Returns, using a foreign key relationship.
Main benefit is I can use Data Annotations (validation) on the Return form as it is separate, without having to add controller action code to ignore null data errors. Separate controllers would be needed for each model class. Not sure if the Return model would have a LoanID or BookID property for the Foreign Key at this stage.

Potential Option 2 -
have one model class/table that contains properties for Loan details and Return details (such as Return Date). Index views to display data should be simpler to code (did I mention this is my first C# and ASP.NET project?) and possibly might have some small performance improvement.
The Loan\Create view would not display the Return form controls, and the controller action would include ModelState.Remove() code.
A new Return view (based on the scaffolded Edit view) would allow for the row to completed later and somehow employ data validation on the Return properties.

Potential option 3
Have some sort of inheritance model, where either Returns : Loans, or bother Loans and Returns inherit from a higher Transactions base class.
tblLoans and tblReturns couldn't be related with a foreign key in this configuration as it creates an infinite loop.


Some other notes that may have an impact:
1. There are only 100 individual books to be loaned out one at a time to ~10 users.
2. Loan and return events also need to be recorded in hard copy - so I would create Index views that list rows where HardCopy = false for easy synchronisation at the end of the week.
3. Complete Loan/Return transactions would need to be counted for periodical statistics reports.
4. Book has a .Status property which will logically related to Loans. i.e. Available, In use, overdue, etc - set in various controller actions)

Current model (based on Option 1) that I'm playing with (some annotations removed for brevity):

[Table(name: "tblLoans")]
    public partial class Loan
    {
        [Key]
        public int LoanID { get; set; }

        [Required]
        public int BookID { get; set; }

        [Required]
        public int UserID { get; set; }

        [Required]
        public int CampaignID { get; set; }

        [Required]
        public DateTime LoanDate { get; set; }

        [Display(Name = "Loan Hard Copy")]
        public bool CommitLoan { get; set; }

        // Return data below - required attributes removed in Controller for creating/editing loans, only required for creating/editing returns

        [Required]
        public Nullable<DateTime> ReturnDate { get; set; }

        [Required]
        public int CompletionID { get; set; }

        public Nullable<bool> CommitReturn { get; set; }

        public virtual Book Book { get; set; }
        public virtual ApplicationUser User { get; set; }
        public virtual Campaign Campaign { get; set; }
        public virtual Completion Completion { get; set; }
    }

// other related partial classes...

Open in new window



If you've been in this situation beofre I'd be interested in what other pros/cons and challenges to anticipate for either option, and which way you would go.

TIA
0
Comment
Question by:Ryan_R
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 40533398
Logically, it sounds like you would just need a single table to track loans/returns.  Yes, a loan and a return are two separate actions from a user perspective, but data wise, they are simply changes of state on a single entity (i.e. a record of a loan).

You could use multiple models to deal with subsets of a loan record if you need to handle them differently in the application, but data-wise it feels like they should be kept together.  The important thing is to make distinction between how the data elements are stored and how they are used/presented by the application.
1
 
LVL 15

Author Comment

by:Ryan_R
ID: 40534688
Thanks Carl, that's the way I was also leaning (as evidenced by the current model code ^).
How would one deal with data validation in this scenario: when an item is Returned, those returned fields will be required... they just won't be when the Loan part is initially created.

I thought I could set the database fields to be nullable and use Data annotations in the model (with the return model view errors being ignored in the Create POST action), but with Code First migrations enabled I'm not sure if it's possible.
0
 
LVL 15

Author Comment

by:Ryan_R
ID: 41665643
Sorry Carl. You're welcome to the points as I followed that course successfully. I just can't seem to find the button in this interface.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction (All good things must come to an end (http://en.wikipedia.org/wiki/All_Good_Things...)) The original MySQL API (http://php.net/manual/en/book.mysql.php) has gone away, deprecated by PHP in Version 5.5, and removed from PHP in all curre…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now