Revealing Module Pattern

Using closures in javascript, we can create public and private methods.

revealing

Only the methods and variables that are specifically returned are available publicly and we can provide references to privately declared methods and variables inside the public methods.

Layout for RMP:

Image File as Response from .NET Web API

When developing a web API, it is often required to send response as a PDF or image file.
Following lines of code will assist to create method that will override the default media type of the response determined by Content Negotiation.

public HttpResponseMessage GetImage()
        {
            byte[] bytes = System.IO.File.ReadAllBytes(
            HttpContext.Current.Server
            .MapPath(“~/Content/myImage.png”));
            var result = new HttpResponseMessage(HttpStatusCode.OK);
            result.Content = new ByteArrayContent(bytes);
            result.Content.Headers.ContentType
            = new MediaTypeHeaderValue(“image/png”);
            return result;
        }

The key to this implementation is the use of the HttpResponseMessage class, which
helps to create a raw HTTP response in the controller itself.

We then set the HTTP 200 status code using the enum HttpStatusCode.OK and set the image bytes as response content.

The last step is to set the media type header to image/png, and the response is returned as HttpResponseMessage type.

Reference:
Building Mobile Applications Using Kendo UI Mobile and ASP.NET Web API

Automating Development in ASP.NET MVC

Inspired by rapid development framework Artisan in Laravel for PHP, I am thinking maybe we (.NET developers) can do something similar in our environment as well.

http://laravel.com/docs/5.0/artisan

Basically I am thinking of automating the whole process of creating Repository classes (including required entries for properties and fields), Model classes,  Service layer (layer where you put your business logic) interfaces and classes and maybe even Controllers and Views! Wouldn’t this be real good treat if all of this code writing could be automated!! Unless you have already seen this automation implemented by someone somewhere elsewhere, I am sure you are excited by this idea.

So this is the big idea. How do I plan to achieve this?

Well, I do all my projects in MVC pattern implementing Entity Framework and Unit of Work Pattern for Data Layer and Service Layer. Basically, I keep a project for Data Access Layer where the .edmx file generated by EF stays along with repositories for each Database objects. Then I expose the repositories for each Entities by a central repository using a Unit of Work pattern.

Then I keep another project for Model classes. For every entities generated by EF, I have models in this project.

Then I have third project for Service (or Business Logic) Layer where I have interfaces and their implementations to do all CRUD activities.

Finally for displaying the View to the user, I use a ASP.NET MVC project where I have controllers for each Entities. These controllers receive request from views and process data as per the request using the Interface exposed by Service Layer. The response sent from controllers to View is usually in JSON format.

The big realization is that no matter what type of entities we are dealing with the whole process of creating Repositories, Models, Services, Controllers and even the Views stays the same. The only changes that we will find are in the Members (Properties) of our Entities.

If we are dealing with User entity, we will have properties like Username, Password and Email and if we are dealing with Product entity, we will have properties like ProductName, ProductPrice, Category and so on.

Hence, if we just replace the properties and entity names, everything stays the same. This opens up the possibility for automating this whole process like I mentioned in the beginning of the post. Heck if I am able to automate only 70% of the process and make necessary adjustments for the remaining 30%, I would be more than happy.

I will be giving this a try and will write about it once its done. What are your thoughts on this?