Excluding a class as a Controller in convention driven development? - asp.net-core

In our project I recently dumped a list of routes to controllers in order to learn a new asp.net project that I recently joined the team of.
However, they have a class used as an attribute in a namespace that is being picked up by asp.net as a controller class, which is registering 'null' / empty routes in the dump, named GenericController.
Is there a way to mark this class using attributes (Similar to marking methods using [NonAction]) as 'not a controller?' And if not, what alternatives are there for excluding 'matching by convention' named classes from being controllers?

Is there a way to mark this class using attributes (Similar to marking methods using [NonAction]) as 'not a controller?'
There's a built-in [NonControllerAttribute].
Also, you can make your class a nested class, an abstract class, an internal class, a generic class , etc to avoid picking it as a Controller.
Fore more details, see Source Code

Related

#functions vs PageModel in Razor Pages

Coming from Blazor (Razor COMPONENTS aka .razor files) it does not matter if one uses the #code directive or creating a code behind .razor.cs file, since AFAIK they are both mashed together since they are partial classes. This looks somewhat similar, but is fundamentally different from razor pages .cshtml PageModel.
However I have seen many examples where the Action Methods aka OnGet() OnPost() have been written in both places. So my question is:
Is there any difference or implications (or official documentation) in declaring members (Properties, Methods) in either the #functions block or the PageModel ?
Like, what if both have an exact same member (which is theoretically possible, since they are different classes - (not tested)... Precedence... an so on ??
PageModel classes can be instantiated easily and predictably from code for unit testing purposes. You could instantiate an instance of the generated Razor page class, but as far as I know, there is no formal contract for the algorithm used to generated the class name. There is nothing to stop the framework developers from changing the naming convention from one version of the SDK to another, breaking all your tests.

Better to add method to pre-defined class or make subclass?

Say you want to add a lengthOfFirstLine method to the predefined File class. Is it a better practice to modify the existing class, or make a new class that extends the File class with your new method?
EDIT -- Specifically, the situation is that a class is lacking one method in particular. I don't want to completely change the class, but rather augment it with that method.
It depends if the method is applicable to all elements of the class File. For instance, lengthOfFirstLine doesn't apply to binary files, so probably it doesn't belong in a generic File class, but if your class only represent text files, then it should go there.
For .NET languages, there's also the option of using extension methods. This way you don't have to "dirty up" a class by adding helper/utility methods to it, and no inheritance is required as well - you add functionality to a class by simply adding a using statement to your code.
Agree with Luis and Lester. If you are using .Net the extension methods are the way to go for this sort of functionality. But you should try not add LengthOfFirstLine to a base class if you can open all sorts of files such as binary files. You would sub class it to a FileClass and add the method to that.
Remember that the extension methods in .Net are syntactic sugar anyway. You can simulate it in your own language using Static classes and methods. This is what .Net does under the covers anyway.
For example have a static FileHelpers class and have various static helper methods on it. The first parameter for each of these static methods would be the File class. So you could call this using FileHelpers.GetLengthOfFirstLine(myOpenedFile)

BlazeDS - Conversion from ArrayList <BaseClass> on java side to Actionscript

So we have a java class with two ArrayLists of generics. It looks like
public class Blah
{
public ArrayList<ConcreteClass> a;
public ArrayList<BaseClass> b;
}
by using [ArrayElementType('ConcreteClass')] in the actionscript class, we are able to get all the "a"s converted fine. However with "b", since the actual class coming across the line is a heterogeneous mix of classes like BaseClassImplementation1, BaseClassImplementation2 etc, it gets typed as an object. Is there a way to convert it to the specific concrete class assuming that a strongly typed AS version of the java class exists on the client side
thanks for your help!
Regis
To ensure that all of your DTO classes are marshalled across AS and Java, you need to define each remote class as a "remote class" in AS by using the "RemoteClass" attribute pointing to the java class definition like this [RemoteClass(alias="com.myco.class")].
BlazeDS will perform introspection on the class as it is being serialized/de-serialized and convert it appropriately (see doc below). It doesn't matter how the classes are packed or nested in an array, as long as it can be introspected it should work.
If you need special serialization for a class you can create your own serialization proxys (called beanproxy) by extending "AbastractProxy" and loading them into blazeds using the PropertyProxyRegistry register method on startup.
You will find most of this in the Blaze developers guide http://livedocs.adobe.com/blazeds/1/blazeds_devguide/.
Creating your own beanproxy class look here: //livedocs.adobe.com/blazeds/1/javadoc/flex/messaging/io/BeanProxy.html

Error mapping UserType from to property with NHibernate

I needed a way to trim strings within my persistent class because my legacy database is using char fields. I downloaded the nHhaddIns dll to use its TrimString class which is derived from IUserType.
Using their example I created a property in my mapping class as shown at the bottom.
uNHAddIns is added as a project within my solution. However, I received this error:"Could not determine type for: uNhAddIns.UserTypes.TrimString, uNhAddIns, for columns: NHibernate.Mapping.Column(HSTAT)"
I tried running the example that is in the uNhAddIns project and receive the same error. Any ideas?
<property name="HSTAT" column="HSTAT" type="uNhAddIns.UserTypes.TrimString, uNhAddIns" />
Don't know if you've managed to fix this already, but does your own uNhAddIns.UserTypes.TrimString inherit from IUserType? My own pattern for user types in NHibernate involves the type implementation living in the DataModel, and the required IUserType interface living separately in my DataAccess layer. The IUserType implementation does the necessary marshalling between the database and my DataModel type implementation.
I just came across this same error when trying to use the DataModel class in my mapping file rather than the IUserType implementation.

When should we create a static class?

How can we distinguish to create a class which is static?
A static class forces all of its methods to be static and prohibits an instance constructor therefor can't be instantiated. If your question extends to WHEN to use static and WHEN instance, please do a search on StackOverflow (or check out the Related box on this page)
At least in C#,
static classes and class members are used to create data and functions that can be accessed without creating an instance of the class.
If you want the class to be static in nature i.e. have only 1 copy within the program (VM) then there are two obvious mechanisms:
1. Make all members and methods of the class static (Java/C#).
2. Use Singleton design pattern.
For this case (static in nature), we don't have a language construct and hence one of the above technique is used.
As to your question for this case, such classes should be created if you want your functionality to be accessible globally, unchanged and instantly accessible e.g. utility methods, global constants etc.
Secondly, the keyword 'static' is used with classes to increase their visibility in the package. This keyword can only be applied on inner classes and allows the access to inner classes without the context of their parent class.
Such kind of static classes should be used only for those inner classes that serve their purpose within the parent class as well as are useful outside the class or the package e.g. Key of a POJO.