0

I have an Index Method in my FileController that can return a file that is stored in the Attachments entity. How can I change the method to work with any entity not just the Attachments?

public class FileController : Controller
{
    private MainDatabaseContext db = new MainDatabaseContext();
    // GET: File

    public ActionResult Index(int id)
    {
        var fileToRetrieve = db.Attachments.Find(id);
        var FileObject= File (fileToRetrieve.AttachmentObject, fileToRetrieve.ContentType);
        if (FileObject.FileDownloadName.Length == 0)
        {
            FileObject.FileDownloadName = fileToRetrieve.Filename;
        }
        return FileObject;
    }
}

This is what I have done as a workaround, but it has a lot of repeated code which I wanted to avoid:

public class FileController : Controller
{
    private MainDatabaseContext db = new MainDatabaseContext();
    enum EntityName
    {
        Attachment=1,
        WAProgramApplicationId,
        HouseholdIncome,
        HouseholdMember
    }
    // GET: File
    public ActionResult Index(int id=0,int WAProgramApplicationId=0,int householdIncomeID=0,int householdMemberId=0)
    {

        if (householdIncomeID!=0)
        {
            return GetFileObject(householdIncomeID, EntityName.HouseholdIncome);
        }
        if (id!=0)
        {
            return GetFileObject(id, EntityName.Attachment);
        }
        if (WAProgramApplicationId != 0)
        {
            return GetFileObject(WAProgramApplicationId, EntityName.WAProgramApplicationId);
        }
        if (householdMemberId!=0)
        {
            return GetFileObject(householdMemberId, EntityName.HouseholdMember);
        }
        return null;
    }

    private ActionResult GetFileObject(int id, EntityName entityName)
    {

        if (entityName==EntityName.Attachment)
        {
            var fileToRetrieve = db.Attachments.Find(id);
            var FileObject = File(fileToRetrieve.AttachmentObject, fileToRetrieve.ContentType);
            if (FileObject.FileDownloadName.Length == 0)
            {
                FileObject.FileDownloadName = fileToRetrieve.Filename;
            }
            return FileObject;

        }
        if (entityName == EntityName.HouseholdIncome)
        {
            var fileToRetrieve = db.HouseholdIncomes.Find(id);
            var FileObject = File(fileToRetrieve.AttachmentObject, fileToRetrieve.ContentType);
            if (FileObject.FileDownloadName.Length == 0)
            {
                FileObject.FileDownloadName = fileToRetrieve.Filename;
            }
            return FileObject;

        }
        if (entityName==EntityName.WAProgramApplicationId)
        {
            var fileToRetrieve = db.WAProgramApplications.Find(id);
            var FileObject = File(fileToRetrieve.AttachmentObject, fileToRetrieve.ContentType);
            if (FileObject.FileDownloadName.Length == 0)
            {
                FileObject.FileDownloadName = fileToRetrieve.Filename;
            }
            return FileObject;
        }
        if (entityName==EntityName.HouseholdMember)
        {
            var fileToRetrieve = db.HouseholdMembers.Find(id);
            var FileObject = File(fileToRetrieve.AttachmentObject, fileToRetrieve.ContentType);
            if (FileObject.FileDownloadName.Length == 0)
            {
                FileObject.FileDownloadName = fileToRetrieve.Filename;
            }
            return FileObject;
        }
        return null;
    }
}
Mark3308
  • 1,298
  • 11
  • 22
  • 1
    What have you tried? You must have come across the `db.Set()` method, but does all this code apply to all entities you want to support? Does every entity type have a `AttachmentObject` property? – CodeCaster Feb 28 '17 at 12:32
  • Not all my entities just some of them have it – Mark3308 Feb 28 '17 at 12:33
  • So, show the code that looks like this and the related entities that you want to generalize, and explain what you've tried to make the code generic. – CodeCaster Feb 28 '17 at 12:51
  • When you say any entity do you want to read the entity name from input parameters? Or use a base controller to return files? – Filip Cordas Feb 28 '17 at 13:26

2 Answers2

0

to make the entity more generic just use the Set method, you can do it like this:

db.Set<YourEntity>().Find(id);
Amro Mustafa
  • 589
  • 4
  • 15
0

They are some approaches how to implement a generic controller with data.

Check here and here.

If you have a repository that can handle _repository.get<T> and return the right object, you can achieve this.

Community
  • 1
  • 1
Ygalbel
  • 5,214
  • 1
  • 24
  • 32