devigned / profile-playground

2 stars 1 forks source link

Methods on the Resources rather than the Client #7

Open fearthecowboy opened 7 years ago

fearthecowboy commented 7 years ago

A version where we expose operations as methods on resources as opposed to monolithic 'client' methods.

That would go a really long way to ensuring that operations are correct for a given type.

devigned commented 7 years ago

@fearthecowboy do you mean something like this?

var client = new Client();
var vm = client.Compute.Models.VirtualMachine(...);
vm.Create();  // makes an ARM request to create the Virtual Machine
vm.Color = Colors.Red;
vm.Update(); // makes an ARM request to update the Virtual Machine

Are you implying the models are stateful? That is, the models understand what properties are dirty and would then be able to persist via a patch only the changed properties.

anuchandy commented 7 years ago

I see a problem if expose delete method on the models. For instance

var vm = // get VM
vm.Delete();  // makes an ARM request to delete the Virtual Machine

After this, if user tries to call vm.Create() then that will fail. The reason is when we retrieved the model some of the properties which is required for Create will be null (e.g. password)

anuchandy commented 7 years ago

What if we return different flavors of same model based on the context.

For example

class VirtualMachine {
// Getters for all properties
}
class VrtualMachineWithCreate : VirtualMachine {
  VirtualMachineWithUpdateDelete create();
}
class VirtualMachineWithUpdateDelete : VirtualMachine {
  void delete();
  VirtualMachineWithUpdateDelete update();
}
class Compute.Model {
   VrtualMachineWithCreate VirtualMachine(...);
}
class ComputeClient {
  VirtualMachineWithUpdateDelete GetVirtualMachine(...);
}

This will block user from calling unsupported action to an extend, for example they are not allowed to call create() on the model returned by GetVirtualMachine(...), though they can call update() after delete.