MEG.FactoryBase 1.0.0

dotnet add package MEG.FactoryBase --version 1.0.0
                    
NuGet\Install-Package MEG.FactoryBase -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MEG.FactoryBase" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MEG.FactoryBase" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="MEG.FactoryBase" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MEG.FactoryBase --version 1.0.0
                    
#r "nuget: MEG.FactoryBase, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MEG.FactoryBase@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MEG.FactoryBase&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=MEG.FactoryBase&version=1.0.0
                    
Install as a Cake Tool

1.1) Library Code Details

You can examine in detail how the library codes work by reading my Factory Design Pattern in MEG Library article.

1.2) Implementation

First, include the MEG.FactoryBase library into your project via the Nuget Package Manager.

The AddFactoryBase method must be called to inject classes in Program.cs. We can provide Assembly information where classes derived from FactoryBase will be located with FactoryBaseSettings.


var factoryBaseSettings = FactoryBaseSettings.GetFactoryBaseSettings<PaymentFactory>();
builder.Services.AddFactoryBase(factoryBaseSettings);

If we rewrite the code structure below using FactoryBase.

void Pay(CreditCardTypes creditCardType)
{
    switch (creditCardType)
    {
        case CreditCardTypes.Mastercard:
            PayWithMastercard();
            break;
        case CreditCardTypes.Visa:
            PayWithVisa();
            break;
    }
}

void PayWithMastercard()
{
    
}
void PayWithVisa()
{
    
}

The PaymentBase class that derives from FactoryBaseModel must be created.

public abstract class PaymentBase: FactoryBaseModel<CreditCardTypes>
{
    public abstract string Pay(int amount);
}

Classes derived from PaymentBase and marked with the FactoryModelIdentifier attribute must be created.


[FactoryModelIdentifier<CreditCardTypes>(CreditCardTypes.Mastercard)]

public class MastercardPayment : PaymentBase
{
    public override string Pay(int amount)
    {
        // other operations
        return $"{amount} TRY paid with Mastercard"
    }
}

[FactoryModelIdentifier<CreditCardTypes>(CreditCardTypes.Visa)]
public class VisaPayment: PaymentBase
{
    public override string Pay(int amount)
    {
        // other operations
        return $"{amount} TRY paid with Visa card"
    }
}

The PaymentFactory class that derives from FactoryBase must be created.


public class PaymentFactory : FactoryBase<PaymentBase,CreditCardTypes>
{
}

If we want to use it in the Controller, the Pay method in the PaymentFactory is called when it comes from the service.


public IActionResult GetPayment([FromServices] PaymentFactory paymentFactory,[FromQuery] CreditCardTypes creditCardType , [FromQuery] int amount = 15)
{
    var paymentResult = paymentFactory.CreateBaseModel(creditCardType)!.Pay(amount);
    return Ok(paymentResult);
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 175 3/30/2024