TBARPT.Library 1.0.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package TBARPT.Library --version 1.0.4
                    
NuGet\Install-Package TBARPT.Library -Version 1.0.4
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TBARPT.Library" Version="1.0.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TBARPT.Library" Version="1.0.4" />
                    
Directory.Packages.props
<PackageReference Include="TBARPT.Library" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TBARPT.Library --version 1.0.4
                    
#r "nuget: TBARPT.Library, 1.0.4"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TBARPT.Library@1.0.4
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TBARPT.Library&version=1.0.4
                    
Install as a Cake Addin
#tool nuget:?package=TBARPT.Library&version=1.0.4
                    
Install as a Cake Tool

Introduction

The purpose of this library is to enable Dynamic LINQ string construction and execution, as well as dynamically creating and executing queries using expression trees. This is intended to support specific applications local to our environment, though it may be useful to others. Helpers are also available for retrieving lists of properties, their types, and display names from a given type. Using ReflectionIgnore attribute will prevent a class from returing results, or individual properties from being included in the list of properties.

New in 1.0.4

Corrected issue where nullable property types were not providing the underlying type when calling GetProperties function.

Getting Started

  1. Add this package and dependencies
  2. Example usage:
using System.Reflection;
using TBARPT.Library;
using TBARPT.Library.Models;
using TBARPT.Repository.Repositories;

namespace TBARPT.Library.Example {
    public class Example {
        private readonly IReportRepository reportRepository;
        public Example(IReportRepository reportRepository) {
            this.reportRepository = reportRepository;
        }
        //when type is not known until runtime.
        public QueryResult GetReport(QueryRequest queryRequest, string typeName) {
            //use reflection to look up report source type
            //it is recommended to have a base query with the same method name as the class name which holds properties for the report
            Type type = Type.GetType("namespace.of.type." + typeName + ", assemblyName")
                ?? throw new ArgumentException($"Type could not be resolved: {typeName}");

            //use reflection to find queryable method
            //This should be wherever your base queries are stored
            var queryMethod = reportRepository.GetType().GetMethod(typeName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static)
                ?? throw new ArgumentException($"Invalid Report Source: {typeName}");

            IQueryable<dynamic> baseQuery = (IQueryable<dynamic>?)queryMethod.Invoke(reportRepository, null)
                ?? throw new ArgumentException("Base query could not be resolved dynamically.");

            var reportBuilder = Activator.CreateInstance(typeof(DynamicLINQReportBuilder<>).MakeGenericType(type))
                ?? throw new MissingMethodException("Could not create instance of DynamicLINQReportBuilder");

            MethodInfo executeQuery = reportBuilder.GetType().GetMethod("GetReport")
                ?? throw new ArgumentException("GetReport method not found");

            QueryResult result = (QueryResult?)executeQuery.Invoke(reportBuilder, [baseQuery, queryRequest])
                ?? throw new Exception("Execution of query returned null.");

            return result;
        }

        //when type is known at compile time
        public static QueryResult GetReport<T>(QueryRequest queryRequest, IQueryable<T> baseQuery) {

            var reportBuilder = new DynamicLINQReportBuilder<T>();

            QueryResult result = reportBuilder.GetReport((IQueryable<dynamic>)baseQuery, queryRequest);

            return result;
        }
        //Utility to get list of properties from a type along with the display names (if set using DisplayName attribute) and types.
        //If type is not known at compile time, use reflection to get the type.
        public List<PropertyDetail> GetProperties(string className) {
            Type type = Type.GetType("namespace.of.type." + className + ", assemblyName")
                ?? throw new ArgumentException($"Report source could not be resolved: {className}");

            var helpers = Activator.CreateInstance(typeof(Helpers<>).MakeGenericType(type))
                ?? throw new MissingMethodException("Could not create instance of Helpers");

            MethodInfo getProperties = helpers.GetType().GetMethod("GetProperties")
                ?? throw new ArgumentException("GetProperties method not found");

            List<PropertyDetail> propertyDetails = (List<PropertyDetail>?)getProperties.Invoke(helpers, null) ?? [];

            return propertyDetails;
        }
        //if the type is known at compile time
        public List<PropertyDetail> GetProperties<T>(T someType) {
            var helpers = new Helpers<T>();
            List<PropertyDetail> propertyDetails = helpers.GetProperties();
            return propertyDetails;
        }
    }
}

AutoMapper Example usage:

//map individual object
DestinationType destination = AutoMapper.MapTo<DestinationType>(sourceObject);

//map individual object with property name mapping
Dictionary<string, string> propertyMap = new Dictionary<string, string> {
    { "DestinationPropertyName", "SourcePropertyName" }
};
DestinationType destination = AutoMapper.MapTo<DestinationType>(sourceObject, propertyMap);

//map list of objects
List<DestinationType> destinationList = AutoMapper.MapToList<DestinationType>(sourceObjects);

//map list of objects with property name mapping
Dictionary<string, string> propertyMap = new Dictionary<string, string> {
    { "DestinationPropertyName", "SourcePropertyName" }
};
List<DestinationType> destinationList = AutoMapper.MapToList<DestinationType>(sourceObjects, propertyMap);
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.3.6 289 9/16/2025
1.3.5 120 9/5/2025
1.3.4 168 9/4/2025
1.3.3.1-beta 253 9/4/2025 1.3.3.1-beta is deprecated because it has critical bugs.
1.3.3 156 9/4/2025
1.3.3-beta 261 9/3/2025 1.3.3-beta is deprecated because it has critical bugs.
1.3.2 109 8/22/2025
1.3.1 154 8/20/2025
1.3.0 158 8/13/2025
1.2.1-beta 153 8/13/2025
1.2.0-beta 158 8/11/2025
1.1.3 347 6/10/2025
1.1.1 139 4/5/2025
1.1.0 200 4/1/2025
1.0.4 489 3/25/2025
1.0.3 156 3/17/2025
1.0.2 203 3/11/2025
1.0.1 224 3/7/2025
1.0.0 231 3/7/2025

Initial release.