How to add a new entity properly? #347
-
I see that there are a couple of example entities ( Brands/Categories), but zero documentation on how to properly add my own entities into this framework. This is proving to be a stumbling block for me to be able to use what otherwise looks like a great framework. TIA, Miles |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 2 replies
-
Below are the steps I use when creating a new entity. For details on each step, look in the exiting BlazorHero files. Adding new Entity
API for the new Entity
UI FilesIn Client\Pages folder, create all the necessary razor files to manage the entity:
And finally, edit Client\Shared\NavMenu.razor to add a link to access the entity management page. |
Beta Was this translation helpful? Give feedback.
-
Yes that is correct, ED generates both the entity class and the DBContext class (with DBset etc.).
I am going to look at my ED template that I use for generating the entity classes to see if I can easily generate the other files that you require. If I can do that, then great. If not, then I might try what you talked about, build a small program to generate those files into the appropriate folders. I use Sql Server myself, and of course use the Database First approach as well.
I think that if there is a way to generate these entity files programmatically then your framework has a real chance at success.
Cheers,
Miles
From: Ajakblackgoat ***@***.***>
Sent: Saturday, November 27, 2021 1:50 AM
To: blazorhero/CleanArchitecture ***@***.***>
Cc: MilesGibson ***@***.***>; Author ***@***.***>
Subject: Re: [blazorhero/CleanArchitecture] How to add a new entity properly? (Discussion #347)
Not familiar with DevArt Entity Developer but I believe it is for creating the entity class right?
For my current project, I'm using MySQL 8 as the db. And I design the database table first. Then I use MySql script to generate the entity class codes for the table. At least that takes care of the entity.cs file.
Most of the files are created manually. But not all are required. It really based on the use case of the entity.
Yes, it's troublesome if we have 10's of tables to create their respective entity files. I plan to write a small prog just to generate all those files automatically, but still haven't got the time to do so.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub <#347 (reply in thread)> , or unsubscribe <https://github.com/notifications/unsubscribe-auth/ACVNKLXSKH2T5A5TEIBJIVLUOCLSXANCNFSM5GJY4HDQ> .
Triage notifications on the go with GitHub Mobile for iOS <https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675> or Android <https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub> . <https://github.com/notifications/beacon/ACVNKLS2WK25FZDCAF7LRWDUOCLSXA5CNFSM5GJY4HD2YY3PNVWWK3TUL52HS4DFWFCGS43DOVZXG2LPNZBW63LNMVXHJKTDN5WW2ZLOORPWSZGOAANBEVQ.gif>
|
Beta Was this translation helpful? Give feedback.
-
Another thought:
As I am reviewing the different areas of your application, I realized that it would be a lot easier to generate code in various places by using partial classes. Using Shared\Constants\Permissions.cs as an example, I would re-structure this to something like this:
Shared\Constants\Permissions_Framework.cs -- where stuff like Users/UserRoles/Permissions/Hangfire etc all go in here, as a ‘public partial static class Permissions’
And then there would also be:
Shared\Constants\Permissions_Entities.cs that would also be defined as ‘public partial static class Permissions’. I could write to this file myself then.
Same for ApplicationConstants.cs, and likely most of the other areas as well.
Cheers,
Miles
From: Ajakblackgoat ***@***.***>
Sent: Saturday, November 27, 2021 1:50 AM
To: blazorhero/CleanArchitecture ***@***.***>
Cc: MilesGibson ***@***.***>; Author ***@***.***>
Subject: Re: [blazorhero/CleanArchitecture] How to add a new entity properly? (Discussion #347)
Not familiar with DevArt Entity Developer but I believe it is for creating the entity class right?
For my current project, I'm using MySQL 8 as the db. And I design the database table first. Then I use MySql script to generate the entity class codes for the table. At least that takes care of the entity.cs file.
Most of the files are created manually. But not all are required. It really based on the use case of the entity.
Yes, it's troublesome if we have 10's of tables to create their respective entity files. I plan to write a small prog just to generate all those files automatically, but still haven't got the time to do so.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub <#347 (reply in thread)> , or unsubscribe <https://github.com/notifications/unsubscribe-auth/ACVNKLXSKH2T5A5TEIBJIVLUOCLSXANCNFSM5GJY4HDQ> .
Triage notifications on the go with GitHub Mobile for iOS <https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675> or Android <https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub> . <https://github.com/notifications/beacon/ACVNKLS2WK25FZDCAF7LRWDUOCLSXA5CNFSM5GJY4HD2YY3PNVWWK3TUL52HS4DFWFCGS43DOVZXG2LPNZBW63LNMVXHJKTDN5WW2ZLOORPWSZGOAANBEVQ.gif>
|
Beta Was this translation helpful? Give feedback.
-
Yes indeed. That's what I'm doing right now for the permission.cs. Changed it to partial and create another partial permission class for business entities. |
Beta Was this translation helpful? Give feedback.
-
When I look at this:
Create Query class (GetAll, GetAllPaged, GetById, Export, etc as required) and their respective Response class files in Application\Features folder.
It makes me wonder why you don’t use generics here?
public virtual IEnumerable<T> Get(
Expression<Func<T, bool>> filter = null,
Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
string includeProperties = "")
{
try
{
// Get the dbSet from the Entity passed in
IQueryable<T> query = objectSet;
// Apply the filter
if (filter != null)
{
query = query.Where(filter);
}
// Include the specified properties
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
// Sort
if (orderBy != null)
{
return orderBy(query).ToList();
}
else
{
return query.ToList();
}
}
catch (Exception ex)
{
var msg = ex.Message;
return null;
}
}
And so on…
From: Ajakblackgoat ***@***.***>
Sent: Thursday, November 25, 2021 11:28 PM
To: blazorhero/CleanArchitecture ***@***.***>
Cc: MilesGibson ***@***.***>; Author ***@***.***>
Subject: Re: [blazorhero/CleanArchitecture] How to add a new entity properly? (Discussion #347)
Below is the steps when creating a new entity. For details on each step, look in the exiting BlazorHero files.
Adding new Entity
1. Create and define the new Entity class file in Domain\Entities folder .
2. Edit file Infrastructure\Contexts\BlazorHeroContext.cs to add DbSet<> prop and define the entity props in OnModelCreating() function if required.
3. Edit Shared\Constants\Permission\Permissions.cs to add new static class of the new entity containing the permission constants.
4. Edit Shared\Constants\Application\ApplicationContants.cs to add the new entity cachekey constant.
API for the new Entity
1. Create a new FilterSpecification class file in Application\Specifications folder.
2. Create a new AddEditCommand class file in Application\Features folder.
3. Create a new AddEditCommandValidator class file in Application\Validators.
However, I prefer to add the AddEditCommandValidator class in the same file as the AddEditCommand created above.
4. Create a new DeleteCommand class file in Application\Features folder.
5. Create Query class (GetAll, GetAllPaged, GetById, Export, etc as required) and their respective Response class files in Application\Features folder.
I rarely need the response file because I simply reuse the entity class as the return type for each query. However, if response class is required to minimize the amount of data to be returned, I prefer to define the Response class in the Query files itself instead of seperate file.
6. Create a new controller class file in Server\Controllers\v1 folder.
7. Create new Manager class and interface files in Client.Infrastructure\Managers folder.
8. Create a new Profile class file in Application\Mappings folder.
UI Files
In Client\Pages folder, create all the necessary razor files to manage the entity:
* Page to search/list the entity
* Page to add/edit entity
And finally, edit Client\Shared\NavMenu.razor to add a link to access the entity management page.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub <#347 (comment)> , or unsubscribe <https://github.com/notifications/unsubscribe-auth/ACVNKLSGGDFZN7VI6L3L7GLUN4SHFANCNFSM5GJY4HDQ> .
Triage notifications on the go with GitHub Mobile for iOS <https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675> or Android <https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub> . <https://github.com/notifications/beacon/ACVNKLVY7F2R3TLLZUZB5U3UN4SHFA5CNFSM5GJY4HD2YY3PNVWWK3TUL52HS4DFWFCGS43DOVZXG2LPNZBW63LNMVXHJKTDN5WW2ZLOORPWSZGOAAM765Q.gif>
|
Beta Was this translation helpful? Give feedback.
Below are the steps I use when creating a new entity. For details on each step, look in the exiting BlazorHero files.
Adding new Entity
DbSet<>
prop and define the entity props inOnModelCreating()
function if required.API for the new Entity