Code Monkey home page Code Monkey logo

urf.core.sample.v3's Introduction

URF Core 3.x Demo

Sample using Unit of Work and Repository Framework (URF) with ASP.NET Core 3.x

Prerequisites

EF Core Code First

  1. Create Models .NET Standard 2.1 class library.

  2. Add Product class.

    public class Product
    {
        public int Id { get; set; }
        public string ProductName { get; set; }
        public decimal UnitPrice { get; set; }
    }
  3. Create EF .NET Core class library.

    • Reference the Models project.
    • Add NuGet packages:
      • URF.Core.EF
      • Microsoft.EntityFrameworkCore.SqlServer
      • Microsoft.EntityFrameworkCore.Design
  4. Add UrfSampleContext class that extends DbContext

    • Add Contexts folder.
    public class UrfSampleContext : DbContext
    {
        public UrfSampleContext(DbContextOptions<UrfSampleContext> options) : base(options) { }
    
        public DbSet<Product> Products { get; set; }
    }
  5. Add UrfSampleContextFactory class that implements IDesignTimeDbContextFactory<UrfSampleContext>

    public class UrfSampleContextFactory : IDesignTimeDbContextFactory<UrfSampleContext>
    {
        public UrfSampleContext CreateDbContext(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder<UrfSampleContext>();
            optionsBuilder.UseSqlServer(@"Data Source=(localdb)\MSSQLLocalDB;initial catalog=UrfSample;Integrated Security=True; MultipleActiveResultSets=True");
            return new UrfSampleContext(optionsBuilder.Options);
        }
    }
  6. Add code to UrfSampleContext to seed data the database.

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Product>().HasData(
            new Product { Id = 1, ProductName = "Chai", UnitPrice = 1 },
            new Product { Id = 2, ProductName = "Chang", UnitPrice = 2},
            new Product { Id = 3, ProductName = "Cappuccino", UnitPrice = 3 }
        );
    }
  7. Open command prompt at EF project directory and add an EF migration.

    dotnet ef migrations add initial
    
  8. Apply the EF migration to the database

    dotnet ef database update
    

URF.Core Unit of Work

  1. Create Abstractions .NET Standard 2.1 class library.

    • Reference the Models project.
    • Add URF.Core.Abstractions package.
    • Add IUrfSampleUnitOfWork interface that extends IUnitOfWork and adds ProductsRepository property.
    public interface IUrfSampleUnitOfWork : IUnitOfWork
    {
        public IRepository<Product> ProductsRepository { get; }
    }
  2. In the EF project add a reference to the Abstractions project.

    • Add UnitsOfWork folder.
    • Create a UrfSampleUnitOfWork class that extends UnitOfWork and implements IUrfSampleUnitOfWork.
    public class UrfSampleUnitOfWork : UnitOfWork, IUrfSampleUnitOfWork
    {
        public UrfSampleUnitOfWork(DbContext context, IRepository<Product> productsRepository) : base(context)
        {
            ProductsRepository = productsRepository;
        }
    
        public IRepository<Product> ProductsRepository { get; }
    }

