0

I'm using Devart's Linq to sql in my project (not Entity Framework). Try to select from database with Query<T>, or ExecuteQuery<T>, but T deliberately don't have public parameterless constructor. Can I somehow overwrite how should a new instance of T created? For example giving an instance of AutoMapper.Profile at some point? Or giving a Func<T> that will create the T instance before it's overwritten with the value from the DB?

using( var DC = new SomeDevartDataContextBaseDerivative() )
{
    // exception because parameterless constructor
    var FM = DC.ExecuteQuery<SomeTypeWithoutParameterlessConstructor>( "Select 1 as Id" );
    return FM;
}
ntohl
  • 2,067
  • 1
  • 28
  • 32
  • I know I can for example create a SomeTypeWithoutParameterlessConstructor2 type with exact same parameters, and with an parameterless constructor, than map it. – ntohl Nov 10 '17 at 15:21

1 Answers1

0

There is no way to pass a type without public parameterless constructor.

Devart
  • 119,203
  • 23
  • 166
  • 186
  • It would be nice, if implemented. – ntohl Nov 14 '17 at 13:55
  • Constructors with parameters should implement logic for checking values passed and generating errors if value is invalid. Passing null (referenced type) and 0 (structure) is not a universal solution, this will not work in most cases. That's why it is not implemented. – Devart Nov 20 '17 at 11:55
  • I mean we can implement the construction logic. Like AutoMapper does with `ConstructServicesUsing` here: https://github.com/AutoMapper/AutoMapper/wiki/Dependency-injection. Or MVC 5 does with `DependencyResolver.SetResolver`. So passing null will never happen. Checking values passed will be the same as before. User must take care of it. – ntohl Nov 20 '17 at 13:48