質問

I have several classes

public class Person
{
   public int Id{get;set;}
   public string Name{get;set;}
   public virtual Institution Institution{get; set;}    
}
public class Institution
{
    public int Id{get;set;}
    public string Name{get;set;}
    public virtual InstitutionType InstitutionType { get; set; }

}
public class InstitutionType
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool Active { get; set; }
    public virtual ICollection<Institution> Institutions { get; set; }

}

This class is a code first model that uses Entity Framework to pull records from tables in a DB. Often there are situations where there is no InstitutionType in the Institution table.

When I use the following code and there is no InstitutionType data for the record I get a Null Reference Exception.

cm.InstitutionTypeId = person.Institution.InstitutionType == null ? 0 : contact.Institution.InstitutionType.Id;

How should these situations be handled? Should my classes be redesigned?

役に立ちましたか?

解決

(Edited to show lazy loading code sample)

Currently, there's no way around checking for a null reference before using a property of the possibly null object.

cm.InstitutionTypeId = (person.Insitituion != null && person.Institution.InstitutionType != null) ? contact.Institution.InstitutionType.Id : 0;

C# does not currently have a safe navigation operator, but according to this:

http://blogs.msdn.com/b/jerrynixon/archive/2014/02/26/at-last-c-is-getting-sometimes-called-the-safe-navigation-operator.aspx

We'll probably be getting one soon: "?."

Alternatively, you can have your Institution property lazy load a new Institution instance, which has initializes with an InstitutionType whose ID is 0. Depending on the business logic you need when using these objects, this may or may not be the best approach. Since these are entity objects, you'll want to make sure you understand the data implications of creating new instances rather than possibly setting a reference to an existing default Institution or InstitutionType. Hope this helps.

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }

    private Institution _Institution;
    public Institution Institution
    {
        get { return _Institution ?? (_Institution = new Institution()); }
    }

}
public class Institution
{
    public int Id { get; set; }
    public string Name { get; set; }

    private InstitutionType _InstitutionType;
    public InstitutionType InstitutionType
    {
        get { return _InstitutionType ?? (_InstitutionType = new InstitutionType() { Id = 0 }); }
    }

}

他のヒント

In general, you have to ask yourself: what will you not be able to do if the data are not present, and what do you want to do instead?

In your example, you won't be able to do much of anything if person is null. In fact, your code should probably never have been entered if person is null, so that's a good place to throw an ArgumentNullException or something like it.

You won't be able to do anything with institutions if person.Institution is null. You'll have to skip over all the code the does things with institutions, but you may or may not want to throw an exception. Probably not.

You can't do anything having to do with the institution type if person.Institution.InstitutionType is missing, but you could probably come up with default values. You might want to treat a null InstitutionType the same as if you had an InstitutionType with ID=0, Name=String.Empty and Active=false.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top