Giter Club home page Giter Club logo

sol_aspnetcore.factory's Introduction

Asp.net Core Factory.

A Library for implement the factory method for Dependency injection in asp.net core.

Step 1

Add following nuget package in your asp.net core Solution. Generic badge

Using Nuget Package Manger:

PM> Install-Package Framework.AspnetCore.Factory -Version 1.0.0

Using .Net CLI:

> dotnet add package Framework.AspnetCore.Factory --version 1.0.0

Using Package Reference:

<PackageReference Include="Framework.AspnetCore.Factory" Version="1.0.0" />

Step 2

Create a Services classes.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace Api.Demo.Contexts
{
    public interface IDemo1
    {
        String TestDemo();
    }

    public class Demo1 : IDemo1
    {
        String IDemo1.TestDemo()
        {
            Debug.WriteLine("Demo 1");
            return "Demo 1";
        }

        public int Test { get; set; }
    }
}

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace Api.Demo.Contexts
{
    public interface IDemo2
    {
        String TestDemo();
    }

    public class Demo2 : IDemo2
    {
        String IDemo2.TestDemo()
        {
            Debug.WriteLine("Demo 2");
            return "Demo 2";
        }
    }
}

Step 3

Go to Startup.cs file, In the ConfigureService method add the following services.

services.AddFactory((x) =>
{
    x.AddTransient<IDemo1, Demo1>();
    x.AddSingleton<IDemo2, Demo2>();
    
     // Or using fluent
      //x.AddTransient<IDemo1, Demo1>()
      // .AddSingleton<IDemo2, Demo2>();
});

Full ConfigureService Method Code.

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();

    services.AddFactory((x) =>
    {
        x.AddTransient<IDemo1, Demo1>();
        x.AddSingleton<IDemo2, Demo2>();
        
        // Or using fluent
        //x.AddTransient<IDemo1, Demo1>()
        // .AddSingleton<IDemo2, Demo2>();
    });

    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new OpenApiInfo { Title = "Api.Demo", Version = "v1" });
    });
}

Step 4

Inject factory in the controller class.

using Api.Demo.Contexts;
using Framework.AspnetCore.Factory.Core;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Api.Demo.Controllers
{
    [Produces("application/json")]
    [Route("api/demo")]
    [ApiController]
    public class DemoController : ControllerBase
    {
        private readonly IExecuteFactory executeFactory = null;

        public DemoController(IExecuteFactory executeFactory)
        {
            this.executeFactory = executeFactory;
        }

        [HttpGet("demo1")]
        public IActionResult Demo1()
        {
            var response = executeFactory.Execute<IDemo1>().TestDemo();

            return base.Ok(response);
        }

        [HttpGet("demo2")]
        public IActionResult Demo2()
        {
            var response = (executeFactory.Execute<IDemo2>()).TestDemo();

            return base.Ok(response);
        }
    }
}

sol_aspnetcore.factory's People

Contributors

kishornaik avatar

Stargazers

 avatar

Watchers

James Cloos avatar  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.