HiveBrain v1.2.0
Get Started
← Back to all entries
principlecsharpMinor

Better approach to online builder

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
builderapproachbetteronline

Problem

I have a windows forms application which needs customized dll files to be built and for that I am using an online builder using asp.net. I am in no way a asp.net expert not even near a beginner but this is what I have come up with so far:

All the work is done by the Handler:

public void ProcessRequest(HttpContext context)
    {
        var assembly = AssemblyDefinition.ReadAssembly(context.Server.MapPath("D2.dll"));

        var ctor = assembly.MainModule.Types.FirstOrDefault(t => t.FullName.Contains("ClassName") && t.Namespace.Contains("NameSpace"))
            .Methods.FirstOrDefault(m => m.FullName.Contains("ctor"));

        // Update Instructions
        ctor.Body.Instructions[1].Operand = context.Request["A"];
        ctor.Body.Instructions[4].Operand = context.Request["GM"];
        ctor.Body.Instructions[7].Operand = context.Request["GI"];
        ctor.Body.Instructions[10].Operand = context.Request["U"];
        ctor.Body.Instructions[13].Operand = context.Request["CT"];
        ctor.Body.Instructions[16].Operand = context.Request["MT"];
        ctor.Body.Instructions[19].Operand = context.Request["D"];

        // Optimize the Body - Required to allow proper build
        ctor.Body.OptimizeMacros();
        ctor.Body.SimplifyMacros();

        // Write the Assembly to Stream so it can be converted to Byte[]
        var memstream = new MemoryStream();
        assembly.Write(memstream);

        // Clear existing response.
        context.Response.Clear();

        // Write new response.
        context.Response.ContentType = "application/octet-stream";
        context.Response.AddHeader("Content-Disposition", "attachment; filename=Build.dll");
        context.Response.AddHeader("Content-Length", memstream.Length.ToString());
        context.Response.BinaryWrite(memstream.ToArray());
    }


Currently using this approach/model I have a lot of problems mainly debugging and fixing errors.

I want to improve on this but due to my lack of knowledge in w

Solution

This looks good to me. A nice use of space, Everything is very clear and easy to read, just a couple of problems:

Magic Numbers / Strings

You have a few of these dotted about, a couple of examples:

ctor.Body.Instructions[1].Operand = context.Request["A"];

I have no idea what A signifies here, nor why it should be the first instruction's operand. Swap A with a const that gives that string a name describing its purpose. You can probably do the same for 1 in this scenario too, unless A's purpose is only FirstInstructionOperandCode or something similar.

Commenting

Your comments don't add anything to your code. A lot of them just describe what you're doing, which any maintenance programmer can work out by reading the actual code. Instead you should use your comments to describe why you're doing something, particularly if it breaks from the expected norms.

The most egregious example is this:

// Clear existing response.
context.Response.Clear();


Structure

The Write New Response section could be refactored out into a separate method, but unless you're writing these responses elsewhere, it's probably not going to give you any significant gains.

Code Snippets

// Clear existing response.
context.Response.Clear();

Context

StackExchange Code Review Q#56870, answer score: 2

Revisions (0)

No revisions yet.