I seem to have a problem - rather unexpected; so I guess I might be doing something silly/wrong.
I register two container scoped services as such:
builder.Register<IServiceInfo>(c => CreateServiceInfo(c)).As<IServiceInfo>();
builder.Register<IServiceInfo>(c => CreateServiceInfoSomeOther(c)).As<IServiceInfo>().Named("someOther");
Now when I try to resolve
container.Resolve<IServiceInfo>()
and container.Resolve<IServiceInfo>("someOther")
I get the same instance. I would expect the first call to return the first instance and the second call to return the second instance. Why is this happening this way?
I am hoping there are some active Autofac experts around here and I'd appreciate any help to quickly fix this.
Thanks, all!
When two services are registered, the last one wins (it doesn't matter that the second is named, it's still a registration for that service), unless you ask for an instance by name.
You can use ".DefaultOnly()" to modify your second registration so that the first is the actual default. See the wiki page for more info.
Related
I've been reading the documentation of existingObject(with:), and it states:
If there is a managed object with the given ID already registered in the context, that object is returned directly; otherwise the corresponding object is faulted into the context.
I tried to find out what does register mean here however I couldn't anywhere for the last 1 hour. Then I tried to check if there's any method that let you register the object, which may shed light on the confusion, again I couldn't find any. Then I tried to print out all "register objects" with registeredObjects, the result is also a mystery.
Can anyone please explain or point me to the right source where I can understand what is this registering mechanism for managed object? And if possible maybe also point me to the right direction of understanding how exactly does core data, especially managed context work? (Everytime I thought I finally understood it, there seems to be more mystery)
I am working with the Owned type as found here: Strong reference of Autofac 2
I'm also using Quartz scheduler, MSMQ, and EF.
My config looks as follows. I've clearly got something wrong as the context that gets injected to the repositories is a different instance than the one given to the service.
builder.RegisterType<EmailAllocationJob>();
builder.RegisterGeneric(typeof(JobWrapper<>));
builder.RegisterType<DataContext>().InstancePerOwned<EmailAllocationJob>();
builder.RegisterType<DataContext>().As<IUnitOfWork>();
builder.RegisterType<EmailAccountRepository>().As<IEmailAccountRepository>();
builder.RegisterType<EmailMessageRepository>().As<IEmailMessageRepository>();
builder.RegisterType<EmailMessageQueue>().As<IEmailMessageQueue>();
builder.RegisterType<EmailAllocationService>().As<IEmailAllocationService>();
I can't for the life of me figure out how to get the configuration fixed. I'd reckon it's the line:
builder.RegisterType<DataContext>().As<IUnitOfWork>();
What I want to say is something like:
builder.RegisterType<DataContext>().As<IUnitOfWork>().InstancePerOwned<EmailAllocationJob>();
Thanks in advance if you can help.
Ok I got it. Needed the line:
builder.RegisterType<DataContext>().InstancePerOwned<EmailAllocationJob>()
.As<IUnitOfWork>().AsSelf();
So it seems important that the DataContext features as the generic argument to RegisterType ONCE, and that the method calls to As<>() and AsSelf() are to be daisy chained in a single statement. Seems obvious now, with a fresh head following yesterday evening.
I have a Command bound to my MainWindow via CommandBinding to the Delete key.
I want that, depending on the context (which element is active), the same command would be translated to different delete commands that semantically delete things.
In few words, I want that pressing the Delete key will delete things depending on where the focus is at that moment.
I have heard about CompositeCommands (PRISM), but I don't know whether that is useful an easily applicable here.
Thanks a lot!
CompositeCommand in Prism helps you register several commands in one, so that all can be called at the same time.
Since you use MVVM, you should have a ViewModel for each kind of object at least. If you have something like a ViewModelBase or an IViewModel, you could simply add a command to it, and initialize it differently in each ViewModel.
I have a custom node type for which I want to have a field that uses a special combobox based on list_text. When one chooses the type list_text it is normally possible to enter a static list of selectable texts, however, I want this list to be dynamic, i.e. based on the results of a db_query. What is the best way to do this using Drupal 7?
A simple example for clarification: A node of this custom type X contains a field that points to another node, so whenever a node of type X is created I want a combobox that contains all other nodes.
(Best solution would be to only display the combobox during node creation, and no longer during edit. But I could also live with it if the combobox was shown during the edit as well.)
I have tried to customize options_select by defining my own data type and implementing hook_options_list accordingly. The combobox was displayed during creation with the correct values, however, I could not save it.. I have no idea what went wrong there, but on the first submit it would change to a different theme, and when I tried again I got an internal server error. Am I on the right track at all with defining a completely new data type for the field? there surely must be a simpler way?
You're right in that you don't need a new datatype. Here's a good tutorial on how to do this. It's not specifically for D7 but I didn't see much that wasn't still applicable. There may be a better way to do it in D7 specifically but I would love to know it too if so :)
The tutorial linked by allegroconmolto sent me on the right way. Thanks for that.
Here's the simpler way of doing it: tutorial
Basically, it is, as I assumed, a common problem and hence a simple solution for it was included in the webform module by now. It provides a hook_webform_select_options_info which can be used to register a callback method. The callback method is then called each time a corresponding option select of a webform is shown, so that you can easily fill it with the results of a dbquery or anything else. Works like a charm and takes next to no time to implement.
I have a fairly simple question to which I cannot seem to find the answer for. I have a silverlight app with Ria Services. In the DomainService class I have an update method like below:
public void UpdateConversationState(ConversationState currentConversationState)
{
var original = ChangeSet.GetOriginal(currentConversationState);
if (original != null)
ObjectContext.ConversationStatesRepository.AttachAsModified(currentConversationState, original);
else
ObjectContext.ConversationStatesRepository.Attach(currentConversationState);
currentConversationState.UpdDat = DateTime.Now;
if(original.Name != currentConversationState.Name)
//Do something extra
}
The problem is that the Name property is always empty. In fact every field except for the Id has default values. I've tried searching for how the GetOriginal method works, but cannot find any help. It seems to mee like it tries to rebuild the original object on the server, based on the changes that are sent back from client to server.
Or maybe anyone knows a better way to check if a certain property of an object has been changed during an update? I could off course compare it to the value in the database, but it seems like I should avoid this extra call to the database.
Any help is again much appreciated :-)
EDIT:
Just found out about the RoundTripOriginalAttribute. This seems to do the trick. Am I the only one by the way that think RIA could be documented a little bit better?
Well, I've been also looking for a way track entity changes with EF4 and after some googling I've found that you need to apply the "RoundTripOriginal" attribute to the properties of the entity you want to track, because RIA (by default) does not send the original values back to the server.
I still have some concerns on this and I asked some of the gurus:
http://forums.silverlight.net/forums/t/218332.aspx
This worked for me, but I still donĀ“t think is the best way out of it.
Hope this helps.