I am using C# to create a view model that I later serialize into Json for use with KnockoutJs.
Now I need to add information on a property level if a certain user has access to view and/or edit the property.
Since Javascript has little to no reflection possibilities I want to do this with C# before serializing to Json.
My view model could look like this:
class ViewModel {
public string Name { get; set; }
}
I want to use a User Access Service that would take the user, which has some Roles, and the view model and go through the Roles and apply the access rights. Something like this:
class AccessService {
public void Apply(IUser user, ViewModel viewModel) {
if(user.Roles.Contains(Roles.Admin)) {
viewModel.AllowRead(vm => vm.Name);
}
}
}
The viewModel.AllowRead(..) method would be an extension method that would take an object (or maybe an interface or type if necessary) and this is where the magic would happen.
I would like the result of this operation to be that the viewModel would get a new property with the name CanRead which in turn would have a boolean property with the name Name.
The resulting viewModel would look like this:
class ViewModel {
public string Name { get; set; }
public object CanRead { // Could non anonymous as well.
return new {
Name = true;
};
}
}
Can this be done with dynamics or do I have to use Reflection.Emit? I'm not asking for "show me the codez". I just want to know if my idea is mental or if it's possible.
Any ideas?
ViewModelused in WPF?