How do I make a public view model base with WinRT classes? - visual-c++

I wanted to create an abstract base class for all of my view models, but I'm running into accessibility issues and can't seem to navigate through the errors. Here's an example of my code:
public ref class ViewModelBase {
...
}
public ref class OtherViewModel : ViewModelBase {
...
}
When I define my code as state above, I get the following error:
error C4585: 'MyNamespace::ViewModelBase' : A WinRT 'public ref class' must either be sealed or derive from an existing unsealed class
If, instead, I make ViewModelBase private, I get this error:
error C3213: base class 'MyNamespace::ViewModelBase' is less accessible than 'OtherViewModel'
This seems like it should be incredibly simple. What am I doing wrong?

What you are attempting is not strictly possible in C++/CX, as in VS 2012 C++/CX does not support public class inheritance for general-purpose scenarios. It is only supported enough to have the XAML scenarios work; the only possible public unsealed types are those found under the Windows::UI::Xaml namespace, and users are not able to author their own public unsealed types.
If you want to have a base class for all your view models, your only options to make all your classes private or to inherit from a base class from Windows::UI::Xaml (such as DependencyObject).
Each of these approaches has drawbacks:
using DependencyObject as your base makes all your viewmodels STA objects (so they can only be manipulated from the UI thread) in addition to having a lot of overhead that isn't really necessary.
Making your classes private means you cannot use the [Bindable] attribute to do databinding, so you would need to have a private databinding implementation. (Your private class would need to implement ICustomPropertyProvider and related interfaces.)

The "sealed" keyword means that the class cannot be inherited from. Your implementation should look something like this:
ref class ViewModelBase sealed
If you want to inherit from ViewModelBase then you need to have only the subclasses be ref classes like this:
class ViewModelBase
...
public ref class OtherViewModel sealed : ViewModelBase

Related

Inheritance or Injecting in Typescript

I create an large project with using Typescript and NestJS framework, I want to create this project in OOP architecture and I have a want important question about it. I want to create one, base service with commons function that will be use in almost every service in the project, and I have a two ways to using a common parts.
First is to create an abstract class with commons methods like below:
export abstract class BaseService {}
and inheritance this abstract class in the target service:
export class UserService extends BaseService {}
Thanks of this I can use all of the methods that exists in BaseService with using this keyword and creating an reference to the function.
But I have a second approach, with using Injecting service via constrcutor:
export class BaseService {}
and injecting via constructor:
export class UserService {
constructor (
private readonly baseService: BaseService
){}
someMethod(): void {
return baseService.getHelloWorld();
}
}
In this way I can to create an reference stricte from injecting service.
And now i have a question. Which approach is better in the large project?
PS. I prefer to create an generic function based on <T> on methods instead of class
In this particular case delegation to an injected service is more appropriate. If you do this, you should probably rename BaseService to something that does not imply that it is a base class to be inherited by other service classes. I suggest SharedService or CommonService.
Delegation is better here because your design suggests that the BaseService and UserService do not have the same meaning and purpose but only have common operations. Reducing code duplication should not be the main justification for choosing inheritance.

How to dynamic modify super classes of a class using asm or javassist

How to dynamic modify a class's super classes to point to another class as its superclass by asm or javassist?
I know how to modify a class's methods and fields.
You should take a look at setSuperclass. Here is a small example:
CtClass c = pool.getCtClass("com.my.class.A");
c.setSuperclass(pool.getCtClass("com.my.class.B"));
Here is the documentation for this method:
public void setSuperclass​(CtClass clazz)
throws CannotCompileException
Changes a super class unless this object represents an interface. The new super class must
be compatible with the old one; for example, it should inherit from
the old super class.
If this object represents an interface, this
method is equivalent to addInterface(); it appends clazz to the list
of the super interfaces extended by that interface. Note that an
interface can extend multiple super interfaces.

Adding attributes to parent class fields and properties

