Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make EntityManager.executeQuery() signature return actual entity type #4

Open
AbakumovAlexandr opened this issue Feb 15, 2018 · 2 comments

Comments

@AbakumovAlexandr
Copy link

AbakumovAlexandr commented Feb 15, 2018

When I'm trying a code like in the example from README.MD:

    getAllCustomers(): Promise<Customer[]> {
        let query = EntityQuery.from('Customers').orderBy('companyName');

        return this._em.executeQuery(query)
            .then(res => res.results)
            .catch((error) => {
                console.log(error);
                return Promise.reject(error);
            });
    }

It doesn't compile with an error:

Type 'Promise<Entity[]>' is not assignable to type 'Promise<Customer[]>'.

So I have to cast it to through any to the actual Entity type it supposed to return:

.then(res => res.results (res.results as any) as Customer[])

Can we change this method signature to return an actual entity type instead of Entity?

@marcelgood
Copy link
Collaborator

That would be nice. Unfortunately, there is nothing in the signature that would let TypeScript infer the result type. At some point you would have to specify the type. For example via the query.

let query = EntityQuery.from<Customer>('Customers').orderBy('companyName')

I'm not sure that would make it any better, though. This used to work w/o having to cast, but TypeScript has gotten more strict and now we are forced to cast.

@AbakumovAlexandr
Copy link
Author

AbakumovAlexandr commented Feb 15, 2018

Making EntityQuery.from() generic is exactly what seems to be the best option here from my point of view.

I'm thinking of how it's implemented in EF, for example, here:

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    string esqlQuery = @"SELECT VALUE Contact
        FROM AdventureWorksEntities.Contacts as Contact where Contact.LastName = @ln";

    // The following query returns a collection of Contact objects.

    // ---------------- See the following line ---------------------------------------------
    ObjectQuery<Contact> query = new ObjectQuery<Contact>(esqlQuery, context, MergeOption.NoTracking);
    // -------------------------------------------------------------

    query.Parameters.Add(new ObjectParameter("ln", "Zhou"));

    // Iterate through the collection of Contact items.
    foreach (Contact result in query)
        Console.WriteLine("Contact First Name: {0}; Last Name: {1}",
                result.FirstName, result.LastName);
}

Also in my opinion, introducing type parameters makes API cleaner by explicitly demonstrating that a method is supposed to return specific type as requested by a caller.

Also, I'd like to make the same proposal regarding EntityManager.createEntity() method and maybe some other similar methods which I'm not familiar yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants