I am trying to figure out the whole Mvvm way of declaring singletons and especially ones that have IoC dependencies tied to them. In my particular example I have a UserService that requires an IDatabase and an IMvxMessenger as an IoC dependency.
The IDatabase is platform specific so In my Droid.UI project I have resolved this using the following code:
var database = new Database(new SQLitePlatformAndroid(), Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "database.db"));
database.Initialize();
Mvx.RegisterSingleton<IDatabase>(database);
In my .Core project I have the following code:
var database = Mvx.Resolve<IDatabase>();
var messenger = Mvx.Resolve<IMvxMessenger>();
Mvx.RegisterSingleton<IUserService>(new UserService(database, messenger));
I am trying to wrap my head around this. What really is the Mvx.Resolve's job and how it works along with the resolving other dependencies. I have already tested the code above and it doesn't seem to be working so I know I am missing something important here.
Any help on this will be much appreciated, thank you in advance!
Follow this link to the Service Location and Inversion of Control page for MvvmCross and look at Constructor Injection and Lazy Singleton.
Instead of resolving the dependency when you register the next, have it be resolved when the UserService instance gets initialized.
Related
Does anyone have an example of the new abstract method CreateContainerExtension that is in Prism.Wpf 7.1.0.172-pre? We are using the common service locator and have essentially bypassed IOC in Prism because we need to resolve things before the Bootstrapper has run.
You should remember that Prism is open source and the source code is in itself a form of documentation.
If you're using the classic Bootstrapper, you'll notice that it has been deprecated in favor of PrismApplication. Since your question is extremely vague as to what container you're even trying to use, it's impossible to tell exactly which Container Extension to use, but I will provide an example using Unity for your reference.
Whether you look at the UnityBootstraper or the Unity PrismApplication, you'll see that it simply returns an instance of the UnityContainerExtension.
protected override IContainerExtension CreateContainerExtension()
{
return new UnityContainerExtension();
}
I'm using springFramework and I try to setBeanDefinitions, the problem is that this methode need a Map beanDefinition as a param... could U tell me plz how I could instantiate this param?
NullPointerException at com.liferay.portal.spring.util.SpringFactoryImpl.setBeanDefinitions(SpringFactoryImpl.java:56)
additional information:
I try to deploy a liferay project without using liferay configuration files (only springFramework libraries), I created my own sessionFactory, my own dataSource ... etc!!
when I run the program, I'm able to create dataBase Schema basing on portlet-hbm.xml information... well now I try to instantiate beans for portal-spring.xml.. (which are xxxxpersistance.java)! those latters told me that they use 'com.liferay.portal.kernel.dao.orm.SessionFactory' as a required type and it can not convert property value of type 'org.hibernate.impl.SessionFactoryImpl'!! so I tried to use the liferay libraries only for those beans and I try to instanciate them manually... but I wasn't able to setBeanDefinitions cause I need a Map beanDefinition as a param... I don't know if there is a way to get them using sessionFactory or not!!
Thanks again
You only mention junit in the tags to your question. I'd recommend to write unit tests without relying on the whole Liferay infrastructure. That will tremendously lower your required setup efforts and simplify your life a lot.
all. I am getting started with NServiceBus and have a pretty good handle on the basics thanks to Pluralsight and the internet. I have an stock MVC 4 project and I have setup dependency injection for my controllers (thanks to this blog post).
Here is how I have my bus setup in Global.asax:
_bus = Configure.With()
.DefaultBuilder()
.ForMVC()
.Log4Net()
.XmlSerializer()
.MsmqTransport()
.UnicastBus()
.SendOnly();
I am assigning it to a local private variable because I need access to the bus in Global so I can do some stuff on Session_End. However, when I run, I get the following error:
The requested service 'System.Web.Mvc.IControllerFactory' has not been
registered. To avoid this exception, either register a component to
provide the service, check for service registration using
IsRegistered(), or use the ResolveOptional() method to resolve an
optional dependency.
According to my stack trace, the failure point is when Autofac tries to resolve the type. For the sake of sanity, I removed the private variable and used just the Configure statement, same thing. I also have Ninject wired up in this app because that is my IoC of choice. Thinking that it was interfering with Autofac in some way, I removed Ninject from the equation, still not working.
So my question is, what am I doing wrong? Am I missing something? This is my first time with NServiceBus, but from everything I've seen, this should just work. Any info would be super helpful. Thanks.
Have a look at our MVC4 sample (this is running against v4, the next major release):
https://github.com/NServiceBus/NServiceBus/tree/develop/Samples/Messaging.Msmq/MyWebClient
I found the solution from your code, John! Here was my issue. This is what I had in my Dependency Resolver Adapter:
public object GetService(Type serviceType)
{
_builder.Build(serviceType);
}
What I needed to do was what you did in your MVC4 example:
public object GetService(Type serviceType)
{
return (Configure.Instance.Configurer.HasComponent(serviceType)) ? _builder.Build(serviceType) : null;
}
Once I did that, it all sorted itself out. Thanks again for the help!
I've seen articles online about how to implement them, but I still don't understand exactly what they do, and if I need one in my application. My application will have a HostApp that has many plugins with controllers and views in them. Will I need a custom DepedencyResolver in my app?
If you have any questions please let me know! Thanks!
No, you don't 'need' one. If you want to use dependency injection, MVC allows you to hook up a resolver so you can inject dependencies at various points, most typically is injecting some dependency into your controller.
public class CustomerController
{
public CustomerController(ICustomerRepository repository)
{
}
}
It's up to you to use it or not if your application calls for it. This depends on how you have your layers and dependencies setup, but you dont 'need' it, but it can help in many cases.
Check out Dependency Injection in .Net by Mark Seeman for the best ref on the subject. Many questions could be asked about your implementation since you may have plugins and optional dependencies which is a bit out of the scope of the question but feel free to post more : )
http://manning.com/seemann/
So, I'm new to using Castle Windsor and I'm struggling with how ugly my Controllers are becoming. I've got IOC working in my project which seems to be at least half the problem for most people. Now I'm finding that I'm declaring a ton of dependencies in my controller constructors as below. Are there any good patterns for managing these so I'm not copying/pasting this into each new controller and/or section of the site I create?
public HomeController(ILocalizationService localizationService, // ugly
INewsService newService,
IAnswerService answerService,
ITwitterFeedService twitterService,
IFacebookService facebookService,
ISettingsService settingsService,
IExternalDataService externalDataService,
IUserService userService,
IInstantMessageService instantMessageService,
ICalendarService calendarService,
ILogger logger)
{
// do some stuff to link these up
}
Hope this makes sense. I can add more details if necessary to clarify.
It seems as though your controllers are doing too much. Try to make controllers more specialised, so with the exception of really common stuff like ILogger they don't need too many dependencies.
Review the action methods on the controllers, and see which ones seem to have similar behaviour and dependencies - they're candidates for moving to their own controller.
It seems you have a number of services that are basically doing similar things, such as facebook and twitter. Why not create an ISharingService that handles all your social networking stuff in a single interface?
Then you have an IUserService, which I take to be a repository of some type? If so, you might make better use of an Unit Of Work pattern that would condense all data repositories into one interface.