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

Refactor MVC project's classes and methods

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

Problem

I'm learning C# as a web developer rather than a C# developer learning web development. I've gotten the hang of creating models and controllers (or so I think) for a web project, and I've read some tutorials on general C# and I'm just putting it all together. I'd like to know how I can best refactor this code.

Let's say I have the following model

// Models for Lite Requests
public class RequestLite
{
    public string action { get; set; }
    public string action2 { get; set; }
    public string action3 { get; set; }
    public ParametersLite parameters { get; set; }
}

public class RequestHeavy
{
    public string action { get; set; }
    public string action2 { get; set; }
    public string action3 { get; set; }
    public ParametersHeavy parameters { get; set; }
}

// Models for Heavy Requests
public class ParametersLite 
{
    public List parameter { get; set; }
}

public class ParametersHeavy
{
    public List parameter { get; set; }
    public string membership { get; set; }
    public string featured { get; set; }
}


RequestLite and RequestHeavy are pretty much the same except for the fact that RequestHeavy has some additional parameters. The remote service can only accept one specific kind of data request set per query. If I were to send a RequestHeavy model with membership and featured set as null to a Query that only accepts RequestLite's model, it'd break.

Question: what's the best way to refactor these models? It's redundant to have ParametersLite and a ParametersHeavy if the only difference is two additional parameters. How can I just have one type of Object where I can have it send those parameters only when needed?

Here's the rest of the code

```
public class HomeController : Controller
{
public string endpoint = "http://generic.api.com:12345/query";

public string ConnectToEndpoint(string data)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(endpoint);
httpWebRequest.ContentType = "text/json";

Solution

Your first question can be answered with Inheritence. ParametersHeavy should inherit from ParametersLite.

From the above link:


The new class—the derived class—then gains all the non-private data and behavior of the base class in addition to any other data or behaviors it defines for itself. The new class then has two effective types: the type of the new class and the type of the class it inherits.

Then any change made to ParametersLite will "cascade" into ParametersHeavy. This means if you ever change it, you only have to make the change once instead of twice. This also simplifies the Heavy class a little bit.

public class ParametersHeavy : ParametersLite
{
    public string membership { get; set; }
    public string featured { get; set; }
}


You can do the same for RequestHeavy and RequestLite, but you will need to Override the parameters property.

You should also do yourself and the devs you work with a favor and study up on the C# Naming Guide. Properties and Methods should be PascalCased, variables should be camelCased. Also be careful with your bracket placement. It's usual to see brackets on the new line at the same indentation level. They're all over the place in your second snippet.

Code Snippets

public class ParametersHeavy : ParametersLite
{
    public string membership { get; set; }
    public string featured { get; set; }
}

Context

StackExchange Code Review Q#66942, answer score: 3

Revisions (0)

No revisions yet.