GithubHelp home page GithubHelp logo

yicong1352013 / abp.relateddtoloader Goto Github PK

View Code? Open in Web Editor NEW

This project forked from easyabp/abp.relateddtoloader

0.0 0.0 0.0 141 KB

An Abp module that help you automatically load related DTO (like ProductDto in OrderDto) under DDD.

License: MIT License

C# 97.90% PowerShell 2.10%

abp.relateddtoloader's Introduction

Abp.RelatedDtoLoader

NuGet NuGet Download

An Abp module that help you automatically load related DTO (like ProductDto in OrderDto) under DDD.

Installation

  1. Install the following NuGet packages. (see how)

    • EasyAbp.Abp.RelatedDtoLoader
    • EasyAbp.Abp.RelatedDtoLoader.Abstractions
  2. Add DependsOn(typeof(AbpRelatedDtoLoaderModule)) attribute to configure the module dependencies. (see how)

Usage

  1. Make your Order entity (or aggregate root) like this.

    public class Order : AggregateRoot<Guid>
    {
        public virtual Guid ProductId { get; protected set; }
    
        // do not add navigation properties to other aggregate roots!
        // public virtual Product Product { get; set; }
    
        protected Order() { }
    
        public Order(Guid id, Guid productId) : base(id)
        {
            ProductId = productId;
        }
    }
  2. Add RelatedDto attribute in OrderDto.

    public class OrderDto : EntityDto<Guid>
    {
    	public Guid ProductId { get; set; }
    
    	[RelatedDto]
    	public ProductDto Product { get; set; }
    }
  3. Create MyProjectRelatedDtoLoaderProfile and add a rule.

    public class MyProjectRelatedDtoLoaderProfile : RelatedDtoLoaderProfile
    {
    	public MyRelatedDtoLoaderProfile()
    	{
    		// the following example gets entities from a repository and maps them to DTOs.
    		UseRepositoryLoader<ProductDto, Product>();
    
    		// or load it by a customized function.
    		UseLoader(GetOrderDtosAsync);
    
    		// a target type need to be enabled to load its related Dtos properties.
    		// LoadForDto<OrderDto>();
    	}
    }
  4. Configure the RelatedDtoLoader to use the profile.

    public override void ConfigureServices(ServiceConfigurationContext context)
    {
    	// ...
    
    	Configure<RelatedDtoLoaderOptions>(options =>
    	{
    		// add the Profile
    		options.AddProfile<MyProjectRelatedDtoLoaderProfile>();
    	});
    
    	// ...
    }
  5. Enable the target type to load its related Dto properties.

    either in the Profile

    public class MyProjectRelatedDtoLoaderProfile : RelatedDtoLoaderProfile
    {
    	public MyRelatedDtoLoaderProfile()
    	{
    		// ...
    
    		// a target type need to be enabled to load its related Dtos properties.
    		LoadForDto<OrderDto>();
    	}
    }

    or via RegisterTargetDtosInModule method of RelatedDtoLoaderOptions

    public override void ConfigureServices(ServiceConfigurationContext context)
    {
    	// ...
    
    	Configure<RelatedDtoLoaderOptions>(options =>
    	{                                
    		// adding module will auto register all the target dto types which contain any property with RelatedDto attribute.
    		options.LoadForDtosInModule<MyApplicationContractsModule>();
    	});
    
    	// ...
    }
  6. Try to get OrderDto with ProductDto.

    public class OrderAppService : ApplicationService, IOrderAppService
    {
    	private readonly IRelatedDtoLoader _relatedDtoLoader;
    	private readonly IRepository<Order, Guid> _orderRepository;
    
    	public OrderAppService(IRelatedDtoLoader relatedDtoLoader, IRepository<Order, Guid> orderRepository)
    	{
    		_relatedDtoLoader = relatedDtoLoader;
    		_orderRepository = orderRepository;
    	}
    
    	public async Task<OrderDto> GetAsync(Guid id)
    	{
    		var order = await _orderRepository.GetAsync(id);
    
    		var orderDto = ObjectMapper.Map<Order, OrderDto>(order);
    
    		return await _relatedDtoLoader.LoadAsync(orderDto);   // orderDto.Product should have been loaded.
    	}
    }

See more: Custom DTO source examples.

Roadmap

  • Custom DTO source
  • Support one-to-many relation
  • Support non Guid keys
  • Support multi module development
  • Support nested DTOs loading
  • Get duplicate DTO from memory
  • DTO cache
  • An option to enable loading deleted DTO
  • Unit test

Thanks @wakuflair and @itryan for their contribution in the first version.

abp.relateddtoloader's People

Contributors

gdlcf88 avatar itryan avatar znow avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.