C# – Web API – strong classes or dynamic

asp.net-mvc-web-apicdynamic-typingpocoweb-api

My web api method should return some structured data about my profile. To do it I created the following POCO classes:

public class ProfileInfo
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    //....

    public ProfileAddress Address { get; set; }
    public ProfileDriverLicense DriverLicense { get; set; }
    public ProfileEmergency Emergency { get; set; }
    public ProfileEmployment Employment { get; set; }
    public ProfileCompensation Compensation { get; set; }
    public ProfileFuel Fuel { get; set; }
    public ProfileCompanyInfo CompanyInfo { get; set; }
}

public class ProfileAddress
{
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
}

public class ProfileDriverLicense
{
    //....
}

public class ProfileEmergency
{
    //....
}

public class ProfileEmployment
{
    //....
}

public class ProfileCompensation
{
    //....
}

public class ProfileFuel
{
    //....
}

public class ProfileCompanyInfo
{
    //....
}

So, as you can see, there are many POCO classes which nested one to another.

service method, which fills and returns ProfileInfo object:

    public ProfileInfo GetProfile(string username)
    {
        var profile = _db.Drivers.Where(p => p.AspNetUser.UserName == username).Select(k => new ProfileInfo()
        {
            FirstName = k.AspNetUser.FirstName,
            LastName = k.AspNetUser.LastName,
            Username = username,
            Address = new ProfileAddress()
            {
                Address = k.StreetAddress,
                City = k.City,
                State = k.State,
                Zip = k.ZIP
            },
            AvatarUrl = k.AvatarUrl,
            CompanyInfo = new ProfileCompanyInfo()
            {
                //....
            },
            Compensation = new ProfileCompensation()
            {
                //....
            },
            DateOfBirth = k.DOB,
            DriverLicense = new ProfileDriverLicense()
            {
                //....
            },
            Email = k.AspNetUser.UserName,
            Emergency = new ProfileEmergency()
            {
                //....
            },
            Employment = new ProfileEmployment()
            {
                //....
            },
            Fuel = new ProfileFuel()
            {
                //....
            },
            Phone = k.PhoneNo,
            SSN = k.SSN,
            Notes = k.Notes,
            Truck = (k.Truck != null) ? k.Truck.TruckNo : null,
            Trailer = (k.Trailer != null) ? k.Trailer.TrailerNo : null
        }).FirstOrDefault();

        if (profile == null)
            throw new Domain.InvalidDriverException(username);

        return profile;
    }

and WebAPI method:

    [HttpGet]
    [Route("MyProfile")]
    public HttpResponseMessage GetProfile()
    {
        string username = GetUsernameFromClaims();
        if (String.IsNullOrWhiteSpace(username))
            return Request.CreateErrorResponse(HttpStatusCode.NotFound, "User not found");

        var profile = _serviceDriver.GetProfile(username);
        return Request.CreateResponse<Domain.POCO.Profile.ProfileInfo>(profile);
    }

it works and works fine. But any benefits to use POCO classes in concrete this case? Or I can use dynamic and have the same effect, with much less code and no disadvantages?
With dynamic I have not to write all these POCO classes and very easy to modify. I understand, that dynamic works much slowly, not easy to find mistake (no compilation errors), but in result Web API returns JSON, which is not strong type by default. What sense to create and use POCO classes?

PS. I use strong types, it's more comfortable for me 🙂
But probably for concrete case I write much extra-code?

Best Answer

The advantage of having a strongly-typed return value is that you can have a descriptive document (for example swagger using Swashbuckle) that allows people to auto-generate a client for your API.

If you just return JSON data that is not documented what it looks like, not only do you have to build it dynamically without much error checking on your part, the client will have no way to know how it looks like. So that's trial and error on both sides.

Related Topic