jeudi 3 septembre 2015

Where to validate type creation parameters

I've seen and used all sort of different ways to create objects, but haven't yet found any pattern to follow when validating the data needed to create that same object.

That said, what's the preferred way of doing it?

public SomeClass(Product product, Device device, PlatformType platformType,    User user, Country country)
{
if (environment == null || environment.Id == 0) throw new ArgumentNullException("environment");
if (device == null || device.Id == 0) throw new ArgumentNullException("device");
if (country == null) throw new ArgumentNullException("country");
if (userContext == null) throw new ArgumentNullException("userContext");
if (platformType == null) throw new ArgumentNullException("platformType");

Product = product;
Device = device;
PlatformType = platformType;
UserContext = userContext;
Country = country;
}

OR

public static class SomeClassFactory()
{
    public static SomeClass Create(Product product, Device device, PlatformType platformType, User user, Country country)
    {
    if (environment == null || environment.Id == 0) throw new ArgumentNullException("environment");
    if (device == null || device.Id == 0) throw new ArgumentNullException("device");
    if (country == null) throw new ArgumentNullException("country");
    if (userContext == null) throw new ArgumentNullException("userContext");
    if (platformType == null) throw new ArgumentNullException("platformType");
    }

    return SomeClass(product, device, platformType, user, country);
}

Aucun commentaire:

Enregistrer un commentaire