GithubHelp home page GithubHelp logo

griesoft / newslettersub Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 1.0 30 KB

A very simple newsletter subscription service for ASP.NET Core, which provides functionality to manage and maintain a collection of contacts and newsletter subscribers.

License: MIT License

C# 100.00%
asp-net-core efcore newsletter-service

newslettersub's Introduction

Newsletter Subscribing Service

A very simple newsletter subscription service for ASP.NET Core, which provides functionality to manage and maintain a collection of contacts and newsletter subscribers.

The main purpose of developing this was the need to implement this newsletter subscribing feature in many different projects. So because this was developed mostly for my own usage and without bigger visions in mind, the development of this service might be poor in the future.

Build Status NuGet GitHub Release

Installation

Install via NuGet using:

PM> Install-Package NewsletterSub

Setup

Before you can use the service you need to configure the database context for the service. We assumes that you have already an existing Entity Framweork Core DbContext setup. If not, follow the Microsoft docs on how to do so. Of course you may also create an own new DbContext for the service, it's up to you.

Now in your existing DbContext, implement the INewsletterSubDbContext interface. Don't forgt to add a reference to the NewsletterSub.Data namespace.

You have now implemented two new DbSet's and your DbContext should look something like this

public class MyDbContext : DbContext, INewsletterSubDbContext
{
    public DbSet<Contact> Contacts { get; set; }
    public DbSet<NewsletterSubscription> NewsletterSubscriptions { get; set; }
}

Now override the OnModelCreating(ModelBuilder modelBuilder) method of your DbContext and copy & paste this line of code modelBuilder.BuildNewsletterSubModels() in it. So that would look something like this:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    // Any existing code...

    modelBuilder.BuildNewsletterSubModels();

    // Any existing code...
}

The only steps left now is to create a migration and update your database. Just run the following commands on your .NET Core CLI

dotnet ef migrations add <Migration Name>

AND

dotnet ef database update

No additional code setup is required other then the basic service registration in your Startup.cs. Just add the following line of code in the ConfigureServices(IServiceCollection services) method, anywhere after registering your DbContext.

services.AddNewsletterService();

Usage

You can request an instance of the service with Dependency Injection (DI) by just adding the INewsletterService as a parameter to your constructor of your controller.

public class MyWebController : Controller
{
    private readonly INewsletterService _newsletterService;
    
    public MyWebController(INewsletterService newsletterService)
    {
        _newsletterService = newsletterService;    
    }
}

Now you can just use the service to add or remove newsletter subscribers.

Add subscriber

await _newsletterService.AddNewsletterSubscription("[email protected], "Product X");

Remove subscriber

await _newsletterService.RemoveNewsletterSubscription("[email protected], "Product X");

newslettersub's People

Contributors

jooni91 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

mohammed101

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.