Need your help with modules inheritance in Lua .
Let's say I've got 2 modules:
The 1st one is "Parent" It defines 1 field called "port" and method "connect" that uses port & domain fields to connect to some service. I wanna define the 2nd field (domain) in Child module, not in Parent one. Or at least to override this field by Child module.
module('Parent', package.seeall)
port = 1234
function connect()
ngx.say("connecting to "..domain..":"..port.."\n")
end
Note that "domain" variable is not defined here!
Now let's see the 2nd one, it's "Child":
local base = _G
module('Child', package.seeall)
local Parent = base.require('Parent')
base.setmetatable(Child, { __index = Parent })
domain = '127.0.0.1'
And here goes main lua code creating Child instance:
local Child = require "Child"
Child.connect()
The problem is that variable defined in Child module is invisible for the method defined in Parent module.. I need to change this behavior to let Parent routines code see variables defined in Child module.. Is that possible?
Can i copy Child's namespace to Parent module somehow?
I'm not particularly familiar with Lua modules, but it seems to me the right solution is to redefine the method as function connect(self) and then access port and domain off of self, which will be the package.
function connect(self)
ngx.say("connecting to "..self.domain..":"..self.port.."\n")
end
-- this could also be written as function Parent:connect()
...
local Child = require "Child"
Child:connect()
That's certainly how I'd do it if I were just setting up regular table inheritance without modules.
Related
I have 3 customs tags: my-tag1, my-tag2 and my-tag3
And I am using them like this:
<my-tag1>
<my-tag2 attr1="a">
<my-tag3 attr2="b"></my-tag3>
</my-tag2>
</my-tag1>
I am mounting all tags like this:
riot.mount('*', { store:reduxStore });
my-tag1 can access to store but my-tag2and my-tag3 can not do it.
However, if I do the following, my-tag2 can use store:
<my-tag1>
<my-tag2 attr1="a" store={opts.store}>
<my-tag3 attr2="b"></my-tag3>
</my-tag2>
</my-tag1>
Why? I have to do that in all my tags?
riot.mount('*') mounts all top-level tags. The top-level tags take care of mounting their sub-tags respectively
If you want to pass a store, I think it is best to use mixins. See http://riotjs.com/guide/#mixins
Cheers!
When tags are nested, a new context is created
for a child tag. In the new context, all the parent's properties
inherited are set to undefined.
References:
https://github.com/riot/riot/issues/1720
http://riotjs.com/guide/#context
In your example, by adding store={opts.store} in my-tag2, you are
defining an option store in my-tag2's new context. If you don't do
that, the option store in my-tag2's context is inherited from
my-tag1, but it is set to undefined (so my-tag2 sees undefined for
the store's value).
In order to share the store's value across my-tag1, my-tag2, and my-tag3,
there are two ways in my opinion:
1) my-tag3 initializes store's value to my-tag2's store
<my-tag1>
<my-tag2 attr1="a" store={opts.store}>
<my-tag3 attr2="b" store={opts.store}></my-tag3>
</my-tag2>
</my-tag1>
2) my-tag3 initializes store's value to my-tag1's store using the
parent variable.
<my-tag1>
<my-tag2 attr1="a" store={opts.store}>
<my-tag3 attr2="b" store={parent.opts.store}></my-tag3>
</my-tag2>
</my-tag1>
I want to create nodes programatically with its properties but using the folowing codes nodes can be created but its properties can not be set.
CreateUnspecifiedTypeRequest request_ch = new
CreateUnspecifiedTypeRequest(
Collections.singletonList(xxxElementTypes.yy),
diagramEditPart.getDiagramPreferencesHint());
Command command = diagramEditPart.getCommand(request);
command.execute();
then element.set("idof element") but the properties of the node still empty.
may someone help me .thanks
I am currently using this method in order to create nodes programatically. The node and the properties appear just fine and you can edit them. (note that there is also a way to edit the properties programmatically, with another type of command (EMF))
public void createAndExecuteShapeRequestCommand(IElementType type, EditPart parent) {
CreateViewRequest actionRequest = CreateViewRequestFactory
.getCreateShapeRequest(
type,
PreferencesHint.USE_DEFAULTS);
org.eclipse.gef.commands.Command command = parent.getCommand(actionRequest);
command.execute();
}
A sample caller of that method if the node is meant to be added in the main area of the diagram.
createAndExecuteShapeRequestCommand(xxx.diagram.providers.xxxElementTypes.ELEMENT_HERE, diagramEditPart);
A sample caller of that method if the node is meant to be added inside another node or compartment.
DiagramEditPart diagramEditPart = getDiagramEditPart(); //diagram.getDiagramEditPart();
"ParentElement" parentElement = (("Root_ELEMENT") diagramEditPart.resolveSemanticElement())."getTheElement"();
List list = getDiagramGraphicalViewer().findEditPartsForElement(EMFCoreUtil.
getProxyID(parentElement),
TheElementsEDITPART.class);
createAndExecuteShapeRequestCommand(xxx.diagram.providers.xxxElementTypes.ELEMENT_HERE, (EditPart)list.get(0));
Note that, if you wish to call this method from other class than the one of the xxxDiagramEditor.java you will need somehow to pass there the diagramEditPart.
In Linux when a new process is created, it inherits the normal_prio value of it's parent process for it's static_prio. Where does this actually happen??
Is it done in dup_task_struct() function or in copy_process() function??
It actually happens in sched_fork which is called by copy_process
The parent's priority is transferred into the child initially something like this
p->prio = current->normal_prio;
where p is child's task_struct and current points to parent.
And then normal_prio is modified like this
p->prio = p->normal_prio = __normal_prio(p);
__normal_prio(p) finally boils down to something like
return p->static_prio;
Check out the 2 links I've added to explore more.
I have a scenario where I have two Nhibernate SessionFactorys I need to register an use with StructureMap. Only Foo needs mySessionFactory sessions.
Like this:
For<ISessionFactory>().Singleton().Use(NHibernateConfiguration.GetDefaultSessionFactory());
For<ISession>().HybridHttpOrThreadLocalScoped().Use(x => x.GetInstance<ISessionFactory>().OpenSession());
For<ISessionFactory>().Singleton().Use(AnotherNHibernateConfiguration.GetDefaultSessionFactory).Named("mySessionFactory");
For<ISession>().HybridHttpOrThreadLocalScoped().Use(x => x.GetInstance<ISessionFactory>("mySessionFactory").OpenSession()).Named("mySession");
For<Foo>()
.Use<Foo>()
.Ctor<ISession>("session").Is(x => x.TheInstanceNamed("mySession"));
The problem is that mySessionFactory is now used everywhere when I only wanted to to be used in Foo and everywhere else should use my unnamed instance.
What I'm I doing wrong?
On both your named instances, change Use to Add. Use sets that instance as the default as well as adding it as an instance. You could also reverse the order of your config (the last instance of a type added with Use will become the default), but using the Add method is much more explicit.
I'm wondering what the best design would be for persisteing a new child entity with NHibernate without accidentally overwriting the parent in the database.
The problem I have is that the child entity will look something like this:
class Child
{
Parent Parent;
// other fields
}
My problem is that the child has been supplied from the UI layer along with the ID of the parent, and that means that the Parent ref is basically uninitialized: It will have the ID populated but everything else null - because the only way to populate its fields would be an extra round trip to the database to read them.
Now if I call Session.SaveOrUpdate(child) on NHibernate, what's going to happen with the parent. I don't want NHibernate to cascade save the uninitialized parent since that would just destroy the data in the database. How would people approach this problem? Any best practices?
You must use the session.Load(parentid) to get the aggregate root. In contrast to the session.Get() method, this does not actually fetch any data from the database, it just instantiates a Parent proxy object used to add Child objects to the correct Parent in the DB (eg. get the foreign key correctly).
Your code would probably look something like:
// Set the Parent to a nhibernate proxy of the Parent using the ParentId supplied from the UI
childFromUI.Parent = Session.Load<Parent>(childFromUI.Parent.Id);
Session.Save(childFromUI);
This article explains Get/Load and the nhibernate caches really well
You should probably be working with the aggregate root (probably the Parent) when doing Saves (or SaveOrUpdates etc).
Why not just:
Fetch the parent object using the parent id you have in the child from the UI layer
Add the child to the parents 'children' collection
I think you have to overview your mapping configuration for nhibernate. If you have defined on the reference by the child to the parent that hi has to Cascade all, it will update it!
So if you say Cascade.None he will do nothing. All other are bad ideas. Because you allready has the information of this parent. So why read from db agane?!
If your models looks like this
class Parent
{
}
class Child
{
Parent myParent;
}
and you are trying to set the parent and save the child without having a full parent object, just the ID.
You could try this:
session.Lock(child.myParent, LockMode.None);
before saving, this should tell nhibernate that there are no changes to the parent object to persist and it should only look at the object for the Id to persist the association between Parent and Child