Where to set SetDefaultViewModelFactory in Prism 7.2 - prism

Where in App.xaml.cs should one make a call to ViewModelLocationProvider.SetDefaultViewModelFactory?
ViewModelLocationProvider.SetDefaultViewModelFactory((type) =>
{
return Container.Resolve(type);
});
Should it be done in ConfigureViewModelLocator or somewhere else? In ConfigureViewModelLocator the Container is still null.
Is it best to use a container as the view model factory or use the default behavior of Activator.CreateInstance?

Should it be done in ConfigureViewModelLocator?
Yes.
In ConfigureViewModelLocator the Container is still null.
That does not matter, the configured factory is not called until the first view model is resolved which will be definitely after initializing the container...
Is it best to use a container as the view model factory?
Sure, otherwise your view models will be unable to receive dependencies.

Related

Copy Metadata from Entities to ViewModels in Automapper 5

In my MVC application I use Autofac IoC container together with Automapper. It gives me an opportunity to implement custom ViewModels.
The main question: How can I copy Metadata from Entities to ViewModels using Automapper 5.1.1?
I tried to implement Bettys solution, but I can't correctly register new providers using this approach.
Code is also presented below:
ModelMetadataProviders.Current = new MetadataProvider(
AutoMapper.Mapper.Engine.ConfigurationProvider);
ModelValidatorProviders.Providers.Add(new ValidatorProvider(
AutoMapper.Mapper.Engine.ConfigurationProvider);
or
ModelMetadataProviders.Current = new MetadataProvider(
(AutoMapper.IConfigurationProvider)AutoMapper.Mapper.Configuration);
ModelValidatorProviders.Providers.Add(new ValidatorProvider(
(AutoMapper.IConfigurationProvider)AutoMapper.Mapper.Configuration));
There is no property like Mapper.Engine.
When I implement a second approach, I got invalid operation exception:
Additional information: Mapper not initialized. Call Initialize with
appropriate configuration. If you are trying to use mapper instances
through a container or otherwise, make sure you do not have any calls
to the static Mapper.Map methods, and if you're using ProjectTo or
UseAsDataSource extension methods, make sure you pass in the
appropriate IConfigurationProvider instance.
So, how should I initialize AutoMapper.Mapper.Configuration?
Thank you in advance for your time and help.
Or maybe there is another, better way to map metadata with viewmodels?

Application Init Handler Equivalent in Angular?

I would like to "simulate" global variables in Angular. I would also like to be able to initialize those "global variables" in an App_Init()-type of handler. Such an initialization will require $http calls to populate those variables. I want all of the "global variables" to be completely loaded before Angular "calls up" controllers and views, because controllers would depend on the initialized data.
What are some best practices for that in Angular? Nested controllers? Services?
Example: An app that manages items in a restaurant's menu. Each item will be associated with a category (beverage, appetizer, dessert, etc.). I need to load all of those categories first before Angular "even touches" the controllers and views for the food items.
Are you using ng-view and the $routeProvider service for your app? Assuming you are or will consider, here is what you can do in order of steps:
Build a service that provides acces to the categories to its called. My idea is that this service would load the categories from the server when it's first called and then cache the data loaded, so the next time it's called a cache copy is served, instead, to save a request to the server. Let's call this service categories for now.
Use the resolve property of route object to ensure the dependency on the categories service is resolved before loading the view the user is requesting (and the corresponding controller). As a result, you can inject the categories service into the controller and be sure the service is always available because it has already been resolved.
If you have never worked with the resolve property in configuring routing before, Here is an example and the part of the official docs that talks about it. I recommend you go through them.
Also, in order to understand how resolve works, you need to be familiar with the concept of promise and deferred. If you are not, here is a good starting point on the topic. $q is AngularJS's implementation of promise/deferred.
I can't comment on if the above approach is the best practice, but I know it's a good practice to use service to provide access to some data/functions like if there were global variables.
You can create a service to load the configuration from server and put it in rootScope, in your controller you can call this service on your function and this function you call from ng-init in your view.
View
<mytag ng-init="myFunc()">
Controller
module.controller('MyCtrl', function($scope, myService){
$scope.myFunc = function(){
myService();
}
}
your service (initialize app)
module.service('myService', function($http, $rootScope){
//do something
$rootScope.config = configLoaded;
}
another tip
if you are in trouble with asynchronous calls you can try to use
var deferred = $q.defer();
//when your call come back
deferred.resolve(yourData);
//and in the last line of function
deferred.promise;

Castle Windsor Property Injection of IWindsorContainer

I am having problems injecting IWindsorContainer as a property.
I am using MVC3. I have created my own IView and ViewEngine. From within the View I dynamically build views based on the types registered in Windsor.
For one condition ( a "list" view) I want to display a list of all the IMyTypes registered with Windsor.
In my global.asax I register the Windsor view like this:
container.Register(Component.For<IWindsorContainer>().Instance(container));
Then in my IView implementation, I declare a property like this:
public IWindsorContainer Container { get; set; }
The actual IView is in another component. When I get to the Render method, I want to do this:
IRuleDataDefinition[] ruleDatas = Container.ResolveAll<IRuleDataDefinition>();
But "Container" is always null. Is it because of the way I am creating the IView (I am just using new, it isn't registered with Windsor?) Is it something with the IWindsorContainer itself? Or do I just have everything wrong?
I've also read other people that say "if you are using Container.Resolve you are probably doing it wrong." So if I am doing this wrong, please let me know.
EDIT
Perhaps a better way to phrase the question is: What is the best way to do the equivalent of container.ResolveAll() when you don't have a reference to the container? I need to loop through all registered versions of IMyType.
EDIT THE 2ND
I got it to work by using Windsor for the entire dependency chain, which, of course, is what you are supposed to do, I learned.
You should not have a dependency on the Container... that's an anti-pattern and it smells like a service locator.
What about a typed factory instead? Your typed factory may return a list of given components based on a common interface.

Castle.Windsor, ASP.NET MVC, Handling Null Resolution of Injection

Using Castle.Windsor in ASP.NET MVC (3.0) is there any way I can appropriately handle if one of my dependencies resolves null? For instance, say I have a IMembershipService.
class ServiceInstaller
{
// ...
void Install( // .. )
{
container.Register(
Component
.For<IMembershipService>()
.ImplementedBy<MembershipService>()
.LifeStyle.PerWebRequest
);
}
}
Okay, this works great. Now, perhaps not all of my site requires a user to be logged in. Let's assume that maybe my web host's database server crashes for a few hours. In that event, things that looked into the database, or tried to call on my ISession might return null.
What can I do in this case? I can write if(membershipService == null) a hundred times over, but that seems pretty dumb. Is there a built-in solution to say "Hey, if we have an error, do this..?"
I think that the service should never be null. If the database is down, the service should be returned nevertheless, but its methods should throw an exception, return null or some default value, depending on the semantic of the service.
Ciel, i had this problem just recently and found your question while looking for the answer.
Basically you should be using a typed factory to resolve your components at runtime in your wrapping component. The factory should return a default object if there is no match regarding the component you're looking for, default object that would implement whatever behavior is needed.
In the case of your IMembershipService, implement a NotCheckingMembershipService class inheriting the interface and doing nothing and make it the default for the components that won't need it. More specific membership services can be linked to specific controllers.
To do so you must create a generic "catch-all" implementation
public class NotCheckingMembershipService<T>: IMembershipService<T> where T: Controller
{
}
And register it as the default component for an open IMembershipService
_container.Register(
Component.For(typeof(IMembershipService<>))
.ImplementedBy(typeof(NotCheckingMembershipService<>))
.IsDefault());
Then simply register your custom membership services where needed. Resolution won't fail and you will always be able to call the interface.

CodeIgniter: Lifecycle of the Controllers

The text is quite long, the question is actually simple and written in bold. The other text is only for trying to explain the problem.
I have recently started to use CodeIgniter for developing my website. Currently, I'm writing a side menu where you can filter the items showed in the view (basic e-commerce functionality).
The idea I had was to have an array in my Shop-controller where I store my filtering values which are passed to and from my filterview, so the filter menu kan handle several types of filters.
The problem is that it seems like the constructor of the controller is run each time the controller is called. I thought that the controller was constructed only once when the user enters the website. I'm pretty new at website devlopment and am mainly a C++-guy, so this seems a bit strange for me.
My question is pretty simple actually: Is it true that the controller instance is created at each call to a controller function? Otherwise, what am I doing wrong to cause the controller instance to be reinstantiated at each controller call...
I hope that my question is not too fuzzy. It is important for me to understand the lifecycle behaviour of CodeIgniter to be able to find a simple solution for this. I would like to avoid using $_SESSION, because I would like to use a OOP like solution.
Is it true that the controller
instance is created at each call to a
controller function? Otherwise, what
am I doing wrong to cause the
controller instance to be
reinstantiated at each controller
call..
Yes, the controller instance is re-instantiated every time you make a call to that controller. In CodeIgniter, there is no such thing as a persistent instance of your controller for each user interacting with your application (unlike single-user apps built in C++). What you do have is session management, where you use Session variables to store data persistent to that particular session between the user's browser and your web server (more). Another way to do this is by using cookies. I personally prefer session over cookies.
There is no harm in using $_SESSION for the purpose of your filter, and it is not against OOP principles. What matters is how you use the data stored in your Session variable once your controller instance has loaded ( that's where the OOP concepts come into play).
Shivaas, the same answer can by safely used with models? What about autoloading models? I mean, autoloading it's just a way to avoid typing $this->load->model('the_model') when needed? There's no way to use singleton pattern without recurring to use session/database?
private $instance;
function init_model() {
if ($this->instance === NULL) {
$this->instance = array();
}
return $this->instance;
}
Once init_model exits, the class instance is destroyed so $instance will be always initialized to a new array()?

Resources