Giter Club home page Giter Club logo

dotnetcore-blog's Introduction

dotnetcore-blog: A realistic example of using modern frameworks

I found it to be incredibly hard to get an example of how to use dotnetcore in a non hello world application. Outside of Getting Started tutorials people need to use real libraries and use them together to accomplish a goal. We want to pull from configuration files and we all have one off problems to deal with.

I got frustrated one day when I was updating dotnetcore 1.0.0 to 1.1.0 and found it incredibly hard to find answers on how to fix it. So I decided to create a "latest dotnetcore" realistic example that uses common frameworks and patterns to see how dotnetcore really handles these situations.

I chose the subject matter of a blog half because I'm mocking the getting started tutorials I am working to explain in more detail, and half because it's a concept that most of us clearly understand.

I chose SQL Server as a database provider simply out of convenience. I'd like to perhaps create a branch that uses DocumentDB in Azure as well.

Current Versions

Microsoft.NETCore.App: "1.1.1"

NETStandard.Library": "1.6.1

Microsoft.EntityFrameworkCore: "1.1.1"

Real World Uses

This repository shows examples of:

  • Returning proper HTTP Status code responses from aspnetcore Web API
  • Entity Framework Core with first migrations
  • Decoupling Entity Framework core from clients
  • Using appsettings secrets to hide connection strings and sensitive data
  • AutoMapper to make domain entities to safer data transfer objects
  • Unit testing projects [coming soon]

Projects

API

An aspnetcore Web API project meant to feed blog data to any client.

Business

Business is meant for services. I use this layer as an easy layer for a client to access data based on the client's needs.

Domain

Maybe Domain isn't a great name here. It's more a Data project. Meant to house my Entity Framework models and configuration. Domain logic is more stored in the Business project.

DTO

The Data transfer objects project is meant to create a simple model for the calling code that does not show sensitive data and does not show irrelevant data to the calling client. DTOs are common when mapping from an Entity Framework model.

Running the Example

First, you'll need the correct version of the dotnet sdk. You can get that here: https://www.microsoft.com/net/download/core#/current. Be careful to be on the right tab. There is an 'LTS' (Long Term Support) tab that typically has an older version of the SDK. You want to look at the 'Latest' tab. I'm using the .NET Core 1.0.3 SDK - Installer x64 and the Visual Studio 2015 Tools (Preview 2) x64.

Next, you'll need to create your secret appsettings file. As shown in core-blob.api\Startup.cs, you will need an appsettings.secrets.json file in your API root (right next to the existing appsettings.json for the API). This will hold your connection string for your database. You can look at appsettings.json to see the format that you need to use, but it should look something like this, depending on your connection, mine is using a local database.

{
    "ConnectionStrings": {
        "BlogConnectionString": "Server=(localdb)\\mssqllocaldb;Database=BlogExample;Trusted_Connection=True;MultipleActiveResultSets=true"
    }
}

Once you have this in place you should be good to go. You can use the command prompt to update your entity framework database using dotnet ef or you can use the Package Manager Console (with the default project pointing to core-blog.domain) and run Update-Database. This runs all of the migrations and creates the database in its current form.

If you have any questions or comments, feel free to contact me on twitter at @StevenHook

dotnetcore-blog's People

Contributors

swhook52 avatar jagostonicei avatar

Watchers

James Cloos avatar Jason Agostoni avatar

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.