How to parse settings object in ShellViewModel (Caliburn.Micro) - caliburn.micro

I have a Dictionary Object defined as below
Dictionary<string, object> dictArguments = new Dictionary<string, object>();
dictArguments.Add("CommandLine", strCommandLineArguments);
And then I am passing it ShellViewModel as below.
DisplayRootViewFor<ShellViewModel>(dictArguments);
Whereas I am at a loss to figure out how and where ShellViewModel parses this argument because as far as Caliburn is concerned ShellViewModel has a single CTOR with ieventAggregator. Any pointers please?
Thanks,
Deepak

The parameter for DisplayRootViewFor accepts Windows Settings as dictionary. So for example,
Dictionary<string, object> dictArguments = new Dictionary<string, object>();
dictArguments.Add("Height", 1000);
dictArguments.Add("Width", 1500);
dictArguments.Add("ShowInTaskbar", false);
dictArguments.Add("WindowStartupLocation", WindowStartupLocation.CenterScreen);
DisplayRootViewFor<ShellViewModel>(dictArguments);
These settings would influence the Height,Width,ShowInTaskbar and WindowStartupLocation properties of your View (Caliburn Micro does that for, you do not need to do it manually).
I do not think this is useful for the storing CommandLine argument.

Related

Can you programmatically switch between existing Session Contexts

I have multiple existing Stateful Session Beans. I want to use a new library/framework that instantiates objects (outside the container manager). This framework does not conceptually have 'sessions'. I'm trying to develop a proof of concept to route calling of beans in different session contexts based on a map of session identifiers to session contexts.
Below are 2 handlers that are instantiated by the library. The first handler creates new BoundSessionContexts and maps them to generated keys. The second handler is suppose to be passed the key and based on it propagate to the appropriate corresponding sessionContext, lookup the beans in that context, and call those beans.
public class Handler1 implements Handler1Interface{
//**1**
WeldManager weldManager = (WeldManager) CDI.current().getBeanManager();
BoundSessionContext boundSessionCtx;
//**2**
Map<String, Object> sessionMap = new HashMap<>();
public Handler1(){
}
//**3**
#Override
public long start(){
long sessionId = new Random().nextLong();
//**4**
boundSessionCtx = weldManager.instance().select(BoundSessionContext.class, BoundLiteral.INSTANCE).get();
//**5**
//boundSessionCtx.associate(sessionMap);
//Make certain the sessionId isn't already in use.
while(SessionMapper.get(sessionId)!=null){
sessionId = new Random().nextLong();
}
//**6**
SessionMapper.put(sessionId, boundSessionCtx);
return sessionId;
}
//**7**
#Override
public void stop(long sessionId){
SessionMapper.remove(sessionId);
}
}
public class Handler2 implements Handler1Interface{
//**8**
#Inject
EJB1 ejb1;
//**9**
#Inject
EJB2 ejb2;
BeanManager beanManager;
BoundSessionContext boundSessionCxt;
//**10**
Map<String, Object> sessionMap = new HashMap<>();
public Handler2(){
}
#Override
public Object process(long sessionId, Object input){
lookupEJBs(sessionId);
//**11**
ejb1.method();
Object result = ejb2.method();
return result;
}
//**12**
private void lookupEJBs(long sessionId) {
boundSessionCxt = SessionMapper.get(sessionId);
boundSessionCxt.associate(sessionMap);
boundSessionCxt.activate();
beanManager = CDI.current().getBeanManager();
//**13**
TypeLiteral<EJB1> typeLiteral = new TypeLiteral<EJB1>() {};
Set<Bean<?>> beans = beanManager.getBeans(typeLiteral.getType());
Bean<?> bean = beanManager.resolve(beans);
ejb1 = bean.create(boundSessionCxt);
//**14**
TypeLiteral<EJB2> typeLiteral2 = new TypeLiteral<EJB2>() {};
beans = beanManager.getBeans(typeLiteral2.getType());
bean = beanManager.resolve(beans);
ejb2 = bean.create(boundSessionCxt);
}
I've never been able to call ejb2.method(). While I have used EJBs for many years this is my first attempt at manipulating contexts. I am definitively feeling lost. It's one thing to use the magic, it's another to warp that magic to your whim.
Questions (In no particular order):
A) Is what I'm trying to do 'reasonably' acheivable? Or is this a pipe dream? NOTE: I am currently using WELD-API 3.0.SP4, but I am not married to it.
B) I have never truly understood the reason for the map(10) that is associated with a context(12).
B1) What is it's purpose?
B2) How does it's use affect what I'm trying to do here?
B3) Am I correct in that I would want to associate and activate the context inside the object where I want to use the context beans?
C) Am I correct that #Inject (8 and 9) is pointless as the handler2 object is not instantiated/Injected by the bean manager.
Many thanks to anyone who can help me understand EJB/CDI session contexts better. :)

