Declare some types and use globally in a Razor/Blazor page

91 Views Asked by At

Sorry for the confusing title of my question, I am not sure how to make it clearer.

Let's say we have a razor/blazor pages, and it is a list of Users

Reality:

<component EntityType="User" .... />
@{
   var item = someAPI.get<User>(id);
}
...
@code{
   var anotherItem = new User();
   anotherItem.FirstName = "John";
   anotherItem.Price = 100; //error will be prevented at compile time;
   ...
}

As we can observe this page is quite "User-specific" with a lot of User as the type, and when we want to replicate it (with some modifications) for a list of Product, we need to replace the type either manually or in a batch which both are more or less error-prone. Some errors will be detected at compile time, but not always all of them.

Is there any way to declare some types in the beginning and use them in both tags and codes, so that the compiler will eventually generate finally the same thing as above?

Dream:

##DECLARE ITEM_TYPE = User##
##DECLARE MAYBE_ANOTHER_ITEM_TYPE = UserPreferrence##

<component EntityType="ITEM_TYPE" .... />
@{
   var item = someAPI.get<ITEM_TYPE >(id);
}
...
@code{
   var anotherItem = new ITEM_TYPE ();
   anotherItem.FirstName = "John";
   anotherItem.Price = 100; //error will be prevented at compile time;
   ...
}

Restriction: Such pages cannot be converted to a generic component.

0

There are 0 best solutions below