制作方法更通用的ASP MVC

时间:2017-02-28 12:29:32

标签: c# asp.net-mvc entity-framework

我的FileController中有一个Index Method,它可以返回存储在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;
    }
}

这就是我所做的解决方法,但它有很多重复的代码我想避免:

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;
    }
}

2 个答案:

答案 0 :(得分:0)

要使实体更通用,只需使用Set方法,就可以这样做:

db.Set<YourEntity>().Find(id);

答案 1 :(得分:0)

它们是如何使用数据实现通用控制器的一些方法。

检查herehere

如果您有一个可以处理_repository.get<T>并返回正确对象的存储库,则可以实现此目的。