How can I get sorting, paging and filtering on a Repository without MVC

asp.net-core c# entity-framework-core

Question

I'm trying to create just the back-end part for a simple CRUD project. I'm completely new to asp.net and currently reading as I go. But I got stuck.

I've created the entities (models) I need

public class VehicleMakeEntity
{
    public int Id { get; set; }

    public string Name { get; set; }

    public string Abrv { get; set; }

    public VehicleMakeEntity Make { get; set; }
}

I've also created a standard DbContext class and the repositories

class MakeRepository : IMakeRepository
{
    ProjectDbContext context;

    public MakeRepository(ProjectDbContext context)
    {
        this.context = context;
    }

    public void Delete(VehicleMakeEntity entity)
    {
        context.VehicleMake.Remove(entity);
        context.SaveChanges();
        Console.WriteLine(entity.Make.Name);
    }

    public void Insert(VehicleMakeEntity entity)
    {
        context.VehicleMake.Add(entity);
        context.SaveChanges();
    }

    public void Update(VehicleMakeEntity entity)
    {
        context.VehicleMake.Update(entity);
        context.SaveChanges();
    }

    public void Dispose()
    {
        context.Dispose();
    }
}

Now the problem I have is I cannot find any documentation that both doesn't use MVC and uses repositories. I need to create a service class that implements basic paging, sorting and filtering but everything I look at Like this uses MVC and I have no idea how to convert it to my project.

1
0
11/17/2018 10:58:57 PM

Accepted Answer

Based on your code and comments, you have a setup that is meant for .NET Core web apps that can be implemented with services (dependency injection). You can, however, run the entire dependency injection setup on a console based app that doesn't host a web app.

First, you will need to add the Microsoft.AspNetCore.All package so you get all the classes you might be using (mainly Microsoft.Extensions.DependencyInjection). You can now build your service provider something like this:

var provider = new ServiceCollection()
                .AddDbContext<ProjectDbContext>(config => // connection here)
                .AddScoped<IMakeRepository, MakeRepository>()
                .BuildServiceProvider();

You can now fetch any service you want out of your provider. The above code also allows you to add any service you find in any .NET Core MVC tutorial since it uses the same classes and libraries. Let's say you want to fetch the IMakeRepository, you can use the following:

var makeRepo = provider.GetService<IMakeRepository>();

You can now test out the functions inside your makeRepo object.

-1
11/18/2018 1:09:36 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow