Aviran Cohen
Aviran Cohen

Reputation: 5691

Object oriented database models

UPDATED WITH CODE!

I am developing a web application using ASP.NET MVC 3 and I want to create an object-oriented list using "ADO.NET Entity Data Model".

I've created models by entity framework "Code-First" using "ADO.NET Entity Data Model" and then generated a database. Afterward I have generated an entire DAL using "Reverse engineer Code First" from the database that contains my classes plus "DataContext" object.

I have a simple type called "Item", contain "Name" property. In addition, there are some derived types such as "Weapon" and "Armour", containing exclusive properties such as "Damage" and "Defense" respectively.

As a common object-oriented way, I want to iterate over the Items being able to see the exclusive properties of each type.

This is my purpose, being able to iterate over all the items a player has, including their properties, Even though they are derived from Item:

        // Creating a game context that derives from DbContext
        GameContext context = new GameContext();

        // Take my profile
        Player me = context.Players.First();

        // Get my items and show them, show both 
        var myItems = me.Items;

        // Printing all the items that I have with the properties of the derrived objects as well
        // ("Weapon" and "Armour" objects as well (They derive from "Item" object)
        foreach (var item in myItems)
        {
            // Accessing the item's name. every item has this property, so there is nothing special.
            Console.WriteLine("Name:" + item.ItemName);

            if (item.Type == "Weapon")
            {
                // Accessing the item's damage, which is uniqe to a weapon (that derrives from "Item" object).
                Console.WriteLine("Damage:" + (Weapon)item.ItemDamage);
            }

            if (item.Type == "Armour")
            {
                // Accessing the item's defense, which is uniqe to an armour (that derrives from "Item" object).
                Console.WriteLine("Defense:" + (Armour)item.ItemDefense);
            }
        }

From now further this is what I have and I am not satisfied with, even though I think this is unnecessary.

My current model relations (That I am not satisfied with): These are my models created in a "ADO.NET Entity Data Model"

Item base class that contain the different items, NOTE that the use of an "Item" object is just like the use of an abstract class - It only going to be used by its derived classes - "Weapon", "Armour", "Jewelry" and so on.

Weapon which contain a navigation property the appropriate item, plus its own properties (Damage in this case).

ConcrateItem is an item that a player has. which means its contains a navigation property (being used just like pointer) to that item, plus the number of upgrades that this instance of item was upgraded, since it is individually per item and not shared among all the items of this kind.

What I've done at first was to link the weapon and armour types to the appropriate item type, so I can access the item base properties if i iterate over the Weapons table or over the Armour table, but the problem is that I Want to iterate over all items at once and from this point to be able to get an exclusive properties and not to iterate over every table that contains a specific class derived from Item.

Does anyone know how can I achieve that?

Upvotes: 0

Views: 2231

Answers (2)

Aviran Cohen
Aviran Cohen

Reputation: 5691

I've thought about a way to do it, and after that I realized that the auto-generate classes from database was a bad tool, and I used instead:

  • Right click and select ‘Add Code Generation Item’.
  • From the ‘Add New Wizard’ select ADO.NET DBContext Generator. You’ll see it gives a Model1.tt (t4 template) name.
  • Change the name if you want to and select Add. You will see under in the class library a node gets added with the t4 template. When you expand the node you have the C# (or VB) classes for your entities.

Source: http://sumitmaitra.wordpress.com/2011/07/14/entity-framework-and-ms-web-stack-of-love-first-steps/

This gave me Data access layer that functionate great with OOP.

Here is the final code (which is working):

        // Note that ModelsContainer derives from DbContext
        ModelsContainer context = new ModelsContainer();

        var weapons = context.Items.OfType<Weapon>();

        foreach (var weapon in weapons)
        {
            Console.WriteLine(weapon.Name + ":" + weapon.Attack);
        }

        var armours = context.Items.OfType<Armour>();

        foreach (var armour in armours)
        {
            Console.WriteLine(armour.Name + ":" + armour.Defense);
        }

Upvotes: 1

Jayprakash S T
Jayprakash S T

Reputation: 243

Use EntityFramework CodeFirst to retreive the data for the table and then use the ASP.NET MVC cache feature to store the end user related data in the cache at the remote end user machine. In this way you can avoid server trips for retrieving data from each table for every event at the end user side.

http://weblogs.asp.net/scottgu/archive/2010/01/27/extensible-output-caching-with-asp-net-4-vs-2010-and-net-4-0-series.aspx

Upvotes: 1

Related Questions