Put views/viewType/viewInstance in App.config?

Topics: CAB & Smart Client Software Factory
Jun 2, 2007 at 3:00 AM
In App.config, you can have a CompositeUI section, services collection for services (serviceType and serviceInstance). Is it possible to have a views collection for views (view interface as the viewType and concrete view as viewInstance) in App.config?
So I can swap different concrete views for the same view interface in App.config? For example, I have a Windows calendar and WPF calendar, both implements ICalendarView. Another app wants to show a calendar view in a workspace but it's not decide at design time. So I want to put different viewInstances in the App.config.
Another interesting question will be what happen if I add a viewInterface/viewInstance in the place of serviceType/serviceInstance? Will the object builder crash?
Coordinator
Jun 3, 2007 at 8:56 PM
Edited Jun 4, 2007 at 12:09 AM

lzy_tek wrote:
In App.config, you can have a CompositeUI section, services collection for services (serviceType and serviceInstance). Is it possible to have a views collection for views (view interface as the viewType and concrete view as viewInstance) in App.config?


No there is nothing built in. You can build that yourself by creating a new configuration section and create the views using a service instead of WorkItem.SmartParts.AddNew<MyView>();
You would do
ViewFactory.AddNew<IMyView>()

The ViewFactory service would be added to the WorkItem and the AddNew method will grab the type T of the interface, will look up in the configuration section for the "activeTechonlogy" implementation and create the correct view and add it to the WorkItem.SmartParts collection.

<views activeTechonlogy="WPF">
   <view viewInterface="IMyCalendar, CalendarModule">
       <implementation type="WPFCalendar, CalendarModule"  technology="WPF" />
       <implementation type="Calendar, CalendarModule"  technology="Winforms" />
   </view>
</views>

Another interesting question will be what happen if I add a viewInterface/viewInstance in the place of serviceType/serviceInstance? Will the object builder crash?


Services are singleton. That means that you won't be able to have two instances of the same view. Also, the views will be accesible using WorkItem.Services, which is some kind of a hack.

Let me know if it helps
Matias
http://staff.southworks.net/blogs/matiaswoloski
Jun 4, 2007 at 4:09 AM
Thanks, Matias. That confirms my thoughts. Can you point me the code sample to create factory service which will look up config file? I know I have to use reflection but it's better to copy some snippets (like code for services).
Coordinator
Jun 5, 2007 at 3:10 AM
Edited Jun 5, 2007 at 1:36 PM
public class ViewFactory : IViewFactory {
   private WorkItem container;
 
   [InjectionConstructor]
   public ViewFactory( [ServiceDepdendency] WorkItem container ) {
       this.container = container;
   }
 
   public TView AddNew<TView>() {
       Type interfaceType = typeof(TView);
       Type implType = GetImplFromConfiguration( interfaceType );       
       return (TView)this.container.SmartParts.AddNew( implType );
    }
}

usage

public class ModuleController {
 
    public void AddServices() {
        WorkItem.Services.AddNew<ViewFactory, IViewFactory>();
    }
 
    public void AddViews() {
        IMyView view = WorkItem.Services.Get<IViewFactory>().AddNew<IMyView>();
        WorkItem.Workspaces["SomeWorkspace"].Show(view);
    }
}

Matias
http://staff.southworks.net/blogs/matiaswoloski