Hi,
I have a method in Application layer. It is responsible for validating and adding an account. It is very much the same as what you have done with OrganizationUnit. The problem is that I can not use it this way in my custom API which is in a custom project. (added manually to the solution). I want to send almost the same message as AjaxResponse in the ABP Framework.
public async Task<AccountDto> Create(CreateAccountInput input)
{
var account = new Account
{
Id = (input.Id == Guid.Empty ? Guid.NewGuid() : input.Id),
AccountNumber = input.AccountNumber,
Name = input.Name
};
// When not valid, it throws the first exception and immediately bubbles up to the caller.
if (account.Name.IsNullOrWhiteSpace())
throw new UserFriendlyException(111, L("AccountName_IsMandatory", account.Name));
if (account.Name.Length < 3)
throw new UserFriendlyException(112, L("AccountName_IsTooShort", account.Name));
if (await AccountNameAlreadyExists(account.ParentId, account.Name))
throw new UserFriendlyException(113, L("AccountName_AlreadyExists", account.Name));
// Save
using (var unitOfWork = UnitOfWorkManager.Begin())
{
await _accountRepository.InsertAsync(account);
UnitOfWorkManager.Current.Completed += (sender, args) => { /* TODO: Send email to assigned person or do something else */ };
await unitOfWork.CompleteAsync();
}
return account.MapTo<AccountDto>();
}
This is an example. In reality I would like to know 'exactly' which error was the cause of failure. Throwing "UserFriendlyException" for a validation is not something I could use and test. Look at the code below from the test project:
[Fact]
public async Task Should_Not_Create_Account_With_Same_Name_In_Same_Level2()
{
//Arrange
var account = new CreateAccountInput
{
AccountNumber = "123",
Name = "Same Name"
};
UsingDbContext(
context =>
{
context.Accounts.Add(
new Account
{
Name = account.Name, // "Same Name"
AccountNumber = "456"
});
});
//Act & Assert -- This just tells me that there was an "UserFriendlyException". Nothing more.
var ex = await Assert.ThrowsAsync<UserFriendlyException>(
() => _accountAppService.Create(account)
);
Assert.NotNull(ex);
ex.Code.ShouldBe(113);
}
I have to say that I have many custom API's which I put in their own project in the solution. Their project is a copy of the xx.Web.Host project. They are not intended for the UI. They have their own controller and just call the Application App services. You recommended this approach to me.
By the way I saw AjaxResponse and ErrorInfo classes in the ABP. Should I use them or is there any other better way? I want to response message for all of my custom API's are very much like the AjaxResponse.
Thanks.
Hello,
My application consists of two parts, one is the UI based on the Zero core template which works fine and other one should be a set of custom API's which will be used by mobile apps or other applications.
The custom API's could become very complicated in the future. Is there any recommendation about how to deal with such scenario's? For example, I don't know where to start, should I create a new ASP.NET Core project inside solution and do everything apart from ABP? Or should I copy the ABP.API project from non-core zero module in my solution and use that? Or maybe a another way... please help.
Can anyone point me to the right direction, please? I couldn't find any clear guidance about my scenario in documentation or in the forum.
By the way, I would need multitenancy, authentication and all other goodies of ABP in my custom API's as well.
Thanks, Alex
Hi,
I have a Table-per-Hierarchy inheritance structure in my database. How can I implement that in ABP ? I use Entity framework and that works simple and great. But as far as I can see, the only way in ABP to deal with this is building a custom repository for each drived entity. Am I right? Is there any other and better way to make this work?
Thanks
Hi,
Is is possible to skip the .Application layer en use .Core domain services directly from the controllers? Do I miss anything?
I saw in other posts and in the documentation that we can eliminate the domain services and use .Application services and put logic in there. In some cases it might be a more productive approach. But I wonder what would I miss if I skip the .Application services and DTO's and use Domain directly. Something like ZeroModule. Would I miss transaction, UoW, Logging, Auditing, ....and all that stuff all together or I have them in the .Core Services as well?
Before someone point me to the 'best practices' stories, YES people I know them very well. In my current project I don't need DTO's and have one UI.
By the way, I think you have done a great job with this ABP. Thanks for sharing it.
But