Open Closed

OData with Dto/ViewModel instead of Entities #7595


0
mhrabak created

Hi guys, I tried to implement OData support for ASPNet Zero template but I am facing problem. I guess we can not use ApplicationService when we want to use OData right? I followed this article but the problem is we want to use Dtos/ViewModels instead of Entities, but AbpODataEntityController force us to use (and return) Entities. Idea is that repos will work with Entities and controller will transform entities to ViewModels and return ViewModel. So something like this: ` [AbpMvcAuthorize] public class ODataController<TEntity, TViewModel> : AbpODataController where TEntity : class, IEntity where TViewModel : class, new() {

[NotNull] private readonly IRepository _repository;

[NotNull] private readonly IMapper _mapper;

public ODataController([NotNull] IRepository

For ODataConventionModelBuilder we will register Dtos/ViewModels instead of Entities too. In our one of our applications we have something similar. We must specify TEntity too just for sake of Repository. Question is when we implement this will we be able to use nswag for generating proxies in angular? What about permissions in angular? Do you see some problems with this solution? Do we have to rework generated AppServices to controllers to be able to use them with OData too?

Thanks in advance.


9 Answer(s)
  • 0
    ismcagdas created

    Hi,

    Have you followed one of the documents below ?

    ASP.NET Core MVC 5.x: https://aspnetboilerplate.com/Pages/Documents/OData-Integration

    ASP.NET Core: https://aspnetboilerplate.com/Pages/Documents/OData-AspNetCore-Integration

    Thanks,

  • 0
    mhrabak created

    Yes I sucessfully implemented approach 2, but question was quite different. I asked how can we use OData in ASP.NET Zero with DTOs/ViewModels instead of Entities.

    P.S.: Can you please update your documentation for ASP.NET Core v2.2 it is necessary to registee MvcCore pipe like this: https://github.com/OData/WebApi/issues/1707

    services.AddMvcCore(options => { options.EnableEndpointRouting = false; foreach (var outputFormatter in options.OutputFormatters.OfType<ODataOutputFormatter>().Where(_ => _.SupportedMediaTypes.Count == 0)) { outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata")); } foreach (var inputFormatter in options.InputFormatters.OfType<ODataInputFormatter>().Where(_ => _.SupportedMediaTypes.Count == 0)) { inputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata")); } }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

  • 0
    ismcagdas created

    Thanks @mhrabak for your explanation. Unfortunately I don't know how to do it at the moment. Are you able to do it in a Regular OData project ?

  • 0
    mhrabak created

    @ismcagdas Yes, We have implemented such functionality (code from my initial post). Question is if exists such possibility with asp.net zero. Reasons to do it is same as for common controller - security, not to show all properties from entity and that ViewModel can be different from Entity of course which is our reason why we need such functionality.

  • 0
    ismcagdas created

    Could you share how did you implement it before ? Similar approach should work in AspNet Zero as well (Not in AppServices but Controllers defined by you).

  • 0
    ismcagdas created

    @mhrabak any news ?

  • 0
    mhrabak created

    Sorry for delayed answer. I'm overhelmed by porting our current solution to aspnet zero. We solve it that way we inherit our ViewModels from Entity

  • 0
    mhrabak created

    I tried today enable OData - I am able successfully communicate using OData but odata url is not visible in swagger. Used https://aspnetboilerplate.com/Pages/Documents/OData-AspNetCore-Integration Is it possible to use nswag with OData in zero?

  • 0
    ismcagdas created

    @mhrabak

    I'm not sure. You can get more accurate information if you can check https://github.com/domaindrivendev/Swashbuckle.AspNetCore repository.