Skip to content
Jon P Smith edited this page Apr 11, 2018 · 6 revisions

CrudServices has tow types of Read method, which are used in very different ways. The ReadSingle<T> versions are used in Update and methods Delete, while the ReadManyNoTracked<T> is use when you want to produce a list. I will cover each one separately.

ReadSingle<T>

There are two versions of this command, both of which can handle a entity class or a GenericServices' DTO (with its ILinkToEntity<TEntity> interface). They are:

  • ReadSingle<T>(params object[] keys), which uses the primary key value(s) to load the item.
  • ReadSingle<T>(Expression<Func<T, bool>> whereExpression), which uses a LINQ expression to find an single instance.

Both of these return a status error saying "Sorry, I could not find the {class name or description} you were looking for" if no entry was found. The whereExpression version will throw an exception if more than one instance is found. If you ask for an entity class then will return a tracked entity, i.e. you can alter the instance returned and call EF Core's SaveChanges method to update it.

Typically in web/mobile applications the is ReadSingle<T> methods are used to show the user a page that contains, which is why they support projection to a DTO. The DTO can contain.

  1. Read-only information - used for the user to confirm that this is the correct item to update/delete.
  2. Read/Write information - these are the inputs for the user to edit/change things in an update.

In the document GenericServics and DTOs I talk about how the Read-only information should be marked with the [ReadOnly(true)] attribute. Its important to do that.

NOTE: You should always return the Read-Only values in case there is an error. Otherwise, when you redisplay the page some of the information will be missing. See all the update RazorPageApp examples, where I do that.**

ReadManyNoTracked<T>()

This is the method you will use to list things. It returns an IQueryable<T>, which you can then filter, page etc. - see Home/Index/Index.cshtml.cs for a implementation that adds sorting, filtering and paging on the result of a ReadManyNoTracked<BookListDto>() call.

If you provide ReadManyNoTracked<T>() with a GenericServices DTO it will use AutoMapper to create a LINQ Select query.

Configuring the AutoMapper Read mapping on DTOs

When reading to a DTO sometimes the default AutoMapper mapping may not do produce the LINQ you want. In that case you need to add a PerDtoConfig<TDto,TEntity> class, where you can alter the read mapping. See this link on how to do that.

Clone this wiki locally