How can I get a model instance inside a DataAnnotationsModelMetadataProvider derived class in MVC2? To be more precise, how can I get the model instance to which a property belongs, inside a GetMetadataForProperty derived method?
Related
I have a class User that inherits from ApplicationRecord. When I check User.ancestors.include?(Class), the result is false but for User.class, the result is Class.
What's the use case for the information supplied by the class method in such cases? What does it really tell me to be of practical use? It doesn't seem to have anything to do with inheritance.
What's the use case for the information supplied by the class method in such cases? What does it really tell me to be of practical use? It doesn't seem to have anything to do with inheritance.
class works the same way for every object. Calling class on a class provides the same information as calling class on an instance. That's because in Ruby, classes are instances, too.
'foo'.class returns String because 'foo' is an instance of String. Likewise, User.class returns Class because User is an instance of Class.
In particular, User is not an instance of ApplicationRecord.
It might not be obvious that User is an instance of Class when creating it via the class keyword:
class User < ApplicationRecord; end
But it becomes very obvious when you create it explicitly via Class.new: (both examples produce the same result)
User = Class.new(ApplicationRecord)
User.class #=> Class
Because the above is just like: (using String.new for demonstration purposes)
foo = String.new('foo')
foo.class #=> String
User is a class. All classes are instances of the class Class (i.e., User.class #=> Class). Therefore, all instance methods defined on Class, or on Class's ancestors, can be invoked on User (i.e., every instance method of Class is a class method of User).
For example, one of Class's instance methods is instance_methods. One can therefore execute User.instance_methods to obtain an array of User's instance methods.
After reading another StackOverflow post, I realized that it can only be understood by keeping the following in mind:
User class has a double role. It plays Class as well as Object.
As Class, it lets other classes inherit from it.
As Object, it inherits the class method all the way up from the Object class.
If I wanted my User.class to behave on the traditional lines (that is, User.class shows ApplicationRecord), then I have to override
it as follows:
class User
def self.class
self.superclass
end
end
Classes in Ruby are first-class objects—each is an instance of class
Class
User is an object of class Class (which inherits from Object).
class is an instance method of Object. It returns the class of the object. Therefore, User.class returns Class. Every object will have this method, and it is useful to know what the object is (e.g., for debugging). Consider this code:
1.class
# => Fixnum
"q".class
# => String
The reason why User.ancestors does not return Class is because it returns the modules included in the module. For example, Class.ancestors will include Class, but not User because User is an object of Class, which includes its own modules.
Is there any possibility to map an existing individual of a class to another class with Protege functions in Jess? The function make-instance creates new instance, while modify-instance allows instance slot updates. But, I would like to use an existing class instance to make it as an instance of another class?
I understand the instance variables on an action class are thread-safe since action classes are instantiated per request. But I have this need for extending action classes and I'm concerned about the thread-safety.
Say, for example I have some common attributes and a few methods handling those attributes among several action classes. I prefer to put them in a single action class and make that extend the ActionSupport. And all action classes will then extend the base action class that I just created. My question is, are the instance variables on the base action class thread-safe? Does S2 manage the base action class at all?
Also what makes an action class an action class to be managed by the S2 and instantiated per request? Getting declared in the struts.xml? Extending ActionSupport class?
I think you're a bit confused about how Java works.
If you have a class A and a class B extends A, when you instantiate B there is a B. It's not like there's a single instance of A backing all instances of B. There's no "management of base classes".
Classes declared as actions either via XML, annotations, or convention are instantiated by the Struts action instantiation mechanism. Extending ActionSupport has (almost) nothing to do with it, the only time it might have something to do with it is because ActionSupport implements the Action interface.
Also what makes an action class an action class to be managed by the S2 and instantiated per request? Getting declared in the struts.xml? Extending ActionSupport class?
Getting declared in the struts.xml: yes, is that that turns a Java class into an Action.
And every Action class is thread-safe because it's ThreadLocal, no matter what it extends nor implements. Every request of every user will have its own copy of each Action class.
P.S: The other classes (not declared in struts.xml) are not "(action classes) not managed by S2 and instantiated per request", they're simply NOT Actions.
I have a System.Web.Mvc.RazorView object which is strongly typed when in cshtml.
Can I get the model type from an instance of this class?
This is possible.
Call BuildManager.GetCompiledType(view.ViewPath) to get the type generated by compiling the view.
You can find the model type by checking the generic argument of the compiled type's base type (which should be WebViewPage<TModel>)
There's no way to get the model given only an instance of a System.Web.Mvc.RazorView. It's available inside the RenderView method which is passed a ViewContext but from the outside you can't access it. But if you are inside a view you could use the Model property.
Here's my scenario, I have 2 classes "com.project.ClassA" and "com.project.ClassB", I'm trying to map the fields ClassA.name to ClassB.person.nameObj.firstName.
As you can see, Dozer needs to create person object and nameObj before it can do the mapping, there is some factory classes to initiate the Person and NameObj objects.
I know we can define custom create method in field and class level, but that only applies to the field or the class that you are mapping. In my example, the mapping are between 2 String fields, but 2 objects are required to be created before the mapping is performed.
The error I'm getting now is
org.dozer.MappingException: java.lang.NoSuchMethodException: com.project.ClassB.Person.()
Any helps or ideas will greatly appreciated. Thanks.
Two things
1. ClassB needs a constructor which initialized obj Person, similarly Person needs a constructor to initialize nameobj.
2. accessor and mutator methods should be present (getters and setters).
After you do both these steps this should work.