CodeFreak
CodeFreak

Reputation: 13

C#: How can I expose a class property to one class but not to another?

I have a data contract "StudentInformation" in my class library, something like this:

public class StudentInformation
{
    public int StudentId { get; set; }
    public bool CanChangeBus { get; set; }
    public List<int> AvailableBuses { get; set; }
}

public class BusChangeRequestModel
{
    public StudentInformation StudentInfo { get; set; }
    ...
    ...
}

public class BusChangeResponseModel
{
    public StudentInformation StudentInfo { get; set; }
    ...
}

The Request model sends in StudentId, the class library processes the information and populates the properties "CanChangeBus" and "AvailableBuses", which are then returned in the response model.

I want to hide the properties "CanChangeBus" and "AvailableBuses" from the request model. If I change the setter of these properties to "internal" then the properties cannot be set by calling application but they are still visible. How can I hide them from calling application's instance of request model?

Upvotes: 1

Views: 896

Answers (3)

Charlie
Charlie

Reputation: 1293

Using inheritance. Something like:

public class StudentBase
{
    public int StudentId { get; set; }
}

public class StudentInformation : StudentBase
{
    public bool CanChangeBus { get; set; }
    public List<int> AvailableBuses { get; set; }
}

public class BusChangeRequestModel
{
    public StudentBase StudentInfo { get; set; }
    ...
    ...
}

public class BusChangeResponseModel
{
    public StudentInformation StudentInfo { get; set; }
    ...
}

Upvotes: 1

XenoPuTtSs
XenoPuTtSs

Reputation: 1284

So BusChangeRequest only needs to see the id, then it should only have the id.

public class StudentInformation
{
    public int StudentId { get; set; }
    public bool CanChangeBus { get; set; }
    public List<int> AvailableBuses { get; set; }
}

public class BusChangeRequestModel
{
    public int StudentId { get; set; }
}

//I don't know what is expected as a repsonse?  Is StudentInfromation really the correct response
//or is something like "Accepted", "rejected", Fail...???
public class BusChangeResponseModel
{
    public StudentInformation StudentInfo { get; set; }
}

Upvotes: 0

Riad Baghbanli
Riad Baghbanli

Reputation: 3319

public class BasicStudentInformation
{
    public int StudentId { get; set; }
}

public class StudentInformation : BasicStudentinformation
{
    public bool CanChangeBus { get; set; }
    public List<int> AvailableBuses { get; set; }
}

public class BusChangeRequestModel
{
    public BasicStudentInformation StudentInfo { get; set; }
    ...
    ...
}

public class BusChangeResponseModel
{
    public StudentInformation StudentInfo { get; set; }
    ...
}

Upvotes: 3

Related Questions