views:

44

answers:

1

I have a question re: performance and design. Crux of the problem is: do I wrap RunWithElevatedPriviledges around a sequence of methods all requiring its use (but the call is in the wrong layer), do I call it each time in the appropriate later.

I am refactoring code that initially had 2 layers (front-end & database).

I've now split this out into 4 layers

  • Front-end (Sharepoint)
  • Service-layer
  • Data-access-layer
  • Database

Now, owing to a prescribed configuration I need to throw RunWithElevatedPriviledges around the sql queries so that the app pool can authenticate against the database (please dont post comments telling me this is a bad idea, I realise this is a terrible idea but it's not my call).

For each data call I will have to be in elevated privileges. For my mind, this should happen in the Service layer. The FE should have no clue about this requirement and neither should the Data Access Layer. However, the service layer is called multiple times to populate a series of dropdowns

i.e.

userlistbox.datasource = MyService.GetUsers(); //Returns an iEnumerable object
citylistbox.datasource = MyService.GetCities(currentRegion); //Returns an iEnumerable object

etc.

The unrefactored version wraps all of these calls with the elevated privileges. My sense is that it will be slightly less performant to push this into the service layer but that it's a better design.

Thoughts? And please, no bashing the constraints I can't do anything about.

+1  A: 

Better in the Service layer than in the Front End. Maybe you could pass the SPSecurity.CodeToRunElevated Delegate?

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.codetorunelevated.aspx

IrishChieftain
I'm inclined to agree re: service layer, though I don't believe passing a delegate will help in my case as it implies my Front-End knows about the elevated privileges.I suppose what I could do is pass the service my controli.e. Service.PopulateUsersListBox(userListBox);But then that requires the service to know about the Front end.ARG!
Knowledgethoughts
Maybe you could somehow "register" the page's controls with the Service layer dynamically at load time?
IrishChieftain
Technically, it's no problem. I'm struggling with whether it's a good idea.Perhaps UserList needs to be a domain object with the UserListBox encapsulating it, essentially having an instance (i.e. UserListBox.Data = new UserList());Additionally, aggregated methods would be something like PopulateForm(){Service.PopulateData(UserListBox.Data, CityListBox.Data, etc))UserListBox.Update();CityListBox.Update();}That feels a little more tidy, although requires more code....
Knowledgethoughts
I really think that it's not a major sin to make the calls in the front end under elevated privileges... just my two cents :-)
IrishChieftain