Where should be defined an action on entity - symfony

sorry because my question is dum but I prefer to ask to organize correctly my code.
Actually, in a Controller, I do an action like 'get entity + set activated + persist + send a mail'.
I know this should be placed somewhere else to be usable by others controllers.
I guess that it should be a service, but I got an hesitation, it could be in the model? but given the send mail action, I'm not sure.
I know this is something that may have been discussed in the past, I just couldn't find a clear explanation about that.
There is an official page for business logic on Symfony but not clear about that: http://symfony.com/doc/current/best_practices/business-logic.html
Thank's in advance for your help

You should create custom services and add them to the servicecontainer. This link will help you: http://symfony.com/doc/current/service_container.html#creating-configuring-services-in-the-container

Related

Symfony2 execute service before controller action

So we've got SomeBundle and want execute some actions (services\another action from another bundle or something else) before SomeBundleControllerAction will be called. I read that some guys tries it from bundle class, some from event listener (but i can not get in how it works) and now question is.
How to call, proper way, (let it be) service before any of action from our SomeBundle will be called?
I don't like just posting a link, but this pretty much explains what I would do in your case. You can inject your service into the listener anyway you would like (constructor, setter).
http://symfony.com/doc/current/cookbook/event_dispatcher/before_after_filters.html#before-filters-with-the-kernel-controller-event
here was a full description of my answer (some bad person put minus =\ but didn't help at all, next time put links if you know where to find an answer) http://symfony.com/doc/current/book/internals.html#kernel-controller-event

Json Dynamic views spring MVC

I am kind of new to spring MVC
and I wanted to use JasonViews (Jackson) for dynamic JSON per request and I didn't want to change my controllers too much .. so #marty here gave me a great solution in his blog
http://martypitt.wordpress.com/2012/11/05/custom-json-views-with-spring-mvc-and-jackson/
The problem is that I need control over the HTTP status codes,so my controllers are returning types of HttpResponse{T} and not List{T} how can I customize the code in order to support parsing of types like HttpResponse{List{Book}}?
If not possible, can I control the Http status code without the HttpResponse?
I may need to see an example of your code to really understand what you are asking, but this might point you in the right direction:
Take #marty's code and try extending HttpEntityMethodProcessor with it.
http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessor.htmlS
Then, you need to make your method signatures look like
public ResponseEntity<MyType> getMyTypeWithControlOverResponseEntity(...) {...}
You can find more info on this signature type at:
http://static.springsource.org/spring/docs/current/spring-framework-reference/htmlsingle/#mvc-ann-httpentity
Let me know if this is not the right direction you were trying to go or need more help. =)

What is a good approach to bind an entityproxy to a UI component?

I'm currently working on a GWT project. The thing is, I find very hard to believe that I need to repeat some boilerplate over and over to bind the data from an EntityProxy (say a getSomeData() method) to a UI component (say a TextBox).
How do you guys overcome this issue? For now I have to create a method to read from the TextBox and set it to the EntityProxy, and a method to write to the TextBox after reading from the EntityProxy.
Basically, it's always the same! i.e.:
// Update the text box
T someData = entity.getSomeData();
textBox.setText(someData);
// Update the entity
String value = textBox.getText();
entity.setSomeData(value);
You get my point? I'm aware there is no Reflection at client side. I could use deffered binding but I'm not sure how or if it is a good approach with RequestFactory's EntityProxys.
Thank you
I use the technique you have defined in your question to push and collect data from my controls. Recently I have found out that there is a built-in feature of GWT called Editors. I didn't have a chance to try it myself yet but perhaps you want to check it out it seems promising. Also here is another useful link from Thomas Broyer's blog about Editors

SIMPLE MVC Question MVC Redirect

