Nancy/TinyIoC multiple concrete classes for single interface - nancy

We have two auth methods for different modules – UserAuthModule and ServiceAuthModule. We’ve created 2 base classes that modules derive from. We’ve interfaced the AuthProviders into IAuthProvider. Then we have a dependency in the constructors that should get the correct AuthProvider injected. However, we can’t find a way to tell Nancy/TinyIoC which concrete class to use. Here is the pseudo-code:
abstract class UserAuthModule : NancyModule
{
public UserAuthModule(IAuthProvider authProvider) // should get the UserAuthProvider concrete class
}
abstract class ServiceAuthModule : NancyModule
{
public ServiceAuthModule(IAuthProvider authProvider) // should get the ServiceAuthProvider concrete class
}
Here's an example of one of the concrete module's class declaration:
public class AccountModule : UserAuthModule
We then get stuck: how do we register 2 concrete classes for the IAuthProvider interface? We could name them, but can’t figure out how Nancy knows which class to inject when it does the constructor injection.
Inside our bootstrapper we have:
Container.Register<IAuthProvider, UserAuthProvider>(“UserAuth”);
Container.Register<IAuthProvider, ServiceAuthProvider>(“ServiceAuth”);
We could resolve the type from the container, but there's not container access within the Nancy module.

Is creating a unique interface for each based off of IAuthProvider out of the question?
interface IUserAuthProvider : IAuthProvider { }
interface IServiceAuthProvider : IAuthProvider { }
And then register:
Container.Register<IUserAuthProvider, UserAuthProvider>();
Container.Register<IServiceAuthProvider, ServiceAuthProvider>();
And then modify the constructors:
public UserAuthModule(IUserAuthProvider authProvider)
public ServiceAuthModule(IServiceAuthProvider authProvider)

Related

Base Class vs Extended Classes

I'm a new learner, I'm practicing the base and child classes. My question is how do we decide which class should be instantiated, extended or the Baseclass?
Thanks in advance
package MavenProject2Package2;
import org.testng.annotations.Test;
import MavenProject2Package.JavaTesting;
public class JavaTesting2 extends JavaTesting
{
#Test
public void f1()
{
JavaTesting a1 = new JavaTesting();
System.out.println(a1.msg);
JavaTesting2 a2 = new JavaTesting2();
System.out.println(a2.msg);
}
}
Base class - it's a class which you should be extending from. - eg - superclass.
In superclass you may put some general fields and methods, which are used across your web app. For example, locators for header as well as footer items, because they are the same for all the pages (mostly).

Facing java.lang.IllegalAccessError for a base abstract class when trying to inject its implementation in wildfly 10

We are receiving the following exceptions in one of our code :
java.lang.IllegalAccessError: tried to access class base.BaseMessage from class message.beans.TerminalPowerCommandProducer$Proxy$_$$_WeldSubclass
Our class structure is as follows :
The Base Message creator class with default message properties
package messages.base
//... required imports
abstract class BaseMessage{ //some protected variables and methods }
the intermediate message class with extension to BaseMessage some additional properties for specific message types
package messages.base
//... required imports
public abstract class PowerMessage extends BaseMessage {//some more protected variables and Logger(using #Inject) and methods}
The actual implementation of the above abstract classes
package messages.beans
//... required imports
#Named
public class TerminalCommandMessage extends PowerMessage {// some more variables with injections and methods with abstract method implementation}
This class is now injected in some other classes :
package messages.beans
#Named
public class TerminalPowerCommandProducer {
#Inject
TerminalCommandMessage commandMessage
//some other code
}
We are receiving exception as reported above.
We are using WildFly version 10.1.0 Final with jdk 8
Is there an issue with the way we have consumed it?
Because if we mark the BaseMessage class as public it all works fine.

AngularJS: How to inject service to a static member of a regular class

I'm using typescript&angularJS for a project and I have a public class (not controller, just a regular class) to hold some enums, is that possible I can instantiate a static member of this class with a service object?
With Controller, it's very easy to use DI of AngularJS to instantiate a service object but for a regular class, I don't know how to do it.
Thanks.
Add code snippet:
export public Enums {
public static serviceObj; //how to instantiate serviceObj with some service?
}
Not exactly sure what you're asking but is this what you're looking for?
interface ServiceObj {
}
class Enums {
public static serviceObj : ServiceObj;
}
Enums.serviceObj = [set from some external location]

Use of Wrapper class for deserialization in callout?

I found the following use of a wrapper class, and was wondering if it is a good practice or whether its just duplication of code for no reason.
//Class:
public class SomeClass{
public Integer someInt;
public String someString;
}
//Callout Class:
public class CalloutClass{
public SomeClass someMethod(){
//...code to do a callout to an api
SomeClass someClassObj = (SomeClass)JSON.Deserialize(APIResponse.getBody(), SomeClass.class);
return someClassObj;
}
}
//Controller:
public class SomeController {
public SomeController(){
someClassObj = calloutClassObj.someMethod();
SomeWrapper wrapperObj = new SomeWrapper();
for(SomeClass iterObj : someClassObj){
wrapperObj.someWrapperInt = iterObj.someInt;
wrapperObj.someWrapperString = iterObj.someString;
}
}
public class someWrapper{
public Integer someWrapperInt{get;set;}
public String someWrapperString{get;set;}
}
}
The wrapper class "someWrapper" could be eliminated if we just use getters and setters ({get;set;}) in "SomeClass."
Could anyone explain if there could be a reason for following this procedure?
Thanks,
James
My assumption (because, code in controller is extra pseudo) is
SomeClass is a business entity, purpose of which is to store/work with business data. By work I mean using it's values to display it (using wrapper in controller), to calculate smth in other entities or build reports... Such kind of object should be as lightweight as possible. You usually iterate through them. You don't need any methods in such kind of objects. Exception is constructor with parameter(s). You might want to have SomeObject__c as parameter or someWrapper.
someWrapper is a entity to display business entity. As for wrapper classes in controllers. Imagine, that when you display entity on edit page and enter a value for someWrapperInt property, you want to update someWrapperString property (or you can just put validation there, for example, checking if it is really Integer). Usually, as for business entity, you don't want such kind of functionality. But when user create or edit it, you may want smth like this.

How to extend abstract Entity class in RIA Services

I want to add a bool variable and property to the base Entity class in my RIA services project so that it is available throughout all the entity objects but seem unable to work out how to do this. I know that adding properties to actual entities themselves is easy using .shared.cs and partial classes but adding such properties to the Entity class using similar methods doesn't work.
For example, the following code doesn't work
namespace System.ServiceModel.DomainServices.Client
{
public abstract partial class Entity
{
private bool auditRequired;
public bool AuditRequired
{
get { return auditRequired; }
set { auditRequired = value; }
}
}
}
All that happens is that the existing Entity class gets totally overriden rather than extending the Entity class.
How do I extend the base Entity class so that functionality is available thoughout all derived entity classes?
You won't be able to add a property to the Entity class. This class is already compiled and cannot be modified (partial classes only work because your have the source code of the class in your solution and the code can be merged at compile time).
One option may be to create a class that inherits from Entity, then add your property in this class, and have your entities inherit from your custom class instead of Entity. This might not be practical for use with designers, though.
public class MyEntityBase : Entity
{
private bool auditRequired;
public bool AuditRequired
{
get { return auditRequired; }
set { auditRequired = value; }
}
}
public class Entity1 : MyEntityBase
{
}

Resources