Base solution for your next web application
Open Closed

OData with Dto/ViewModel instead of Entities #7595


User avatar
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.


10 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    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,

  • User Avatar
    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);

  • User Avatar
    0
    ismcagdas created
    Support Team

    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 ?

  • User Avatar
    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.

  • User Avatar
    0
    ismcagdas created
    Support Team

    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).

  • User Avatar
    0
    ismcagdas created
    Support Team

    @mhrabak any news ?

  • User Avatar
    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<TPrimaryKey> for these ViewModels we create DbQuery in DbContext (otherwise exception is thrown from Abp) and our repositories works with ViewModels and inside concrete implementation of Repository we map Entity to ViewModel - for now using AppServices without OData - this will come later we now need to quickly port base functionality. Idea behind Odata is to create OData controller and using DI we will inject concrete AppService to this controller thanks to this we will have all previously created functionality with support for OData - just idea not tested if possible.

  • User Avatar
    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?

  • User Avatar
    0
    ismcagdas created
    Support Team

    @mhrabak

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

  • User Avatar
    0
    ismcagdas created
    Support Team

    This issue has been automatically closed because it has not had recent activity.