Search code examples
c#asp.net-mvc3-tier

Transferring paged data in 3tier application


I'm not sure there is no question as such. but I did not find It So basically what I have is a 3-tier web application(MVC) with several assemblies defining BLL and DAL. For each Db entity defined I created Data Transfer Object to pass through the BL layer, but in some cases I need to return data in form of portion within DataPortion class(defines a limited list and amount of all data was detected by the query. Interface of DataPortion

public interface IDataPortion<TItem> where TItem : class
{
    IEnumerable<TItem> Items { get; }
    int TotalCount { get; }
}

And here the example of repository's method signatures that returns data in that way

IDataPortion<TEntity> Get(int page, int pageSize);
IDataPortion<TEntity> GetByAuthor(string uid, int page, int pageSize);

So it returns DataPortion type to BL layer. Before returning data to Presentation layer, all entities are mapped to DTO objects. But what am I supposed to do with this DataPortion? I can not just directly reference DAL from presentation l-r bypassing the BL assembly.

So my question is: Should I create some sort of DTO for DataPortion, like this

PagedResult<TItem>
{
    IEnumareble<TItem> Items { get; }
    int TotalCount { get; }
}

And than map it to IDataPortion implementation? Or I should be avoid of such data transferring from DAL and return totalCount and list of items separately? By using DataPortion I'm trying to avoid of creation methods or properties like "FoundedByAuthorAndCreationDateCount" in my repositories.


Solution

  • You should not expose your data layer components such as your model to presentation layer directly. In order to transfer the data to presentation layer, you can use DTOs, viewmodels etc. In your case, you can create another project named "Crosscutting" that contains helpers, viewmodels, generic objects, DTOs etc. All other projects can reference to "Crosscutting". You can move IDataPortion interface and DataPortion class there so all other project can access to them. Both model classes in Data Layer and viewmodels in other projects can utilize this interface.