views:

199

answers:

2

I'm coming from a background in ColdFusion, and finally moving onto something modern, so please bear with me.

I'm running into a problem casting objects. I have two database tables that I'm using as Models - Residential and Commercial. Both of them share the majority of their fields, though each has a few unique fields. I've created another class as a container that contains the sum of all property fields. Query the Residential and Commercial, stuff it into my container, cunningly called Property. This works fine.

However, I'm having problems aliasing the fields from Residential/Commercial onto Property. It's quite easy to create a method for each property: fillPropertyByResidential(Residential source) and fillPropertyByCommercial(Commercial source), and alias the variables. That also works fine, but quite obviously will copy a bunch of code - all those fields that are shared between the two main Models.

So, I'd like a generic fillPropertyBySource() that takes the object, and detects if it's Residential or Commercial, fills the particular fields of each respective type, then do all the fields in common. Except, I gather in C# that variables created inside an If are only in the scope of the if, so I'm not sure how to do this.

    public property fillPropertyBySource(object source)
    {
        property prop = new property();
        if (source is Residential)
        {
            Residential o = (Residential)source;
            //Fill Residential only fields

        }
        else if (source is Commercial)
        {
            Commercial o = (Commercial)source;
            //Fill Commercial only fields
        }
        //Fill fields shared by both
        prop.price = (int)o.price;
        prop.bathrooms = (float)o.bathrooms;

        return prop;
    }

"o" being a Commercial or Residential only exists within the scope of the if. How do I detect the original type of the source object and take action?

Bear with me - the shift from ColdFusion into a modern language is pretty..... difficult. More so since I'm used to procedural code and MVC is a massive paradigm shift.

Edit: I should include the error: The name 'o' does not exist in the current context For the aliases of price and bathrooms in the shared area.

+4  A: 

This is best done with an interface like this:

public interface IProperty 
{
   int price { get; set; }
   float bathrooms { get; set; }
}

public class Residential : IProperty
{
   public int price { get; set; }
   public float bathrooms { get; set; }
   //Other stuff...
}

public class Commercial : IProperty
{
   public int price { get; set; }
   public float bathrooms { get; set; }
   //Other stuff...
}

Then in your method, take the interface instead of object:

public property fillPropertyBySource(IProperty source)
{
    property prop = new property();
    if (source is Residential)
    {
        Residential o = (Residential)source;
        //Fill Residential only fields

    }
    else if (source is Commercial)
    {
        Commercial o = (Commercial)source;
        //Fill Commercial only fields
    }
    //Fill fields shared by both
    prop.price = source.price;
    prop.bathrooms = source.bathrooms;

    return prop;
}

An interface is used when your classes have common properties that you want to access. You could also have a base class Property with price and bathrooms and inherit from that instead, whichever you prefer.

Nick Craver
Am I missing something, Nick? It looks like this will still kick out an out-of-scope ref exception...? Definitely agree with the Interface approach, though.
Gus
@Gus - Woops missed the rename on those variables, fixed!
Nick Craver
I'm afraid I'm not following. Residential and Commercial are generated from the database directly, via a Linq To SQL deal. I don't actually have class definitions for them in code. The database has fields like "bathrooms" and "price", which is what I need to get from the source object into my manually crafted class of "property", which also has all those fields, which I intend to fill with data from either Residential or Commercial. I'm not sure how to add interfaces to the Linq to SQL generated model, but Residential and Commercial already have the fields of bathrooms and price.
Mortanis
@Mortanis - You can add the interfaces (or base class) via a partial class pretty quickly, see this question for details: http://stackoverflow.com/questions/1609215/can-you-implement-an-interface-on-a-linq2sql-class
Nick Craver
@Nick - That did it! Many thanks!
Mortanis
+1  A: 

Because of the artificial constraints imposed on you by using database-generated object types instead of hierarchically defined ones, there's really no elegant solution to this at all. The best solution I can offer is to use a partial class to attach a known interface and then use overloading to do the minimum amount of work required when copying the object:

interface IPropertyBase
{
    decimal Price { get; set; }
    int Bathrooms { get; set; }
}

partial class Residential : IPropertyBase
{
}

partial class Commercial : IPropertyBase
{
}

class Property : IPropertyBase
{
 public decimal Price { get; set; }
 public int Bathrooms { get; set; }

    // Commercial
    public int Offices { get; set; }

    // Residential
    public int Bedrooms { get; set; }

    private void CopyFromBase(IPropertyBase o)
    {
        Price = o.Price;
        Bathrooms = o.Bathrooms;
    }

    public void CopyFrom(Commercial o)
    {
        CopyFromBase(o);

        Offices = o.Offices;
    }

    public void CopyFrom(Residential o)
    {
        CopyFromBase(o);

        Bedrooms = o.Bedrooms;
    }
}

As a general note, "union" objects of this type are often a bad idea. You're better off defining the common properties in an IPropertyBase interface and leaving the Residential/Commercial objects in their "native" form as you work with them. If you need to create a combined collection of all properties in an area, for example, create a List<IPropertyBase> and work directly with that -- it will keep the objects in the list intact (allowing you to determine their original type later using the is/as operators if you need) and it won't have the overhead of lots of empty, meaningless fields on each object.

Dan Story
@Dan - Many thanks. That takes the logic I had in the Controller back to the Model where it belongs. Works great!
Mortanis