MEG.FactoryBase
1.0.0
dotnet add package MEG.FactoryBase --version 1.0.0
NuGet\Install-Package MEG.FactoryBase -Version 1.0.0
<PackageReference Include="MEG.FactoryBase" Version="1.0.0" />
<PackageVersion Include="MEG.FactoryBase" Version="1.0.0" />
<PackageReference Include="MEG.FactoryBase" />
paket add MEG.FactoryBase --version 1.0.0
#r "nuget: MEG.FactoryBase, 1.0.0"
#:package MEG.FactoryBase@1.0.0
#addin nuget:?package=MEG.FactoryBase&version=1.0.0
#tool nuget:?package=MEG.FactoryBase&version=1.0.0
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 | Versions 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. |
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
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 |