Are there alternative ways to register helpers with controllers in MonoRail? - helper

In MonoRail controllers can be adorned with the Helper attribute to make helpers available in views. Is there another way to register helpers, perhaps via configuration file or dependency injection, with controllers? I'm keen on avoiding creating a base controller just for the purposes of providing helpers and adding yet another controller to the controller hierarchy.

IControllerContext has a Helpers dictionary. All controllers expose IControllerContext via the ControllerContext property.
Another option is to implement an IHelperDescriptorProvider to replace the default one (which reads helper descriptors from [Helper] attributes). Your implementation could read helper configurations from a config or whatever you want.

Related

ServiceStack's SerializeFn custom serializer/deserializers - sticks across requests?

I'm using ASP.Net with MVC, and would like to have custom SerializeFn for only certain requests. It looks like the JsConfig stuff is static, and I do see the JsConfig.BeginScope() stuff to keep the config scoped, but it doesn't seem to apply to the custom serializers/deserializers. If I define a custom SerializeFn, is it going to stick around across requests because it's static? If so, is there some way to stop that from happening?
Thanks.
The Custom SerializerFn's are static and not overridable in JsConfigScope scoped configuration so they do apply to all requests.
As custom serializers can't be limited in scope, one solution would be to map it to a custom type and attach the serializer to that:
public class CustomPoco : Poco {}
JsConfig<CustomPoco>.SerializerFn = //...;
So when you need special serialization you can map it to your custom type and serialize that:
var customType = dto.ConvertTo<CustomPoco>();
var jsonWithCustomSerialization = customType.ToJson();

Kohana 3.3 - how to correctly move controller function to model

again I've got a question about Kohana and how I am supposed to use model functions.
I want to move parts of a controller function into a more appropriate model to be able to access this function from additional controllers. (From what I have read so far I conclude that calling the controller function from a different controller is considered bad architecture).
My problem is that depending on several circumstances (i.e. model parameters) this controller function creates a log entry in a different database table and sends an email to some users.
How am I supposed to create this log entry and send the mails if main functionality resides inside the model? Should I instantiate the second model from within the first, call the log function and afterwards send the mails exactly how I did from my controller?
Thanks in advance.
This is a question that doesn't have 1 correct answer, unfortunately. A lot of it comes down to how you prefer to implement the MVC pattern.
At its base MVC uses: Models, Views and Controllers
Models
These classes should represent entities in your database
Example:
Model_User maps to an entity in your Users table
$user = new Model_User;
$user->first_name = 'Joe';
$user->last_name = 'Smith';
$user->save();
Views
These files store presentation data/templates (html usually/mostly)
Example:
index.tpl
<h1>HELLO, WORLD!</h1>
<h2><?=$some_variable_from_controller?></h2>
Controllers
These files handle incoming requests and process data to be injected into views
Example:
Controller_Home handles request to the home page
class Controller_Home extends Controller {
public function action_index(){
$view = View::factory('index');
$view->render();
}
}
Now that you get the basics it's time to understand a specific problem this limited structure promotes. Controllers get kind of fat and messy. This leads us to libraries or a service oriented architecture.
These libraries allow us to move large groups of logic into a portable service layer that can easily be used across all controllers, models and other libraries. They also break our code up into smaller more concise chunks that actually made sense.
Example:
In my controller instead of using a bunch of logic that logs a user in via facebook I can simply create a Social_Login_Service and use it as follows.
Social_Login_Service::facebook($user_email);
Now you would simply see that 1 clean line in your login controller instead of a whole messy slew of logic that will pile up in your controller until it melts your brain to looks at.
This is a very basic overview of a possible direction (and one that I prefer).
It is very useful to break up your sites into smaller components and if you're using Kohana, I recommend doing this with Modules (http://kohanaframework.org/3.1/guide/kohana/modules) they're great.
I hope this little snippet helped.

Purpose of AssertRequiredRoles?

