-1

I am learning c# at the moment, I used to program using Unix/C - Oracle and lately I am learning to do windows programming.

Since in c# (or at least using MVC) the data or database table is represented as object/class.

For example say, a simple computer shop part tracking; a shop can purchase a computer and dismantle it into parts and they can sell the parts; or the parts can also be purchase from supplier individually. in relational database we can have something like

Table: Computers
Fields: ComputerId, Maker, PONumber, SerialNumber, DismantleFlag

Table: Parts
Fields: PartId, ComputerId, PONumber, PartDescription, SerialNumber

say if the shop want to trace a part where they but it from, we do query on parts table using part serial number if computerId is null then get the detail where the part is bought from using PONumber, if computerId is not null then use PONumber from computers table.

so if we do the same using model representation I think it would go something like this. (I know the syntax is not correct, it's just a representation)

class Part
{
   properties PartId, PONumber, PartDescription, SerialNumber
}

class Computer
{
   properties ComputerId, Maker, PONumber, SerialNumber, DismantleFlag

   List<Part>Parts
}

so I would assume there will be object for a list of Parts to store all the parts that purchase individually and there is also object for a list computers which in turn has a list of parts inside them if DismantleFlag is Y.

is that mean if I want to do the same operation to trace the parts, I have to go trough the list of parts object, and I also need to go through list of computers object and for each computer object I need to scan through all the parts with in the computer object?

if my understanding is correct then relational database is more simpler to store and manage data, and how it is going to be translated in MVC if the data/model is not represented in object form?

Thank you.

user7293420
  • 33
  • 2
  • 7
  • 1
    If a `Part` can exist independently of a `Computer` and there are business operations which can be performed on a `Part` without any context of a `Computer` then it sounds like a `Part` is also an aggregate root of your business domain. In which case when you want to operate on a given `Part` you would query your data by some identifier for that `Part` and perform your operations on it. Beyond that, it's not really clear to me what else you're describing. It just sounds like you're designing for a model where `Computer` is always the root, but describing a business where it isn't. – David Sep 28 '17 at 16:15
  • 1
    @TheBeardedLlama No, this question as-is is highly inappropriate for Code Review. – Mast Sep 28 '17 at 16:27
  • 1
    Your question has little to do with MVC, it's about the backend and storage. Your 'complaint' would hold for (Azure) Table storage but the same classes could just as well be mapped to relational tables with EF. – H H Sep 28 '17 at 16:55

3 Answers3

1

I am assuming the following rules are possible in your scenario:

  1. Computers have Parts
  2. Parts might or might not be contained in a computer
  3. Parts can migrate from one computer to another
  4. you might want to keep a history of part migration.

Computers and Parts would be completely independent entities. Relationship between Computers and Parts would be Many to Many.

This is the basic model structure I would create given the rules above.

The Boolean Current in the ComputerParts table would represent wether the relation ship is the current one (true if part in this computer or false if part no longer resides in this computer) this approach allows for almost any Computer vs Parts relationships imaginable.

These classes are the most basic of models and would perhaps be modified if you where using an ORM like Entity Framework. Hope this helps

public class Computers
{
    public string ComputerID { get; set;}
    public string Maker { get; set;}
    public string PONumber { get; set; }
    public string SerialNumber { get; set; }
    public Boolean DimantleFlag { get; set; }
    public DateTime DateCreated { get; set; }
}

public class Parts
{   
    public string PartID { get; set; }
    public string PONumber { get; set; }
    public string SerialNumber { get; set; }
    public string PartDescription { get; set; }
    public DateTime DateCreated { get; set; }
}

public class ComputerParts
{
    public string ComputerID { get; set; }
    public string PartID { get; set; }
    public Boolean Current { get; set; }
    public DateTime DateCreated { get; set; }
}
Ibrahim Malluf
  • 657
  • 4
  • 6
  • Hi Ibrahim, so in object model we can normalized data object similar to ER diagram as per your example, I thought when we represent data in object model we need to use thing like inheritance, etc... this is where I am confuse, a lot of example on the net is very simple like album has list of song, so song is always part of (inherit in) album object, which is fine; I know they use this example so they can explain in simple term but not always represent the real world unfortunately. – user7293420 Sep 30 '17 at 01:22
  • Let's get the term inheritance cleared up here. Album has many songs does not comprise an inheritance. An album contains a collection of songs. Inheritance implies an object that is of type object. that is to say that a cat is a type of animal and a dog is a type of animal. So both Dog and Cat inherit from Animal. – Ibrahim Malluf Oct 01 '17 at 04:18
  • I was showing you how a Many to Many relationship actually exists as models, not necessarily how EF would best handle it. In EF you would add a virtual collection of type parts in computers and add a virtual collection of type Computers in Parts. EF would automatically create the association table ComputerParts. But that table would be without the two extra fields I display in this example (Current and DateCreated). – Ibrahim Malluf Oct 01 '17 at 04:31
  • If you want the association table to have extra fields such as I show in the example then you have to declare the table and add virtual collections of Parts and Computers to it. Take a look at this conversation to see what I mean: https://stackoverflow.com/questions/19342908/how-to-create-a-many-to-many-mapping-in-entity-framework – Ibrahim Malluf Oct 01 '17 at 04:35
  • Hi Ibrahim, Thanks for the info and the link – user7293420 Oct 02 '17 at 03:39
0

Often in asp.net-mvc your data actually is stored in a relational database, and you use a framework like Entity Framework to translate the results of sql queries into your model representation.

Entity Framework also supports LINQ, which uses a more Sql-like syntax. So if you're using EF, a similar query might look like this.

// in practice, we might put this in a using statement
var db = new MyDbContext();

var qry = 
    from p in db.Parts
    join c in db.Computers on p.ComputerId equals c.ComputerId
    where p.SerialNumber == mySerialNumber
    select c.PoNumber

string poNumber = qry.FirstOrDefault();

Now, the query you actually seem to want will probably might use a left-join. The left-join syntax in linq is a little funky, but it's at least usable

var qry = 
    from part in db.Parts
    join ljcomp in db.Computers on part.ComputerId equals ljcomp.ComputerId into gcomp
        from comp in gcomp.DefaultIfEmpty()
    where part.SerialNumber == mySerialNumber
    select comp == null ? part.PoNumber : comp.PoNumber

Behind the scenes, EF translates your linq query into sql, and executes it against the database, and then can convert it into your string value, or even into an object if that's what you tell it to select.


You don't have to use Entity Framework, you can communicate with the database in other ways, but for now I'll leave that research as an exercise to you.

  • Hi Sam, at the moment I just create a small & simple project as part of my learning, so there is no existing db, I am aware that some project may already have data in sql db; the reason I put this question is because a lot of exampe/tutorial on the net about MVC is they use code first and use object to represent the data, one of the example from MVA is an album and songs in an album, so they create class song and class album and in album class there is list of song. continue on the comment below... – user7293420 Sep 30 '17 at 01:06
  • I just try to work out the what is the best practice for new project, code 1st or sql 1st, since if it create using entity relation ship, there will be 2 tables, 1 for album and 1 for song, but on the example on MVA it represent as 1 object, album class which has a list of song class/object. – user7293420 Sep 30 '17 at 01:08
  • @user7293420 The above code can certainly be used with a Code-First approach. With a Code first Approach, EF automatically makes a database for you based off of your Models and DbContext. The relational database is where the data is stored. The classes are just how it exposes it to the c# – Sam I am says Reinstate Monica Oct 01 '17 at 03:20
  • Hi Sam, Thanks for the info – user7293420 Oct 02 '17 at 03:40
0

In Model-View-Controller(MVC). There's is different ways of Creating Your Models (Which is the Class You want to create to store your data).There are Code First And Entity framework database first approach

This is a Code first approach example http://tutlane.com/tutorial/aspnet-mvc/code-first-approach-in-entity-framework-in-asp-net-mvc-with-example

This is an Entity framework database first approach example http://www.c-sharpcorner.com/UploadFile/8ef97c/mvc-5-0-application-using-entity-framework-db-first-approach/

Hope this helps

Jephren Naicker
  • 336
  • 1
  • 2
  • 18