Click here to Skip to main content
15,909,530 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to show main window after successful login.I used interface as parameter to constructor in main window. Now when I try showing main window, I get an error because I cannot pass interface as parameter to main window.I saw many posts like mine but I thought it is quite different from them.

This is my main window constructor:

    <pre>public Home_Page(IGetAllItemClass clas)
            {
                InitializeComponent();
                _allClass = clas;
            }
            IGetAllItemClass _allClass;


My code in login Window from where I need to show main form:

Home_Page h = new Home_Page();
     h.ShowDialog();

My app.xaml:

<Application x:Class="Cafe_WPF.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:local="clr-namespace:Cafe_WPF"
                 Startup="App_Startup">
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="scroll_style.xaml" />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    </Application>


This is my App.cs :


<pre>public partial class App : Application
        {
            void App_Startup(object sender, StartupEventArgs e)
            {
                #region login_dependencies
                var container = new UnityContainer();
                container.RegisterType<IGetIP, get_ip_address>();
                container.RegisterType<IUserDetails, get_user_details>();
              container.RegisterType<IgetBusinessDetailsFromPosId, get_business_info_from_pos>();
                #endregion
                #region home_page_dependencies
                var home = new UnityContainer();
                container.RegisterType<IGetAllItemClass, get_all_item_class>();
                Home_Page hm = home.Resolve<Home_Page>();
                #endregion
                Login_Window lg = container.Resolve<Login_Window>();
                lg.ShowDialog();
            }
        }


This is my interface:

namespace Cafe_WPF.Interface
    {
      public interface IGetAllItemClass
        {
            DataTable item_class(string business_info_id, string rvc_id);
        }
    }


And my class service implementing interface is:

class get_all_item_class : IGetAllItemClass
    {
        public DataTable item_class(string business_info_id, string rvc_id)
        {
            try
            {
                string sql = //query
                return CafePOS.Library.DataAccessLayer.Instance.ExecuteQuery(sql);
    
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

I am trying to use dependency injection.May be I am missing something. Can anyone help me on this ? I am stuck on this.





What I have tried:

Above codes are what I tried.May be I am taking architecture the wrong way
Posted
Updated 9-Aug-17 4:19am
Comments
Kornfeld Eliyahu Peter 9-Aug-17 9:29am    
But the constructor in the first part will never be called...
public Home_Page(IGetAllItemClass clas)!!! but new Home_Page()!!! Which will call the default (parameter-less) constructor, or fail...
Gorkhali 10-Aug-17 2:03am    
I too meant that sir. How can I be able to call parameterised constructor .If I invoke Interface from Login window ,is there any meaning of having Dependency Injection? How can I call that home page?
Kornfeld Eliyahu Peter 10-Aug-17 2:10am    
You have got your solution 16 hours ago! Read it!
Gorkhali 10-Aug-17 2:24am    
I read it and did the same way.Could you please read the comment below that answer and help me on that? Thanks.

1 solution

Quote:
Home_Page h = new Home_Page();

You can't call that, because the Home_Page class doesn't have a parameterless constructor. You need to pass in an instance of the IGetAllItemClass interface.

The simplest solution is to import the Home_Page instance as a parameter to the constructor of your Login_Window class:
C#
private readonly Home_Page _homePage;

public Login_Window(..., Home_Page homePage)
{
    ...
    _homePage = homePage;
}

public void ShowHomePage()
{
    _homePage.ShowDialog();
}

That way, you're using the instance created by the Unity container, rather than trying to create a new instance manually.
 
Share this answer
 
Comments
Gorkhali 10-Aug-17 2:23am    
Thank you sir.I was stuck on it for few days. At the same time, I have a bit confusion. This way I need to import instance of every form I need to show? Can you please clarify me? Or if I my application architecture or Unity implementing architecture is wrong?
Richard Deeming 10-Aug-17 6:57am    
You either need to import an instance of each form, or you need to import the dependencies required to create each form.

A better approach would be to create a factory for each form, where the implementation imports the required dependencies. You would then import the factory for each form you need to show.
public interface IHomePageFactory
{
    Home_Page Create();
}

public class HomePageFactory
{
    public HomePageFactory(IGetAllItemClass getAllItems)
    {
        GetAllItems = getAllItems;
    }
    
    public IGetAllItemClass GetAllItems { get; }
    
    public Home_Page Create()
    {
        return new Home_Page(GetAllItems);
    }
}

public class Login_Window
{
    public Login_Window(IHomePageFactory homePageFactory)
    {
        HomePageFactory = homePageFactory;
    }
    
    public IHomePageFactory HomePageFactory { get; }
    
    public void ShowHomePage()
    {
        Home_Page h = HomePageFactory.Create();
        h.ShowDialog();
    }
}

var container = new UnityContainer();
container.RegisterType<IGetAllItemClass, get_all_item_class>();
container.RegisterType<IHomePageFactory, HomePageFactory>();

Login_Window lg = container.Resolve<Login_Window>();
lg.ShowDialog();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900