Web API with URF.Core

  1. Add a new ASP.NET Core Web API project

    • Remove WeatherForecast and WeatherForecastController classes.
    • Add NuGet packages:
      • Microsoft.EntityFrameworkCore.SqlServer
      • URF.Core.EF
    • Reference Models, Abstractions and EF projects.
  2. Add a connection string to appsettings.json.

    "ConnectionStrings": {
    "UrfSampleContext": "Data Source=(localdb)\\MSSQLLocalDB;initial catalog=UrfSample;Integrated Security=True; MultipleActiveResultSets=True"
    }
  3. Register services in Startup.ConfigureServices.

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        var connectionString = Configuration.GetConnectionString(nameof(UrfSampleContext));
        services.AddDbContext<UrfSampleContext>(options => options.UseSqlServer(connectionString));
        services.AddScoped<DbContext, UrfSampleContext>();
        services.AddScoped<IUrfSampleUnitOfWork, UrfSampleUnitOfWork>();
        services.AddScoped<IRepository<Product>, Repository<Product>>();
    }
  4. Add a ProductController controller.

    [Route("api/[controller]")]
    [ApiController]
    public class ProductController : ControllerBase
    {
        public IUrfSampleUnitOfWork UnitOfWork { get; }
    
        public ProductController(IUrfSampleUnitOfWork unitOfWork)
        {
            UnitOfWork = unitOfWork;
        }
    
        // GET: api/Product
        [HttpGet]
        public async Task<ActionResult<IEnumerable<Product>>> GetProduct()
        {
            var products = await UnitOfWork.ProductsRepository.Queryable().ToListAsync();
            return Ok(products);
        }
    
        // GET: api/Produc/5
        [HttpGet("{id}")]
        public async Task<ActionResult<Product>> GetProduct(int id)
        {
            var product = await UnitOfWork.ProductsRepository.FindAsync(id);
            if (product == null)
                return NotFound();
            return product;
        }
    
        // PUT: api/Produc/5
        [HttpPut("{id}")]
        public async Task<IActionResult> PutProduct(int id, Product product)
        {
            if (id != product.Id)
                return BadRequest();
    
            UnitOfWork.ProductsRepository.Update(product);
    
            try
            {
                await UnitOfWork.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!await ProductExists(id))
                    return NotFound();
                else
                    throw;
            }
            return Ok(product);
        }
    
        // POST: api/Product
        [HttpPost]
        public async Task<ActionResult<Product>> PostProduct(Product product)
        {
            UnitOfWork.ProductsRepository.Insert(product);
            await UnitOfWork.SaveChangesAsync();
            return CreatedAtAction(nameof(GetProduct), new { id = product.Id }, product);
        }
    
        // DELETE: api/Product/5
        [HttpDelete("{id}")]
        public async Task<ActionResult> DeleteProduct(int id)
        {
            var result = await UnitOfWork.ProductsRepository.DeleteAsync(id);
            if (!result)
                return NotFound();
            await UnitOfWork.SaveChangesAsync();
            return NoContent();
        }
    
        private async Task<bool> ProductExists(int id)
        {
            return await UnitOfWork.ProductsRepository.ExistsAsync(id);
        }
    }
  5. Update launchSettings.json in the Properties folder.

    • Replace weatherforecast with api/product.
    • Set the Api project as the startup project.
    • Select URF.Core.Sample.v3.Api for debugging and press F5.
  6. Start the Web API project and test with Postman.

    • Turn off 'SSL certificate verification' in Settings > General
    GET: https://localhost:5001/api/product
    GET: https://localhost:5001/api/product/1
    
    POST: https://localhost:5001/api/product
    
    {
        "productName": "Chocolato",
        "unitPrice": 4.00
    }
    • Should return 201 Created with correct Location response header.
    PUT: https://localhost:5001/api/product/4
    
    {
        "id": 4,
        "productName": "Chocolato",
        "unitPrice": 5.00
    }
    DELETE: https://localhost:5001/api/product/4
    
    GET: https://localhost:5001/api/product/4
    
    • Should return 404 Not Found.

urf.core.sample.v3's People

Contributors

tonysneed avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar

urf.core.sample.v3's Issues

Duplicate 'Content' items were included

Severity Code Description Project File Line Suppression State
Error NETSDK1022 Duplicate 'Content' items were included. The .NET SDK includes 'Content' items from your project directory by default. You can either remove these items from your project file, or set the 'EnableDefaultContentItems' property to 'false' if you want to explicitly include them in your project file. For more information, see https://aka.ms/sdkimplicititems. The duplicate items were: 'Properties\launchSettings.json' Demo.UrfCore3.Api C:\Program Files\dotnet\sdk\3.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.DefaultItems.targets 316

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.