开发者

Casting objects in C# (ASP.Net MVC)

开发者 https://www.devze.com 2022-12-25 00:40 出处:网络
I\'m coming from a background in ColdFusion, and finally moving onto something modern, so please bear with me.

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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号