1

I have two methods that use different viewmodels but are the same logic. At the moment I have copied and pasted them into their respective controllers. Any way to share these methods somehow?

Song Controller:

public JsonResult IncrementViews(int id)
    {
        using (ApplicationDbContext db = new ApplicationDbContext())
        {
            PublishedSongViewModel song = db.PublishedSongs.Single(x => x.Id == id);
            song.UniquePlayCounts++;
            db.SaveChanges();
            return Json(new { UniquePlayCounts = song.UniquePlayCounts }, JsonRequestBehavior.AllowGet);
        }
    }

Station Controller:

public JsonResult IncrementViews(int id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                RadioStationViewModel station = db.RadioStations.Single(x => x.Id == id);
                station.UniquePlayCounts++;
                db.SaveChanges();
                return Json(new { UniquePlayCounts = station.UniquePlayCounts }, JsonRequestBehavior.AllowGet);
            }
        }

Edit: class so far:

public static IEnumerable<Type> GetElements(ApplicationDbContext db, Type type)
    {
        if (type == typeof(SongsController))
            return (IEnumerable<Type>)db.PublishedSongs;
        else if (type == typeof(RadioStationsController))
            return (IEnumerable<Type>)db.RadioStations;
        else
            throw new Exception("Controller not found, DBHelper");
    }

1 Answer 1

2

Create a class called BasicController and add the method to it, like this:

public class BasicController {
    public JsonResult IncrementViews(int id)
    {
        using (ApplicationDbContext db = new ApplicationDbContext())
        {
            var element = DBHelper.GetElements(db, this.GetType()).Single(x => x.Id == id);
            element.UniquePlayCounts++;
            db.SaveChanges();
            return Json(new { UniquePlayCounts = song.UniquePlayCounts }, JsonRequestBehavior.AllowGet);
        }
    }
}

and modify your classes to inherit from BasicController. You will also have to create the DBHelper class with the GetElements method, which gathers the IEnumerable elements from db based on type.

EDIT: This is how you can create a helper:

public class DBHelper {
    public static IEnumerable GetElements(ApplicationDbContext db, System.Type type) {
        if (type == typeof(SongController)) {
            return db.PublishedSongs;
        } else if (type == typeof(StationController)) {
            return db.RadioStations;
        }
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

How do I create the GetElements method. I have never done helpers before really so I don't know where to start?
You need to create a class called DBHelper, like you create any other class. Inside that you need to define GetElements which will receive an application db context and a System.Type. Based on System.Type you gather the elements. You need to use if-elses for that purpose.
@MartinMazzaDawson, please check my edit. It is untested code, so if something does not feel quite right, then please add a comment
<T> was undefined so I changed it to Type, not sure if this is correct or not. Also in my controller x.Id cannot be found on this.GetType()).Single(x => x.Id == id) It has GUID as a property but I am using int Id
Maybe try without <T> if C# accepts it. I have no environment for C# right now. The second problem is that you tried to do it with Type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.