Declare a Map<String, Object> as a scoped variable cause problem

When I declare a Map<String, Object> scoped variable in my SpringMVC project as below:
#Bean
#SessionScope
public Map<String, Object> allProjects() {
return new TreeMap<>();
}
It is weird that it contains many unexpected things even I didn't put anything into it. Just like it is the whole session scope. It will not happen if I declare it as Map<String, String>. Is there any formal statement in document talked about this?

How to post List of Dictionary

I have a client ( in this case POSTMAN) that is posting collection of object. The properties of the object are not known in advance so I cannot use concrete C# model.
So I am using Dictionary<string,object> that represent a single object where Key will be the property name and Value will be the value of the property. Since client is posting collection i am using List<Dictionary<string,object>>
ISSUE
In controller's action method each dictionary has Key however corresponding value is NULL
POSTMAN
Fiddler shows
model%5B0%5D.FirstName=foo&model%5B0%5D.LastName=bar&model%5B1%5D.FirstName=james&model%5B1%5D.LastName=smith
Quick watch in model:
I tried using JObject, ExpandoObject as model with no luck
I changed the model type from List<Dictionary<string, object>> to List<Dictionary<string, string>> and it worked
[HttpPost]
public IActionResult Update([FromForm]List<Dictionary<string, string>> model)
{
return Ok();
}

JacksonMapping Polymorphic

I am trying to parse json structure https://developers.nest.com/documentation/api-reference
Where device could be various types i want jackson to instantiate relevant objects Thermostat/SmokeAlarm,Camera etc
#Data
#ToString
public class Nest {
#JsonProperty("metadata")
private Metadata metadata;
#JsonProperty("structures")
private HashMap<String, Structure> structures;
#JsonProperty("devices")
private HashMap<String, HashMap<String, Device>> devices;
}
How would i use#JsonTypeinfo to decided which type to instatntite based on values in keys.
Another question would be how woul i get rid of all these multilevel nestings and cal have something like
#JsonProperty("devices")
private List<Device> devices;
parsed according to keys/subtypes

Use of Model object as a param in the request handling methods

I just want to know
Why do we use Model object as a Parameter to the request handling method in the Spring MVC application?
Basic Explanation helps me a lot.
ModelAndView (or Model) is a specialized spring object to store name value pairs (kind of java Map). It is optional to have the Model Object as a parameter to the request method. However in case if your request method has anything that needs to be passed on to the View; then you need a Model.
So Model is basically a data structure that carries information from the service layer to the view layer.
You can also initialize a Model inside your request method as:
public ModelAndView listCarrier() {
HashMap<String, Object> model = new HashMap<String, Object>();
model.put("isView", request.getParameter("isView"));
return model;
}
You can add attribute to Model object and use that attribute inside your JSP like below.
#RequestMapping(method = RequestMethod.GET)
public String login(String name, Model model) {
model.addAttribute("name", name);
return "xyz";
}
and later on you can access this property in your xyz.jsp like below.
Name: ${name}
For more info refer :-Model API docs

Resources