Solved

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

Posted on 2015-01-05
4
126 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video teaches users how to migrate an existing Wordpress website to a new domain.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

726 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