I'm used to work with StructureMap with Web Apps... but now, I'm working on a Windows Forms project and I'd like to use it, but I don't how to configure it.
In web, I'd have a bootstrapper class that is invoked on Application_Start on Global.asax, but I don't know how to do the same on WinForms.
Thanks!
You can initialize the container in the static main method that starts your application. Then retrieve your form instances from the container, so that any necessary dependencies can be injected. You could still put the initialization code in a Bootstrapper.
static class Program
{
[STAThread]
static void Main()
{
ObjectFactory.Initialize(...);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(ObjectFactory.GetInstance<Form1>());
}
}
For a Winforms application the counter part to Application_Start would be the main method that initializes the first Form.
When using ORM mappers with web applications you generally have the thumb rule of creating a data context/session per http request. For a Winforms application you tend to go for a context per operation or per form.
You'd structure the bootstrapping and IoC configuration in the same ways (though I'm not sure how you'd include the form classes themselves, I haven't worked with WinForms much). The only real difference that you'd need is when/where the initializer gets called. It just has to be in the startup of the application. For web applications, you do indeed call it from Application_Start. I think in WinForms apps it would be in the OnLoad event of the main form.
If you have a main method anywhere (similar to a console app) then that would work as well. This could be if the WinForms app was ported from a console app, for example.
Related
I have a WPF desktop app that using EF orm for mapping. My app have several pages
and each page has several windows. EF context is created per form. Pages in app using stateless entity only to show data the user.
My problem is that a page should update an entity for a single task and for this reason there should be a context living on this page. Therefore where I open window on this page for update other entities, there are 2 contexts active on this page.
How I can prevent this condition?
Create a new class "ClientRepository" in your project. This class will contain all entity framework access logic your client has.
Use this class in all your windows and pages.
If you configure this class to behave as singleton, you will only have one entity framework context.
Ideally this can be done using a DI framework like Microsoft Unit or Ninject, but of course you can also do it manually.
Singltons?
You might ask yourself, whether its a good idea to you singletons here.
I had a similar question once and thus asked Brian Noyes (Microsoft MVP) on a "MVVM" course on the pluralsight website.
In his response he wrote: "...most of my client services are Singletons anyway and live for the life of the app."
So, for a client service, its ok to have a singleton.
I have an MVC webapp and am creating a Winforms app. The methods I have created and use in the MVC app will also be used by the Winforms app. Is there a way I can share the code between them so that I can update it all in one place? Maybe turn those methods into a dll that they both use? If so, how do I go about doing that?
I am used to using Windsor Castle IoC with MVC web applications so I'm a little familiar with registering components and bootstrapping the application.
I am now trying to create my first WPF application and I'm a little lost....
In a typical MVC project I would register all my components in Application_Start in the Global.asax file.
Also, in the Global.asax I would do something like this :
_container = new WindsorContainer();
var myControllerFactory = new MyControllerFactory(_container.Kernel);
ControllerBuilder.Current.SetControllerFactory(myControllerFactory);
In MyControllerFactory which inherits from DefaultControllerFactory I would resolve the Controller given to me, thus all the controllers being used would have their dependencies resolved automatically.
How could I do this in a WPF application ?
I created a class called ContainerBootstrapper that registers my components, and now I'm wondering how I use this in my MainWindow so every time I call a component, it's dependencies are resolved automatically...
Unlike MVC, WPF does not provide framework for decoupled UI development out of the box. There are several out there (such as Caliburn.Micro which implements the MVVM pattern) that sit on top of WPF and provide integration for a variety of IoC containers as well as other features you may find useful in WPF development.
Alternatively, you will need to manage the container directly. i.e. instantiate and initialise the container in App.xaml.cs or MainWindow.xaml.cs and ensure that new object construction via your container's Resolve() function. If it is a non-trivial app, you will probably want to integrate this into your app's navigation routines.
I have the following Ria Service define:
namespace SilverlightTest.Web
{
[EnableClientAccess()]
public class ContactService : LinqToEntitiesDomainService<AdventureWorksEntities>
{
public IQueryable<Contact> GetContactSearch(string lastName)
{
ContactRepository rep = new ContactRepository();
return rep.SearchByLastName(lastName);
}
}
}
When I compile the solution, my SilverlightTest project does create the SilverlightTest.Web.g.cs file and the appropriate Context objects are created when I look at it. However, when I attempt to import the SilverlightTest.Web namespace to access the Data Context class for the above service, it says it cannot find the Web namespace.
The only difference I can see between what I'm doing and many examples that are out there on the web is that my AdventureWorksEntities data context is located in a separate business object dll. I tried to query the context directly instead of using the Repository Pattern I'm attempting to do and it also isn't work working.
Any ideas? Is it possible to have Ria Services access a separate DLL handling data access or does it HAVE to be in the same project?
I've been able to put the Ria service in a separate project before, although I do remember having issues. Not sure exactly what it was, but I would check two things: your references and your web.config (in the hosting website). When you add a ria service to a web project it does some things behind the scenes that wire everything up correctly.
Could try adding a service to your web project temporarily and see what it adds.
It seems that Resharper does not recognize the .gs files and their name spaces. If you disable R# or just code without intelisense it works.
I have come across a bit of a problem while using Unity and WPF. The scenario is I have a WPF application which follows the MVVM design pattern. A have a module called ViewKDI. Within this module I have a service called ViewKDIService, the ViewKDIService service utilises another service called UserService.
Every time I load the module ViewKDI I want Unity to return me a new instance of both the ViewKDIService and the UserService.
I have put the below in the shell bootstrapper:
Container.RegisterType<IUserService, UserService>();
In the ViewKDI module I have put the following:
Container.RegisterType<IViewKDIService, ViewKDIService>();
Each time the ViewKDI module loads the ViewKDIService constructor is called. However the UserService constructor is only called the first time, this means that I am not getting a new instance of UserService.
I require unity to give me a new instance of UserService too so that I can manage this session separately from the rest of the application.
Any ideas?
Thanks
Faisal
Unity's default behaviour is to create a new instance of each object each time one is requested, so you shouldn't be seeing this behaviour.
From what I can gather from the source code and MSDN documentation (this is a good read), you can specify a "lifetime manager" object when you register a type to tell Unity how the type should be constructed and cached. Using the TransientLifetimeManager (which essentially does no caching) will cause Unity to re-create the class each time. So try this:
Container.RegisterType<IUserService, UserService>(new TransientLifetimeManager());
... and see if it creates a new UserService each time.