I am implementing my own RequiredRole attribute called RequiredAnyRole, whereby I pass in a list but the user only has to be in 1 of the roles. I have implemented my own method called HasAnyRole which simply queries based on .Any() instead of .All().
I have then overridden the Execute method to use my method rather than HasAllRoles. The problem is im not sure what the method: AssertRequiredRoles is doing? It doesn't seem to be called?
Should I override that to use .Any() rather then .All() too? Here is the original code:
https://github.com/ServiceStack/ServiceStack/blob/82241fc96e187d12f9db2556aea37cf327813adc/src/ServiceStack.ServiceInterface/RequiredRoleAttribute.cs
AssertRequiredRoles is a static helper method that's can be used by other plugins like RequestLogsService to ensure access is only granted to users with the required roles. It's not called when used as a normal attribute filter.
Once you override Execute you retain full control of what gets executed, so you only need to override what you need.

Custom Control Custom Methods?

I have been making good use of custom properties withing custom controls. Is there such thing as custom methods? Say I want something to happen in a CC. A good example is the show method of the dialog box extension. If I have a cc with a extension dialog inside, I want my custom control to have a Show method which insulates the end user programmer from the extension pages Shoe method.
Is there anyway to do this?
At runtime, all Custom Control elements become instances of the UIIncludeComposite class; as such, there are many built in methods that you can call against any given control instance, but there is no way to specify custom methods, as opposed to custom properties.
There are, however, at least two ways you could achieve the result you're after:
Convert your Custom Control to a component (this NotesIn9 episode describes the simplest approach to this process). Once you've migrated the class that Designer generated to one that won't get overridden every time you build your NSF, you can add custom methods without fear that the next build will just wipe them out again. Since Custom Controls are essentially just IBM's implementation of the JSF 2.0 notion of "composite components", you could also create a component from scratch that has the same behavior as your existing Custom Control but also supports custom behavior. Note that either approach does not necessarily require that you create an OSGi library... you can define these components directly in an NSF; you only need to push them to a library if you want to reuse them across multiple NSFs without having to copy the various files to each.
In the custom properties for your control, include one property that accepts an API object. In other words, you could create any object (say, a Java class or SSJS object) that supports the custom methods you wish to define, and pass that object to the control. You could then call those methods by getting a handle on the object via the CC's property map.
For example:
<myCC id="myCustomControl" API="#{someObject}" />
Assuming whatever #{someObject} resolves to includes a show() method, you can call that method by getting a handle on the instance that has been passed to the control:
var cc = getComponent("myCustomControl");
var ccProperties = cc.getPropertyMap();
var ccAPI = ccProperties.get("API");
ccAPI.show(cc);
In the above example, I'm passing the actual Custom Control to the show() method, because the object itself isn't aware of the Custom Control it was passed to. So if that method needs to get a handle on its children to toggle their rendered property, for example, then it needs some other way of determining its context.
Tim's solution with passing in the object is a great solution to that.
Just something that popped into my head, would be easy to make a property similar to the rendered property on a control. Pass in a value and inside the custom control do something based on its value ie. if true display dialog, else hide, in the XPage during run time modify this value and partial refresh the control, the logic will be re run by this and the control will display etc.
Another solution could be to include a JavaScript library in your custom control providing functions (your custom control methods) where you'd have to pass in the id of the custom control instance.

SubSonic: BeforeDelete

SubSonic provides the overrideable methods BeforeUpdate and BeforeInsert for doing validation. Is there a way to hook into the BeforeDelete event, if such a thing even exists?
I am not aware of any BeforeDelete event but you could always override the Delete method and handle it there. Just remember to put any overridden methods in a seperate file(SubSonic classes are partial) the convention is to put that file in an "Altered" folder and handle that there.
From what I remember, objects don't have a Delete method. You need to delete via a controller (at least in my templates!).
In my templates, I have methods PreDelete/PostDelete, PreDestroy/PostDestroy. We may have added these ourselves.
Take a look at this article with some sample code:
http://blog.lavablast.com/post/2008/08/SubSonic-v21-Controller-and-Utilities.aspx
(we have other articles on the same controllers on our blog - but they were for v2.0).

Resources