imb13
imb13

Reputation: 135

Blazor - private component

Is there way to create a Blazor component private for the project that it is being used in?

For example, let's look at Razor Class Library ProjectOne that contains two components: omponent ComponentA and component ComponentB. The component ComponentB is child of component ComponentA.

ProjectOne structure:

ProjectOne

---- Components

---------- ComponentA.razor

---------- ComponentB.razor

There is another project which is Blazor application ProjectTwo. There is a reference in this project to ProjectOne. When calling component ComponentA in razor page, the child component ComponentB is visible and accessable as well.

Is there a way to create this child component ComponentB internal only for project ProjectOne where it is being used within the component ComponentA?

Upvotes: 7

Views: 1535

Answers (2)

Neil W
Neil W

Reputation: 9257

As it looks like the sub-component needs to be public, rather than put it in another project, I just put it in a deeper namespace.

If the root component that I want to expose publicly is in

MyApp.Components.Widget

then I put the child components in

MyApp.Components.Widget.Internal

Achieves a similar outcome to the advice from the earlier answer without the need for another project, seeing as the best we can do is obfuscate it in lieu of making it 'internal' or 'private'.

Upvotes: 5

Jay Byford-Rew
Jay Byford-Rew

Reputation: 6024

Possibly not ideal but create another project to support your public project and only referenced by your public project. Your consuming project only references your public project. So for you example it would change to ...

  • ProjectTwo (App project)
    • ProjectOne (Api component project)
      • ProjectOneInternalUse (supporting project for API use)

(unfortunately you still need to refefence the 'internal use' project in ProjectTwo (the app project) but at least it will be marked as internal in the naming within the namespace)

Upvotes: 0

Related Questions