Implementing ecommerce analytics with MVC Razor - asp.net-mvc-3

I am implementing various analytic tracking services in an MVC3 site (Google, Coremetrics) and researching if a custom HTML helper or partial view would work better given the following details:
-Code runs multiple sites and business logic is needed to change analytic service account Ids.
-The class must accept an object with order details to render the appropriate tags.
-The code must know which view is being rendered.
The solution I am working on includes am HTML helper base class that accepts the needed objects and is inherited by each provider's individual HTML helper. These helpers will live on the common layout. Is there a better way of implementing analytics on MVC and are partial views better suited since business logic is needed?

The GA analytics tracking code is pretty simple, we also use ASP.NET MVC3 but have just dropped the necessary code directly into our 'Order Complete' view.
There's no reason why you couldn't use an HTML helper instead though. I suggest not overly complicating your object model. Just use a single helper that has parameters matching the parameters used in the JavaScript.
You should be able to find examples of eCommerce tracking code, add parameters as appropriate, done. You might want to create another overload that pulls GA ID and domain name from the web.config as in the example as well, but still accepts the order details as parameters.

Related

Where Do I Create External API functions or Classes? Proper MVC Structure for Web Frameworks

I am looking to add multiple API's to my senior project on an MVC based framework (Laravel). I understand the basic concept of MVC, but want to make sure that I am doing things according to best practice.
Basically, I am going to have a class/function that takes a query and calls that query on a Amazon's Product API. I have seen an example of calling API's from directly within the Controller on Laravel (see http://www.phplab.info/categories/laravel/consume-external-api-from-laravel-5-using-guzzle-http-client).
Perhaps I don't understand MVC well enough. Should an external API call be in it's own class? And if so, should it be a Controller Class or a Model Class? I hope the Stack Overflow gurus can enlighten me. Let me know if I need to clarify anything!
It depends to what you want to process with external API.
If it's a part of the business, it can be in Model (lot of people put
the business inside the model to follow the encapsulation principle
of OOP).
If it's the explicit process, it should be in Controller
(like most people do).
For example, if you have a model Transaction in bank transfer (that automatically convert the currency, it needs the external API to get the exchange rate), the external API call should be wrapped in model. So controller cannot modify the Transaction object and it will be safe.
In another hand, you can call to external API in controller, do some extra stuffs then set it back to Transaction object. It's also good because model always contains only properties. It makes application also clear enough.
They are 2 ways of use, none is absolutely right or wrong. But if you choose one, follow it, don't mix.
Another, both 2 are only ok. The better way is putting the external calls to other places (modules etc), then call it by single line in model or controller.

MVC modular GUI components

I am trying to find the way to build complex web pages with MVC3 and AJAX.
I would like to use components to achieve this.
Each component is consisted of it's own model, view and controller.
Multiple components are then placed on some complex view and should act together to
provide desired behaviors.
In some situations, when user performs some action (interaction) with one of the components,
I must update other portions of the page via AJAX.
Component on which action (interaction) occurred, in it's implementation, does not assume anything about view on which it will be used and what portions of the pages should be updated and how.
So when some interaction occurs in some component, I need a mechanism (outside component itself) which will handle this situation and update appropriate parts of the page.
How would you, generally, implement such mechanism?
I would use the Mediator Pattern, also sometimes mistakenly referred to as the manager pattern.
This class would mediate the communication of your components.

Create custom actions in Django admin site (not list actions)

I'm new to django and I'm trying to figure out how to create custom actions inside admin site.
Let's say I want to create some sort of custom form with fancy ajax based ui.
What I would normally do in .NET/PHP/Ruby is prepare some js code and a service that will be called via ajax and return json or even html.
A more concrete example could be an auto complete box to manage a many to one relationship. What should I do to build such a system inside the django admin site? (I know there are a couple of ready to use solution for this. But I'm not interested: just for study purposes).
In this particular case (a fancier widget), ModelAdmin has several useful hooks like ModelAdmin.form and ModelAdmin.formfield_overrides.
Creating a custom Django Form Widget is more elegant, but if the widget is pure JS (like many jQuery widgets), most of the time it is not worth - just override the change_form.html admin template for the Model in question.
You can easily change the form and the template of the ModelAdmin.
So you're free to inject any CSS and JS you like. Take look at this presentation to get the basic idea.

Looking for way to reduce duplication of validation code in my View Models and Logical Layer

Right now the application being built by our team uses the built in MVC attributes and a few home baked ones to validate the View Models. Because of best practice design principles, we have placed those same rules in the Logical Layer. This has unfortunately caused duplication of validation code.
In MVC3 at least, if JavaScript is disabled, these same attributes will still perform the validation they are meant to, so transforming a View Model in to a DTO and asking the Logical Layer to validate it is not an option because this process would have already been done by the framework.
I have not found the following SO post to be of any help. I have used MS Enterprise Library and the API did not sit well with our team.
Good practices for avoiding validation logic duplication when working with both domain objects and view models in ASP.NET MVC
I'm thinking that the best way to do this is to have the validation attributes bound at runtime to specific properties and have a dependency injection container do this. Is this possible or is there a different approach we could take?
You are asking for multiple validation types to be performed here.
You want client validation (it seems) and some other business validation layer.
If thats the case the only choices as I see it are:
duplicate the code (ya I know Im listing options)
1a. use data annotations on your objects for client validation. Business layer validation happens however you define, and separate as a final check. If you use for instance the entity framework's fluent API this is a standard route.
client side validation is just that - helpers for client side. domain validation will happen upon save. This is ideally the more powerful approach, but isn't as friendly when your domain objects don't match property names on your view models, so you need to map domain errors to view model property errors which normally isnt too bad but can get iffy.
implement IValidteableObject (and rid of client validation). This validation logic is then called from your logical layer and the model binder.
You can inject code for validation, but its not pretty and Im not sure how reusable it is outside the MVC validation route.
There may be other ways beyond this, but those are the main options as I see it.
One way of seeing it is: If your api is your mvc application and the validation is already done by the point you are executing your business rules, you can just assume they are valid. It's input validation, of course is something related to your domain but... if you change the way of thinking, your domain expects valid data and assumes you did your homework before using the business rules.

best way to multi-language asp.net mvc 3

I'm trying to create a asp.net mvc3 project for a academic project, and one of the requirements is it has to be able to change between different languages. Currently what i have is the following:
I have a external project that works as a repository for languages and for each view i have an interface for each view that defines all the "placeholders" do define all the changeable text.
At the beginning of any action i obtain the language that is in the uri (something like /{lng}/{command}/{action}) and pass it to the view using the ViewBag, once inside the view i user the repository to obtain the current implementation of the interface for that view in the chosen language.
I can't find any good topic on this mater. I'm just curios if there is a better way to do this and more efficient. And how is it normally done in a professional level.
I'm not very experienced with asp.net just started learning it about a month ago.
Also if it's important i am using the razor engine for the views, and we can't use any JavaScript in this phase of the project.
You may go through the following guide.
I'm working with a project called Griffin.MvcContrib which has some localization features.
First of all, I use the query string and a cookie to switch language. (Just create a link with a flag in your layout English)
and tag your controller with my attribute:
[LocalizedAttribute]
public class YourController : Controller
{
}
The next thing is to get localization of views, models and validation messages.
The localization of models and validations are described here. As for views, you only need to use #T() to get translated texts:
#Model.Title
<div>#T("This text will get translated")</div>
(you need to change pageBaseType in Views\Web.config to Griffin.MvcContrib.GriffinWebViewPage)
I'm almost done with an adminstration area that any non-technical user can use to manage all translations. Check the Griffin.MvcContrib.Admin project here: https://github.com/jgauffin/griffin.mvccontrib/tree/localization/source/Griffin.MvcContrib.Admin

Resources