C # – Design Web API with Command Handler

I'm setting up a new API to set up LocalAccounts and need your advice on how to design the API with CQS

some background:
LocalAccount is an account that refers to residential or corporate customers and contains account information with one or more cards (plastic cards and a virtual card).
Example for local accounts: PrivateDebitLocalAccount, CorpoeateKreditLocalAccount PrivateKreditLocalAccount …

I want to use CQS to make the command available to the action for this entity: AddNewLocalAccountCommand, UpadteLocalAccountCommand, and so on.

My client does not want to work with many commands for the same action: 4 AddNew command for private bitLocalAccount, CorpoeateCreditLocalAccount,
CorpoeateDebitLocalAccount, PrivateCreditLocalAccount and then 4 update command and 4 delete commands … and so on

So I decided to provide a command to create all 4 types of localAccount AddNewLocalAccount, which gets a flat object with all the fields and maps the request to my command in the WebAPI instance (I do this for down-comparison of contract changes for my command) , and send it to Handler with Jimmy Bogard's MediatR library.
In the handler, I call AggrigateLocalAccountServices with 2 dependencies of ServiceFactories to retrieve the required domain object: AccountServiceFactory and CustomerServiceFact. This helps get the relevant domain object from the command: Customer (private or customer) and account based on AccountType in the command (credit or debit)

Example for AddNewCorporateLocalAccountRequest

    var LocalAccountData = new LocalAccountData (Guid.NewGuid ())
CustomerType = CustomerTypes.Corporate,
AccountTypes = AccountTypes.Credit,
Credit Limit = 200,
Deposit = 100,
CompanyId = "43545",
MonthlyLimit = 50,
FirstName = "ddd",
Last name = "Deee",

// In My Controller WebApi, I have mapped the request to the command, even if it's mixed
// fields from direct debit and credit ..

var command = new AddNewLocalAccountCommand (LocalAccountData.Id ==
Guid.Leer? Guid.NewGuid (): LocalAccountData.Id)
CompanyId = LocalAccountData.CompanyId, // Company field
CustomerType = LocalAccountData.CustomerType, // define that
CustomerType (private / company) for LocalAccountFactory (customer)
AccountTypes = LocalAccountData.AccountTypes, // define that
AccountType (debit / credit) for AccountFactory (customer)
CreditLimit = LocalAccountData.CreditLimit, // Credit Data
// Send to commandHandler with Mediatr:
var x = new AddNewLocalAccountCommandHandler (
New LocalAccount Aggregator (
localAccountServiceFactory, accountFactoryService));

var result = mediatR.Send (command);

// in my commandHandler method

        public void handle (AddNewLocalAccountCommand command) {
// reassign all data to the local DTO account so that I can ask the factories in AggRoot to extract the desired domain
var localAccountData = new LocalAccountData (command.Id)
CustomerType = command.CustomerType,
AccountTypes = command.AccountTypes,
Balance = Command.Balance,
var customerAccontId =
.AddNewLocalAccount (localAccountData);

// my agg root class I am making an interface available for adding localAccount. So the implementation is done

Public virtual Guid AddNewLocalAccount (ILocalAccountData data)
var localAccount = CreateLocalAccount (data);
var vc = CreateVirtualCard ();
localAccount.Accounts.Cards.Add (vc);
// Now I have to decide where to assign localAccount to my EF
// object, here or should it be within the service that the
// right type of tables to be mapped
customerDto = Mapper.Map(Request);
_context.Customers.Add (customerDto);
_context.SaveChanges ();
return Guid.NewGuid ();

private ILocalAccount CreateLocalAccount (ILocalAccountData data)
var accountService =
_accountFactoryService.GetService (data.AccountTypes);

var localAccountFactoryService =
_localAccountFactoryService.GetService (data);

// create method is called from the right LocalAccountService
who received the correct domain object.
return localAccountFactoryService.Create (data);

// Here's one of the services that can create the right localAccount type based on the data from the client:

    public class CorporateLocalAccountService: ILocalAccountService
private IAccountService account;

public CorporateLocalAccountService (IAccountService account)
_account = account;
public ILocalAccount CreateData (ILocalAccountData data)
return new CorporateLocalAccount (new entity (data.Id))
Account = _account.Create (data),
Contact = new person {last name = data.lastname, firstname = data.firstname, mobilenumber = data.mobilephone},
CompanyId = data.CompanyId,
CompanyName = Data.CompanyName,




I have some questions about this design:

1.My client can not forward my internal domain so that it can send and send bad fields in customer account requests because a container object is being used. In addition to sending a JSON example of a possible valid contract or changing flat fields in object In my LocalAccountData: DebitAccountData, CreditAccountData, PrivateData, CorporateData. How can I tell that my customer does not make a mistake in the request? Should I change my API request?
2. In my CorporateLocalAccountService, I return the domain object to the aggregate. Should this class create the data in DB using EF, or just return the domain model, and aggregates create it?
3.I work with the command prompt and in CommandHander I call the aggregate object that needs to create localAccount (insert data into 4-5 tables and create a virtual map as a result of building the LocalAccount and submit it to the Client back.) How do I work with my Entity Framework? I mean, do I need repositories or should I call DBContext and use the EF objects directly in the aggregate and avoid repositories? //
I use the commandHandler with TransactionCommandHandler. Do I need the transaction space in the Aggregate class, or can I use the transaction space of my commandHandler decorator?

Many Thanks