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/
Related
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.
I provide a highly customisable application to my clients which is working totally by itself. But If one my client wants to overwrite any Controller, I want to replace my implementation by theirs. However just overwriting the controller causes an ambiguous definition of mappings.
I have been using Component Scanning to load beans.
The potential solutions came to my mind are:
Using component scanner with excluding by a custom filter? (This seems not so easy)
Using a xxxxPostProcessor to remove some beans? (How?)
Any help?
If I got your Question properly,
You can differ implementation by changing URL to particular Implementation name
Say Telecom is interface and AirtelImpl and RelianceImpl are Controllers then
Your request mapping
#RequestMapping(value= "/airtel/doBilling")
#RequestMapping(value= "/reliance/doBilling")
In this way, Implementation flow will differ.
I have followed these steps:
Created a custom annotation: #Devoted
Created a custom ImportBeanDefinitionRegistrar. Iterated already registered bean definitions to find out `#Devoted #Controller's and removed them.
Based on a request I will provide implementation details.
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.
Is it possible to inject dependencies into an MVC ViewPage (must support layout pages) without using DependencyResolver?
I would rather not use DependencyResolver at all (I had major problems when injecting NH sessions into ActionFilters in the past (leaking all over the place)). However, I'm not sure if there is an alternative?
The other complexity I have is that the DependencyResolver needs to be tenant aware (each tenant has its own (StructureMap) container). I'm currently doing this by passing in a lazy instance of my tenant container resolver (seems this is necessary otherwise the resolver is cached):
public SmDependencyResolver(Func<ISiteContainerResolver> containerResolver)
{
this.containerResolver = containerResolver;
}
public object GetService(Type serviceType)
{
var container = containerResolver().Resolve();
If I end up using DependencyResolver should I ditch my StructureMap controller factory since it looks like DependencyResolver handles this too?
Thanks
Ben
Given that the DependencyResolver is used by so many aspects of the ASP.NET MVC framework for dependency injection your life will be easier if you use it - as you say it means you don't need your own versions of things like the controller factory.
That said, the framework is very flexible and it is always open for you to plug in your own version of things - I just prefer to create as little of my own code as possible on the KISS principle.
I am trying to put a "Contract" on a method call. My web application is in Spring 3.
Is writing customs Annotations the right way to go. If so, any pointers( I didn't find anything in spring reference docs).
Should I use tools like "Modern Jass", JML ...? Again any pointers will be useful.
Thanks
Using Spring EL and Spring security could get you most of the way. Spring security defines the #PreAuthorize annotation which is fired before method invocation and allows you to use Spring 3's new expression engine, such as:
#PreAuthorize("#customerId > 0")
public Customer getCustomer(int customerId) { .. }
or far more advanced rules like the following which ensures that the passed user does not have role ADMIN.
#PreAuthorize("#user.role != T(com.company.Role).ADMIN)")
public void saveUser(User user) { .. }
You can also provide default values for your contract with the #Value annotation
public Customer getCustomer(#Value("#{434}") int customerId) { .. }
You can even reference system properties in your value expressions.
Setting up Spring security for this purpose is not to hard as you can just create a UserDetailsService that grants some default role to all users. Alternatively you could make you own custom Spring aspect and then let this use the SpelExpressionParser to check method values.
if you don't mind writing some parts of your Java web application in Groovy (which is possible with Spring) I would suggest using GContracts.
GContracts is a Design by Contract (tm) library entirely written in Java - without any dependencies to other libraries - and has full support for class invariants, pre- and postconditions and inheritance of those assertions.
Contracts for Java which is based on Modern Jass is one way to write contracts.
http://code.google.com/p/cofoja/
As per the writing of this reply, this is pretty basic. Hopefully this will improve as we go on.
I didn't find an ideal solution to this, interestingly it is a planned feature for the Spring framework (2.0 implemented patch):
http://jira.springframework.org/browse/SPR-2698
The best thing I suggest to use JSR 303 which is for bean validation. AFAIK there are two implementations for this:
Agimatec Validations
Hibernate Validator
There's a guide here for integrating it into Spring, I haven't followed it through but it looks ok:
http://blog.jteam.nl/2009/08/04/bean-validation-integrating-jsr-303-with-spring/
I personally recommend C4J for 2 reasons:
It has Eclipse plugin so you don't need to manually configure it.
The documentation is written in a clear, structured format so you can easily use it.
Her's the link to C4J