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?
dynamicwould cope with this with no problem at all - but the consumer might blow up. That's not easy. It's easier to do what you're saying in the here and now, of course, but a lot of 'sound' programming principles occasionally cost a little bit more time. It's time well spent. EDIT: Not to say there's no place for the use ofdynamic, there is.why not easy to modify?Maybe easy to modify, but hard to maintain. Developers mainly measure code complexity by the difficulty of figuring out what to change, as opposed to the actions needed to make a change in something you already understand. Your dynamic approach makes it very hard to figure out what the names of the properties should be. Comparatively, if these property names are available via Intellisense, a developer doesn't need to worry if he made a typo or not, because the compiler will tell him automatically if he used a non-existing property name.Colorproperty. Now hire a British developer who instinctively usesColour(UK spelling). The compiler does not tell him he's wrong, since the compiler doesn't bother to factcheck dynamics. If the British developer had had Intellisense, he would've immediately seen the issue. Without Intellisense, and with a completed build, the developer is now forced to go on a bug hunt because something broke and he doesn't know what. The time cost between the two is immense, and this happens for every typo any developer can make.