C# How to pass service and model to a search window

Asked

Viewed 43 times

2

Hello,

I have the following situation. A CRUD form calling another form (common or generic) to search for records...

private void barButtonItemProcurar_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
        FrmProcura<IMarcaService> frmProcura = new FrmProcura<IMarcaService>(_marcaService);
        frmProcura.ShowDialog();
    }



public partial class FrmProcura<T> : Form
{
    private T _service;

    public FrmProcura(T service)
    {
        _service = service;

        InitializeComponent();
    }

    private void simpleButtonFechar_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void simpleButtonProcurar_Click(object sender, EventArgs e)
    {
        //AQUI
        var dados = _service.Procura("nome", textEditConteudo.Text);

        gridControlDados.DataSource = dados;
        gridControlDados.RefreshDataSource();
    }
}

But in this way the methods (example of "search") of the service are not inherited (//HERE).

What is the best way to resolve this issue? I would also like to pass any model (empty) to the search window so that it returns populated with the record chosen in the search.

  • I couldn’t quite understand your problem... some mistake, where it just doesn’t work?

  • It just doesn’t work. Service methods are not exposed. :-(

  • What do you mean they’re not exposed?

  • My services (brands, products, customers etc) have various methods (search, include, change, delete, pagination etc). The way it is, I can’t use them. [’T' does not contain a Definition for 'Search' and no accessible Extension method 'Search' Accepting a first argument of type’T' could be found]

  • Please enter the code of your Imarcaservice interface and your parents if you hear. I think you can solve by implementing the search method definition in a Parent interface.

1 answer

0

You chose to use the generics in c#, but did not give more information to the compiler than you "want" or expect with this tipo... Thus the compiler interprets his tipo, just as a simple object, so you will be shown only the methods that fit the type object.

You can use the directive where to provide more information on the restriction of tipo you wish to receive. Here you can find more information.

In your case, I believe your code could be written as follows:

public class FrmProcura<T> : Form where T : IMarcaService
{
     private T _service;
}

Specifying that you "wait" a tipo that implements its interface IMarcaService, makes what you compile understand that your service implements IMarcaService and then leave exposed all its methods and properties...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.