In my example to illustrate my use case I have a parent class that is purposely database agnostic (let's say I can't change the code of it for some reasons, because the class come from a commercial assembly or the .net framework or are auto generated by entity framework):
public class Father
{
public string Field1;
public string Field2;
}
Now I'd like to store an object derived from it into MongoDB (again, it's only for the example, there a lot of other use cases and my question has nothing to do with MongoDB):
public class Child:Father
{
public ObjectId Id;
public DateTime DateCreation;
}
But I'd like to add attributes to some elements of the father, like [BsonIgnoreIfNull], without overriding (they are not marked as virtual) or having to fully reimplement the Father in my Child class.
What would be the cleanest way to do this?
Thanks!

Abstract Class Error in Java

I'm new to Java and I'm running into a compile error I cannot figure out.
Chapter5Debug is not abstract and does not override abstract method itemStateChanged(java.awt.event.ItemEvent) in java.awt.event.ItemListener
public class Chapter5Debug extends Frame implements ItemListener
^
Can anyone help me understand what I need to do to fix this?
Appreciate the help!
Sheila
You have to remember that if ItemListener is abstract, then you will need to implement all the methods inside ItemListener. If you want to keep your current structure, you can just add an empty itemStateChanged method to your Chapter5Debug class.
To elaborate on #kevolution's answer:
public class Chapter5Debug extends Frame implements ItemListener {
public void itemStateChanged(ItemEvent e) {
// Write your method here
}
}
An abstract class is one which is just like a regular class but can contain some 'stub' - or abstract - methods. These are methods which need to be implemented by the class extending the abstract class. In this case, itemStateChanged() is marked as abstract, meaning that you need to declare it.
Abstract classes are useful for when you're writing code which you need the caller to provide guts in some ways. In this case, Java cannot know what to do when the item's state changes, but it can do a whole lot of other stuff. The other stuff is in regular methods in the Frame class, and these call the itemStateChanged() method - which will then invoke your own handling code.
You need o implement itemStateChanged(ItemEvent) within Chapter5Debug
example code
public class Chapter5Debug extends Frame implements ItemListener{
//where initialization occurs
checkbox.addItemListener(this);
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
label.setVisible(true);
...
} else {
label.setVisible(false);
}
}
}
ItemListener is an interface and hence implementing ItemListener means you either you will have to provide the implementation in Chapter5Debug
or
You can make Chapter5Debug abstract and then provide the implementation in the class inheriting Chapter5Debug.
Crux is that if you implementing an interface, You can't get away by not providing the implementation. Either you have to provide it there itself or carry it to the child classes.

How to use Rhino.Mocks to evaluate class Properties (getters and setters)

I'm studying how Rhino.Mocks works and trying to understand how can I set manually a value in a class Property.
I have seen a sample in internet where you have only desired Property as argument of Expect.Call(), instead of using a method.
MockRepository mocks = new MockRepository();
Person p = mocks.StrictMock<Person>();
Expect.Call(p.FirstName).Return("John");
Person is a class such as:
public class Person
{
public string FirstName {get;set;}
}
I always receive the error:
Invalid call, the last call has been
used or no call has been made (make
sure that you are calling a virtual
(C#) / Overridable (VB) method).
Am I missing something? Is it possible to set manually class Properties and evaluate them to see if getters and setters are working fine?
As with any mocking framework, Rhino Mocks can only mock interfaces or classes that defines virtual methods and properties.
That's because when implementing a class, Rhino creates a derived class from the one you specify, replacing every virtual (or Overridable in VB) method with a stub implementation that uses an interceptor to handle the call.
When you specify a non virtual method, Rhino can't create a wrapper.
That is also true tor sealed (NonInheritable in VB) classes.
So for your class to work, you should implement the property as such:
public class Person
{
public virtual string FirstName { get; set; }
}
This way Rhino can override the poperty accordingly.

Resources