Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to model this entity

I am creating an application for a shop, where clients are divided into two groups: receivers and payers.

1) Many receivers can be related to one same payer. 2) Payer can be receiver itself, thus he is related to one receiver only

My code so far look like this:

public class Receiver
{
    [Key]
    public int ReceiverId { get; set; }
    public string Name { get; set; }
    [Required]
    public int PayerId { get; set; }
    public virtual Payer Payer { get; set; }
}

public class Payer
{
    [Key]
    public int PayerId { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Receiver> Receivers { get; set; }
}

However I need to let the user create a new payer that is also a receiver at the same time, so the Receivers collection will have only one element. To do this I want to use a checkbox, which will be translated to a new column in the database (IsAlsoReceiver).

I am using MVC scaffolding for creating views, and it created two views for adding Receiver and another for adding Payer. When a user needs a Payer that is also a receiver he has to add entities in both views; can that be simplified in this scenario?

I am asking for a smart way to do so.

like image 442
kul_mi Avatar asked Aug 10 '14 09:08

kul_mi


People also ask

What are entity models?

The Entity Data Model (EDM) is a set of concepts that describe the structure of data, regardless of its stored form. The EDM borrows from the Entity-Relationship Model described by Peter Chen in 1976, but it also builds on the Entity-Relationship Model and extends its traditional uses.

What is a business entity model?

A business entity model is a logical model that documents the entities, or things, that a business or business process uses and interacts with in order to accomplish its business activities and goals.

Is model same as entity?

Entity: An entity represents a single instance of your domain object saved into the database as a record. It has some attributes that we represent as columns in our tables. Model: A model typically represents a real world object that is related to the problem or domain space.

What is an entity model used for?

The Entity Framework enables developers to work with data in the form of domain-specific objects and properties, such as customers and customer addresses, without having to concern themselves with the underlying database tables and columns where this data is stored.


1 Answers

I think you are missing an abstraction layer. When you build a project of a certain size (a size of which I think you have reached) you need each component (data layer, logic layer and views) to see different models.

The Data Access Layer (DAL) has Domain Models

The Logic Layer or API has Data Transfer Objects

The Views have View Models

To make it easy to build and to understand I'm just using Domain Models and View Models.

For your current problem I would build it like so:

//this is the view model
//The user sees this model
public class Person {
    public string Name { get; set; }
    public bool IsReceiver { get; set; }
    public bool IsPayer { get; set; }
}
//these are the domain models
//The database uses these models
public class Receiver {
    [Key]
    public int ReceiverId { get; set; }
    public string Name { get; set; }
    [Required]
    public int PayerId { get; set; }
    public virtual Payer Payer { get; set; }
}
public class Payer {
    [Key]
    public int PayerId { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Receiver> Receivers { get; set; }
}

public class AccountController {

    //create a view for the Person View Model
    //this is your abstraction
    public void SignUp(Person p) {
        //create models for the repository
        Payer payer = new Payer() {
            Name = p.Name
        };
        Receiver receiver = new Reciever() {
            Name = p.Name
        };

        //if the payer is his own receiver:
        Receiver.Payer = payer;

        //if the payer is his own receiver:
        Payer.Receivers.Add(receiver);


        //create for each depending upon some condition
        //I've just allowed the user to select whether he is a payer
        //based upon a checkbox on the form I suppose
        if (p.IsPayer) {
            db.Payers.Add(payer);
        }
        if (p.IsReceiver) {
            db.Receivers.Add(receiver);
        }

        db.SaveChanges();
    }
}
like image 146
Smithy Avatar answered Oct 26 '22 01:10

Smithy