Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should TenantId be set automatically if entity implements IMultiTenant interface? #1130

Closed
Webdiyer opened this issue May 17, 2019 · 5 comments
Labels

Comments

@Webdiyer
Copy link
Contributor

If entity implements IMultiTenant interface, then property value of TenantId must be set manually within the constructor when the entity is created, it's tedious and also dangarous if someone forgot to set it, should it be better to set TenantId value automatically like creation properties in the code below?

@maliming
Copy link
Member

@Webdiyer
Copy link
Contributor Author

Sorry I'm not familiar with aspnetboilerplate and it's not easy to understand in which situation it's impossible to set TenantId value automatically, in abp vnext framework, there're no IMustHaveTenant and IMayHaveTenant interfaces but just one IMultiTenant interface. My problem is: my app service is simply inherited from AsyncCrudAppService, I have to rewrite CreateAsync method completely if I want to set the TenantId value of the entity object to be created, or should I add a TenantId property to my dto object and set this property value from within web layer and then mapped to entity? Which one is the recommended way? Thanks in advance.

@maliming
Copy link
Member

The tenant ID and user ID can be modified within the scope.

If SaveChangesAsync is called somewhere, the wrong tenant ID and user ID may be used.

I personally think AsyncCrudAppService is not suitable for most scenarios.

@hikalkan
Copy link
Member

@maliming explained it well.

Think these cases:

ApplyAbpConcepts see a new entity with tenant id is null and CurrentTenant.Id is X. Setting entity.TenantId to X may not be always correct. What if you intentionally wanted to set it to null (you wanted to create a host-side entity)?

It is similar if entity with tenant id is X and CurrentTenant.Id is null. Setting it to null is not correct.

You can create a new entity in tenant X scope and save your DbContext in tenant Y scope. So, only the code created the entity can know what is the correct tenantid.

Otherwise, it is pretty easy to set it in ApplyAbpConcepts for ABP framework.

BTW, if you problem is AsyncCrudAppService, then you are right. I created an issue to implement it: #1134

@Webdiyer
Copy link
Contributor Author

Thanks for the detailed explanation and the great work you've done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants