Saturday, November 9, 2013

Questions to ask yourself when choosing an open source solution



As a solutions' architect, I love open source solutions and I love even more incorporating them into the business solutions that I'm designing.  First of all, I don't want to reinvent the wheel, secondly I'd rather focus my dev time on implementing the actually business requirement as much as possible, the time gained by not building it yourself is a great gain for the business and the client.  However, there are so many out there, it can be quite overwhelming and one must be careful when choosing one to use it in your production environment.  The consequence of choosing the wrong one could result in the failure of your project in worst case or to cause delay, drive cost at best.  I have a set of questions, the answers to which really helped me to make a sound decision.

  1. What are others saying about this solution?
    1. Like any other opinion, one must take it with a grain of salt.  Be sure to listen to both the good and the bad.  Either way can be exaggerated or partially true.
  2. Is the solution being successfully used in production by any sizable and reputable company?
    1. I don't know about you but I don't want my clients to be the guinea pig of an open source solution. 
  3. What is the open source license the solution is under?
    1. I've seen open source solutions found on github that will actually require a license fee if you were to use them in commercial products.  So be sure to read the license agreement before investing your time and energy into understanding how it works.  Check out the terms of the different licenses here.
  4. Is the solution being actively developed/maintained?
    1. This one actually goes hand in hand with the first one.  The more people use it the more actively and better it is maintained.
  5. What does the source code quality look like?
    1. Go clone/download the source code and the look at the quality of the code base.  One really good indicator is the test coverage and of course reading the code itself.
  6. How well is it documented and how well is the documentation is updated? 
    1. Obviously this is huge, without it you might as well rewrite the solution yourself.
  7. Is there a good community / support group for it?
    1. Being open source, you'll need questions answered quicker than you can find it on your own.  Otherwise, your productivity would be shot!
  8. Who is behind the project?
    1. For instance, Google is behind AngularJS and Microsoft is behind a bunch of open source initiatives.
    2. This is good indicator but not always the case so this should be weighed by the answers to the other questions.  Big companies are known to pull plugs on projects open source or not.
Hope this will help guide you when trying to choose an Open Source solution.

Saturday, November 2, 2013

MVC Partial View to String

Sometimes, I would have some page features that would require fetching HTML on the fly from the server and at the same time the user needs to be notified why the fetching failed from the server.

Suppose that I need to populate a tag in the following HTML:

   <!-- the div tag -->
   <div id='someDivToPopulate'>&nbsp;</div>
    <!-- button to click -->
   <button id='someButton'>click me </button>
    <!-- the javascript to click event and fetching of the JSON Data -->
   <script type='text/javascript'>
       $('#someButton').click(function () {
           $.post(
               "SomeController/GetSomeJsonData",
               {
                   Id: 1
               },
               function (data) { 
                   if (!data.Success) {
                       alert(data.Message);
                       return;
                   }
                   $("#someDivToPopulate").html(data.View);
               });
       });
   </script>

I have pages that makes AJAX requests to have the server return views in HTML string in JSON format.

Typically, my JSON response object would contain properties such as follows:

Figure 1:
//A success response:
{
   Success: true,
   Message: "success",
   View: "{some html string}"
}

//A failure response:
{
   Success: false,
   Message: "record not found",
   View: null
}


Well, Asp.Net MVC makes it supper easy to have such HTML generated from a partial view

Figure 2:

public class SharedController : Controller
    {
  public static string RenderViewToString(ControllerContext context, string viewName, object model)
  {
   //if view name is not specified, use action name
   if (string.IsNullOrEmpty(viewName))
    viewName = context.RouteData.GetRequiredString("action");

   // get view data from model
   var viewData = new ViewDataDictionary(model);

   //render the view and generate HTML string
   using (var sw = new StringWriter())
   {
    var viewResult = ViewEngines.Engines.FindPartialView(context, viewName);
    var viewContext = new ViewContext(context, viewResult.View, viewData, new TempDataDictionary(), sw);
    viewResult.View.Render(viewContext, sw);

    return sw.GetStringBuilder().ToString();
   }
  }
    }
 
You can define this method in shared controller class or in a helper class so any other class can use it too.

The usage of this is quite simple: using out example in Figure 1, suppose I have a controller with the following action.

Figure 3:

[HttpPost]
public ActionResult GetSomeJsonData(SomeRequestModel model)
{
 var someViewModel = AutoMapper.Mapper.Map<SomeDomainModel, SomeReturnModel>(_someRepo.GetById(model.Id));
 var someViewInHtml = RenderViewToString(ControllerContext, "someView", someViewModel);
 var toReturn = new { Success = true, Message = "success", View = someViewInHtml };
 return Json(toReturn);
} 

Note: remember when returning JSON data, be sure to include exactly only what the view needs not more especially if your site is open to the public.   
Otherwise, one ends sending sensitive information to the browser.
 

About Cullen

My photo
Christian, Father, Software Developer/Architect who enjoys technology and using it to make people's lives easier!

Followers