I have a controller that creates a news article. I want to redirect the user to the article's page, something like: "/article/1". Normally I could just throw in return View("MyAction") but I need to pass the integer along in this case. There are overloads for adding a model to the View method call but I don't see anything that will let me accomplish my goal.
What's the best way to do this?
Also, I used ViewData for my success message... if I use a redirect message it isn't going to function so is there a better way to do that too?
I think this is what you might be looking for.
RedirectToAction(new {controller="controllerName", action="article", id=1});
Hope this helps

Looking for a good technique for storing email templates

I am building a site in which we are making moderate use of email templates. As in, HTML templates which we pass tokens into like {UserName}, {Email}, {NameFirst}, etc.
I am struggling with where to store these, as far as best practice goes. I'll first show the approach I took, and I'd be really excited to hear some expert perspective as a far as alternate approaches.
I created HTML templates in a folder called /Templates/.
I call a static method in my service layer, which takes in the following arguments:
UserName
UserID
Email
TemplatePath ("~/Templates")
Email Subject
Within the service layer I have my static method SendUserEmail() which makes use of a Template class - which takes a path, loads it as a string, and has a AddToken() Method.
Within my static SendUserEmail(), I build the token list off of the method signature, and send the email.
This makes for a quite long method call in my actual usage, especially since I am calling from the web.config the "TemplatePath", and "Email Subject". I could create a utility that has a shorter method call than the ConfigurationManager.AppSettings, but my concern is more that I don't usually see method signatures this long and I feel like it's because I'm doing something wrong.
This technique works great for the emails I have now, which at the most are using the first 3 tokens. However in the future I will have more tokens to pass in, and I'm just wondering what approach to take.
Do I create methods specific to the email needing to be sent? ie. SendNewUserRegistration(), SendMarketingMaterial(), and each has a different signature for the parameters?
I am using ASP.NET Membership, which contains probably the extend of all the fields I'll ever need. There are three main objects, aspnet_User, aspnet_Mebership and aspnet_profile. If it was all contained in one object, I would have just passed that in. Is there performance concerns with passing in all 3, to get all the fields I need? That is versus just passing in aspnet_User.UserID, aspnet_User.Email, etc?
I could see passing in a dictionary with the token entries, but I'm just wondering if that is too much to ask the calling page?
Is there a way to stick these in a config file of it's own called Templates.config, which has tags like -
<Templates>
<EmailTemplate Name="New User Registration">
<Tokens>
<UserName>
<UserID>
<Email>
</Tokens>
<Message Subject="Hi welcome...">
Hi {UserName}...
</Message>
</EmailTemplate>
</Templates>
I guess the main reason I'm asking, is because I'm having a hard time determining where the responsibility should be as far as determining what template to use, and how to pass in parameters. Is it OK if the calling page has to build the dictionary of TokenName, TokenValue? Or should the method take each in as a defined parameter? This looks out of place in the web.config, because I have 2 entries for and , and it feels like it should look more nested.
Thank you. Any techniques or suggestions of an objective approach I can use to ask whether my approach is OK.
First of all I would like to suggest you to use NVelocity as a template engine. As for main problem I think you can create an abstract class MailMessage and derive each one for every needed message (with unique template). So you will use this like following:
MailMessage message = new UserRegistrationMessage(tokens);
//some code that sends this message
Going this way you force each concrete XXXMessage class to be responsible for storing a template and filling it with the given tokens. How to deal with tokens? The simpliest way is to create a dictionary before passing it to the message, so each concrete message class will know how to deal with passed dictionary and what tokens it should contain, but you also need to remember what tokens it should contain. Another way (I like it more) is to create a general abstract type TokenSet and a derived one for every needed unique set of tokens. For example you can create a UserMessageTokenSet : TokenSet and several properties in it:
UserNameToken
SomeUserProfileDataToken
etc. So using this way you will always know, what data you should set for each token set and
UserRegistrationMessage will know what to take from this tokenSet.
There are a lot of ways to go. If you will describe you task better I think I will try suggest you something more concrete. But general idea is listed above. Hope it helps =)

Resources