I want to inject a string value into a property using Unity. I can't find a syntax that works. In this case, PutBunniesHere
In this case the error is it doesn't recognise "type" as a valid attribute of value. I added that because it couldn't resolve the type before.
The class has this property:
[Dependency("PutBunniesHere")]
public string PutBunniesHere { get; set; }
And this is the config I'm using for unity.
<?xml version="1.0" encoding="utf-8" ?>
<unity2 xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="IInjectMe1" type="CommonLib.IInjectMe1, CommonLib"/>
<alias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
<container name="unity2">
<register type="CommonLib.IInjectMe2, CommonLib" name="Injected2" mapTo="CommonLib.InjectMe2, CommonLib">
<lifetime type="singleton"/>
<constructor>
<param name="theDependency" dependencyType="IInjectMe1" />
</constructor>
<property name="PutBunniesHere">
<value value="my bunnies" type="System.String"/>
</property>
</register>
</container>
</unity2>
Ok, solved the problem. Once again it was a problem with having a name on the register element. This makes value on the value element take on a different meaning. value means name of a registered type, or a type if there is a name attribute on the parent register element. Take out the name and value means a value (and possibly also a type)
Yuk.
Related
I want to use unity to manage my mongo repository but when I try registering it using the Web.config and LoadConfiguration() I get an error I am unable to decipher:
An exception of type 'System.ArgumentException' occurred in
Microsoft.Practices.Unity.Configuration.dll but was not handled in
user code
Additional information: The container named "" is not defined in this
configuration section.
This is my Global.asax
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
namespace OOP_project
{
public class MvcApplication : System.Web.HttpApplication
{
internal static readonly IUnityContainer unity = new UnityContainer();
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
unity.LoadConfiguration();
}
}
}
and this is the relevant part of my Web.config
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
</configSections>
<unity>
<typeAliases>
<typeAlias alias="string" type="System.String, mscorlib" />
</typeAliases>
<containers>
<container name="container">
<types>
<type type="MongoRepository.MongoRepository`1, MongoRepository" mapTo="MongoRepository.MongoRepository`1, MongoRepository">
<lifetime type="Singleton" />
<constructor>
<param name="connectionString" parameterType="string" value="mongodb://127.0.0.1/Blog"></param>
</constructor>
</type>
</types>
</container>
</containers>
</unity>
I would like to understand what this error actually means and how to fix it.
I see two problems with your code:
First, you need to specify the container name when you call LoadConfiguration. In the configuration file, the container name is "container" (in the <container> xml element), so you need to specify that like this:
unity.LoadConfiguration("container");
Or alternatively, change the container name in the configuration file to an empty string like this:
<container name="">
The second problem which is not directly related to your question is that the singleton lifetime should be specified with a small letter like this:
<lifetime type="singleton" />
By the way, why do you want to use a configuration file to configure Unity? Configuring Unity with a configuration file is brittle. For example, if you change a class name in code, it will not change in the configuration file. You would have to change it manually or your application would break.
Unless you need to be able to change dependencies without recompilation, your should prefer to configure the container via code.
Please note that you could make some of the registrations through code and some in the configuration file (for the dependencies that you need to be able to change without recompilation). You could easily find a resource online of how to do that.
How can I write hashMap inside an fxml. I tried like this, but my IDE doesn't recognize the "entry" tag name. This must create a Map<String, Integer>
<FXCollections fx:factory="observableHashMap">
<entry>
<String fx:value="Vandaag" />
<Integer fx:value="1"/>
</entry>
<entry>
<String fx:value="Deze week" />
<Integer fx:value="7"/>
</entry>
<entry>
<String fx:value="Deze maand" />
<Integer fx:value="31"/>
</entry>
</FXCollections>
Defining as
<FXCollections fx:factory="observableHashMap">
<foo>123</foo>
<bar>456</bar>
</FXCollections>
works. However you loose the type safety and may not able to define a space separated key like
<kung foo>123</kung foo>
So defining the map in controller seems more appropriate.
There isn't much about maps in the introduction to FXML. However, you can do something like this: (in fxml)
<Group fx:id="mapWrapper">
<properties Vandaag="1" Deze_week="7" Deze_maand="31" />
</Group>
Then in your controller class you can have:
#FXML
private Group mapWrapper;
private ObservableMap map;
public void initialize() {
// the properties object is backed up by observable hash map
map = mapWrapper.getProperties();
// do something with the map
map.forEach((key, value) -> System.out.println(key + ":" + value));
}
prints the following:
Deze_maand:31
Vandaag:1
Deze_week:7
It probably isn't exactly what you wanted but it works
I'm having problems in getting my Dexterity content type to show a custom Add Form. I have already done this in a previous product, but, amazingly, I cannot accomplish this using Plone 4.1 and plone.app.dexterity 1.0.3
My CrmContact content type, living in package.name.types.contact.py, has its schema defined in this way:
from five import grok
from zope import schema
from zope.interface import implements
from plone.directives import form, dexterity
class ICrmContact(form.Schema):
"""A contact item for the CRM"""
title = schema.TextLine(
title=_(u"Company name"),
)
...
class CrmContact(dexterity.Container):
implements(ICrmContact)
class Add(dexterity.AddForm):
grok.context(ICrmContact)
grok.name('package.name.contacts.types.contact')
grok.template('add')
My template lives in package/name/types/contact_templates. It's a typical template. I know it's not being rendered because it has a dummy node that will call a non existing method using tal:content, in order to raise an exception; so I'm sure the template itself is not the issue.
My content type FTI is registered correctly during installation, and the content type is available and addable.
Finally, in profiles/default/types.package.name.types.contact.xml:
<?xml version="1.0"?>
<object name="package.name.types.contact" meta_type="Dexterity FTI"
i18n:domain="package.name" xmlns:i18n="http://xml.zope.org/namespaces/i18n">
...
<!-- Method aliases -->
<alias from="(Default)" to="(dynamic view)" />
<alias from="edit" to="##edit" />
<alias from="sharing" to="##sharing" />
<alias from="view" to="(selected layout)" />
<!-- Actions -->
<action title="View" action_id="view" category="object"
condition_expr="" url_expr="string:${object_url}" visible="True">
<permission value="View" />
</action>
<action title="Edit" action_id="edit" category="object"
condition_expr="" url_expr="string:${object_url}/edit" visible="True">
<permission value="Modify portal content" />
</action>
</object>
Unrelated, but maybe I have to add something here...
I think I followed the correct procedure, as you may see, but I still cannot get it to work.
I know class Add is getting instanced because if I provide an updateWidgets() method and insert a breakpoint, it gets called; and when I introspect the object, self.template is None; even though:
(Pdb) getattr(self, 'grokcore.view.directive.template')
'add'
How can I provide a custom template to the Add Form of my custom type?
You should remove the line grok.context(ICrmContact).
From http://plone.org/products/dexterity/documentation/manual/developer-manual/advanced/forms:
Also note that we do not specify a context here. Add forms are always registered for any IFolderish context.
Firstly I would like to say that I am quite new to Spring (in particular the MVC framework), and just trying to understand how everything works so please go easy on me.
I'm playing around with a dummy application that I've created, and I've created a simple login form that users can access via the /login.html bean. The bean definition is as follows:
<bean name="/login.html" class="test.controller.LoginController">
<property name="successView" value="list_messages.html" />
<property name="commandClass" value="test.domain.Login" />
<property name="commandName" value="login" />
</bean>
(the Login class is a simple object containing a username and password field with appropriate getters and setters).
The LoginController class does virtually nothing for now:
public class LoginController extends SimpleFormController
{
#Override
protected ModelAndView onSubmit(Object command, BindException errors) throws Exception
{
return new ModelAndView(new RedirectView(getSuccessView()));
}
}
Now I have one view resolver in my bean definition file, which goes as follows:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
To support my Login form I have a login.jsp file in my jsp directory.
My question is as follows: why does accessing /login.html redirect me to login.jsp? I have not specified a formView property for my form, so how does the view resolver know to redirect me to login.jsp?
Thanks in advance for any help!
Joseph.
When you do not specify The logical view name, Spring relies on DefaultRequestToViewNameTranslator, which is installed by default. So if your request is something like
http://127.0.0.1:8080/app/<LOGICAL_NAME_EXTRACTED_BY_VIEW_NAME_TRANSLATOR_GOES_HERE>.html
Have you seen <LOGICAL_NAME_EXTRACTED_BY_VIEW_NAME_TRANSLATOR> ??? So if your request is
http://127.0.0.1:8080/app/login.html
The logical name extracted by ViewNameTranslator is login which is supplied To viewResolver and Translated To
/jsp/login.jsp
Nothing else
I am trying to build a messaging system and for this i have the table definition below
Message
Id
From
To
Body
ParentId // Subcollection, i want to get Asnwers (Message.ParentId== Message.Id)
IsRead
and i have this in the Message.cs
IList<Message> Answers;
I have tried this but it gives me all the messages and all the answers in the main collection.
But i dont want answers to be seen like a message (like the main item).
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="RealEstate.Core" namespace="RealEstate.Core.Domain">
<class name="Message" table="Message" lazy="true">
<id column="id" type="Int64" name="Id">
<generator class="native" />
</id>
<property name="From" column="[From]" type="Int64" />
<property name="To" column="[To]" type="Int64" />
<property name="Body" column="Body" />
<property name="ParentId" column="ParentId" type="Int64" />
<property name="SenderType" column="SenderType" type="Byte" />
<property name="IsRead" column="IsRead" type="Boolean" />
<bag name="Answers" lazy="true" cascade="delete">
<key column="ParentId" />
<one-to-many class="Message"/>
</bag>
</class>
</hibernate-mapping>
How can this mapping be done, they are in the same table ?
Thank you very much
Before attempting an answer, I'd strongly recommend that you search the NHibernate Users Group as there are tons of helpful NHibernate folks that lurk there answering all kinds of questions.
But let me see if I can help here.
Hmmm, I'm not totally sure I understand the question. You say:
I have tried this but it gives me all
the messages and all the answers in
the main collection.
But i dont want answers to be seen
like a message (like the main item).
Do you mean that the Answers collection contains all answers in the database?
Can you post up more code, showing the query your running, and the class code?
One potential problem you have with your scenario is that ParentId can be NULL in the database. This gives NHibernate problems when mapping a one-to-many.
Try making the association bidirectional (documentation reference). That sometimes helps avoid a few traps.
To do that, add this to your class
public class Message {
///<summary>Reference to parent message</summary>
public Message Parent {get;set;}
//... rest of class
Add this to your mapping:
<bag name="Answers" lazy="true" cascade="delete" inverse="true">
<key column="ParentId" />
<one-to-many class="Message"/>
</bag>
<many-to-one name="Parent"/>
The inverse=true will make NHibernate manage the relationship from the Parent property, not the collection. This is necessary because ParentId can be null.
In your code, rather than using myMessage.Answers.Add( blah ); you can use answer.Parent = myMessage. Of course, you can write nice helper methods to make this more meaningful.
someMessage.AddAnswer( someAnswer );
Which looks like this:
public void AddAnswer(Message answer)
{
answer.Parent = this;
if( ! this.Answers.Contains(answer) )
this.Answers.Add(answer);
}
Hope this helps.
You want to map a tree ?
Maybe this could help:
how to map a tree in nhibernate