When to use the Factory Pattern

I’ve recently be challenged by freshly written code at my company where they used the factory pattern.  This led me to research into why one would want to use it.  In a lot of my research, they discourage it until it is really needed because it adds complexity with the additional classes and creation logic.

Here are some of my additional thoughts:

  • This pattern hides the creation of an object.
  • Useful when creating the object involves multiple steps.
  • Centralizes object creation so that if it needs to change it only needs to be changed in one place.
  • Useful if you have a lot of code that checks if object type is this…then do this.  This really is one of the most useful features I see.
  • It could be used for unit testing to switch in test data in place of live data.
  • Can bring unnecessary complexity: http://www.oodesign.com/factory-pattern.html , also agile principles, patterns, and practices in c#, Robert Martin.
  • Case Switch pattern is easier and more useful in that you don’t have to create a method for each and the calling logic doesn’t have to know what type of object it is dealing with.

Web API with multiple post apis on a controller

I was researching how to add multiple post APIs on a controller and found a solution that seems to be working.  I found this on this post: http://e-zooka.com/mvc4-webapi-multiple-post-get-actions/

public class EnrollmentController : ApiController
{
[System.Web.Http.AcceptVerbs(“GET”, “POST”), System.Web.Http.ActionName(“MyApi1”)
public Result MyApi1([FromBody] MyObject parameter)
{
var result = new Result
{
Code = “0”,
Message = “Success”,
TimeStamp = DateTime.UtcNow.ToString()
};
return result;
}

[System.Web.Http.AcceptVerbs(“GET”, “POST”), System.Web.Http.ActionName(“MyApi2”)
public Result MyApi2([FromBody] MyObject parameter)
{
var result = new Result
{
Code = “0”,
Message = “Success”,
TimeStamp = DateTime.UtcNow.ToString()
};
return result;
}
}

public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// To handle routes like /Mobile
config.Routes.MapHttpRoute(
name: “ControllerOnly”,
routeTemplate: “Api/{controller}”
);

// Controller with ID
// To handle routes like /api/Mobile/1
config.Routes.MapHttpRoute(
name: “ControllerAndId”,
routeTemplate: “Api/{controller}/{id}”,
defaults: null,
constraints: new { id = @”^\d+$” } // Only integers
);

// Controllers with Actions
// To handle routes like /api/Mobile/actionname
config.Routes.MapHttpRoute(
name: “ControllerAndAction”,
routeTemplate: “Api/{controller}/{action}”
);

// Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable return type.
// To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
// For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
//config.EnableQuerySupport();

// To disable tracing in your application, please comment out or remove the following line of code
// For more information, refer to: http://www.asp.net/web-api
//config.EnableSystemDiagnosticsTracing();
}
}

MVC – Images

I’m digging deeper in to MVC now and struggled to display images.  Not sure of the best folder to put them in since Visual Studio doesn’t automatically create a folder for images.  Anyway, I got this to work:

<img src=”@Url.Content(“/images/ImageName.jpg”) alt=”Image” style=”background-color:White; height: 188px; width: 187px;display:block” />

Web API

I’m currently implementing WebAPI into my company’s external web services.  It was pretty easy to learn and add to our existing WCF web services.  I used online videos (especially PluralSight) and ASP.NET to get acquainted with Web API:

I went with MVC version 4.0 and I created methods using a routing of “Api/{controller}/{action}”

The automatic help documentation is very impressive.

I first created a sample Web API project and then copied and pasted the code into my existing web service project.  I did have to add a lot of references and install a lot of packages from NuGet but it was fairly easy to do.

I liked testing the APIs with Fiddler.  It is easy to switch back and forth between JSON and XML.