The repository pattern is a must use for MVC bases projects

The repository patterns seperate the logic between data-methods and the business logic. In a MVC project it's a quick, and dirty idea to directly use the DataContext class in de datalayer. While this works it will let you create duplicate code, queries which all have to adjusted in case of a data-model change.

Create a repository, or maybe a generic repository as a layer between your business logic and the DataContext class will prevent duplicate code but it is allso easier to test.

If using a generic repository a class can look like this:

    public class KlantRepository : GenericRepository, iKlantRepository
    {

    }

All the neccessary CRUD methods are in the GenericRepository class. This will give you room and overview to just put the custom data-queries in this class.

The generic repository class canlook like this:

public class GenericRepository : iGenericRepository
{
    public GenericRepository(string databaseNaam = "") { }

    public ReturnMessage Create(T poco)
        {
            ReturnMessage returnMessage = new ReturnMessage();
            try
            {
                if (poco == null)
                { 
                    returnMessage.Succeeded = false, 
                    returnMessage.Message = "Er is niets om in te voeren( geen poco!)";
                };
                return;

                var database = Database.Db;
                if (database == null)
                { 
                    Succeeded = false, 
                    Message = "Database niet geinitialiseerd!"; 
                };
                return;

                database.Insert( poco);

                returnMessage.Succeeded = true;
                returnMessage.Message += "Record is opgeslagen";
            }
            catch (Exception ex)
            {
                returnMessage.Succeeded = false;
                returnMessage.Message += ex.Message;
            }
            return returnMessage;
        }

    public ReturnMessage Delete(T poco)
    {
        ReturnMessage returnMessage = new ReturnMessage();
        try
        {
            Database.Db.Delete(poco);
            returnMessage.Succeeded = true;
            returnMessage.Message = "Record is deleted";
        }
        catch (Exception ex)
        {
            returnMessage.Succeeded = false;
            returnMessage.Message = ex.Message;
        }
        return returnMessage;
    }

    public ReturnMessage Delete(int pocoId)
    {
        ReturnMessage returnMessage = new ReturnMessage();
        try
        {
            var poco = Single(pocoId);
            Database.Db.Delete(poco);
            returnMessage.Succeeded = true;
            returnMessage.Message = "Record is deleted";
        }
        catch (Exception ex)
        {
            returnMessage.Succeeded = false;
            returnMessage.Message = ex.Message;
        }
        return returnMessage;
    }

    public ReturnMessage Update(T poco)
    {
        ReturnMessage returnMessage = new ReturnMessage();
        try
        {
            Database.Db.Update(poco);
            returnMessage.Succeeded = true;
            returnMessage.Message = "Record is bijgewerkt";
        }
        catch (Exception ex)
        {
            returnMessage.Succeeded = false;
            returnMessage.Message = ex.Message;
        }
        return returnMessage;
    }

    public virtual IEnumerable SelectAll()
    {
        return Database.Db.Query(sql);
    }
}

So big advantage here is that have splitted you're data-acces logic from the business-logic to the repository-layer wich will prevent duplicatie code etc. Advantage of the generic repository is that you don't have to create all the CRUD methods per POCO repository but next to that have room for custom data-queries. The lates advantage is test-able. In your unit test you can create unit test for all you repository's wich will assure you that the data-access is working ok and next to that it's easier for testing your controller actions: see:

//Controller unit tests:

{{opmerking.Naam}}:

{{opmerking.OpmerkingText}}

            

Saving your comment....

Naam is verplicht!
Email is verplicht!
Opmerking is verplicht!