add service (injected) inside automapper - .net-core

i'm working in .net core
adding automapper in startup like
services.AddAutoMapper(AutomapperHelper.GetProfileTypes());
is it possibile to inject/add some services/objects inside automapper profiles inside CreateMap() ?!
and use it or pass to mapped objects, inside mapping profiles ...
i would not like to use opt.Items during .Map()
P.S. i was loosing time looking ConstructServicesUsing() but it looks like designed for creating relovers and type converter

Related

Use Json.Net for input binding of Azure EventGridEvent

I currently use Json.Net throughout my project and it works like a charm. Unfortunately, the input binding for Azure Functions use System.Text.Json to deserialize the EventGrid event json and bind it toEventGridEvent object from the Azure.Messaging.EventGrid 4.0.0.-beta.4 library
have a look here: https://github.com/Azure/azure-functions-host/issues/5469
This causes massive problems as all my custom JsonConverters that i have do not work at all which leads to incorrectly deserialized objects. Changing from Newtonsoft.Json to System.Text.Json is a massive amount of rework and i would absolutely like to avoid that, if i can.
Is there a way to force the input bindings to use Json.Net instead of System.Text.Jsoneither via DI in FunctionssStartup.Configure() or otherwise?

Map like for like using Unity IoC

Is there a quick way to map an ISomething to Something without doing so explicitly for every map?
So, instead of writing many of these:
container.RegisterType<ISomethinmg, Somethinmg>();
container.RegisterType<ISomethinmgElse, SomethinmgElse>();
Can I skip the registration, and just map them proper at runtime?

Customizing data annotation attributes for ASP.NET MVC

I would like to customize at runtime the attributes that MVC sees on a view model property. As far as I know, MVC relies internally on type descriptors to enumerate the attributes. Is there a way to hook a type descriptor somewhere to return a custom list of attributes for a property?
Is there a way to hook a type descriptor somewhere to return a custom
list of attributes for a property?
It depends. If you want to override the Data Annotations used by the metadata provider then you could write your own custom ModelMetadataProvider and replace the default one (DataAnnotationsModelMetadataProvider). This allows you to have a custom metadata provider for a given type and return this information at runtime.
If on the other hand you are doing validation, then you are a bit out of luck. For more flexibility I would recommend you using FluentValidation.NET instead of data annotations.

is there a way to populate an existing object from json using json.net based upon an interface

Looking for a way to do something like the following:
NewtonSoft.Json.JsonConvert.Populate<IMyContract>(jsonStr, currentObj);
where the json.net engine would only attempt to populate the properties that are identified in the IMyContract.
Any suggestions?
Maybe u can use
PopulateObject(String, Object, JsonSerializerSettings)
And use your own JsonSerializerSettings. See: http://james.newtonking.com/projects/json/help/html/T_Newtonsoft_Json_JsonSerializerSettings.htm
Try to use one of the delegates to restrict the properties that are set yourself. That can be based on an Interface, using reflection.
I don't know any other way. If you don't get it with the JsonSerializerSettings you can always write your own PopulateObject method using the Json framework.

how can i use structure map asp.net 3.5

I am new to the structure map but i want to use it in my asp.net site for dependency injection
can any one suggest me simple example to use structure map for the dependency injection
you will need to do something like this:-
StructureMapConfiguration
.ForRequestedType<IResourceA>()
.TheDefaultIsConcreteType<ResourceB>()
.CacheBy(InstanceScope.Singleton);
This tells StructureMap to inject ResourceB when there is a request for ResourceA.
Structure Map
You can configure programatically or via configuration file.
Programatical example (there are other ways):
StructureMap.StructureMapConfiguration.ForRequestedType<ISomething>().TheDefaultIsConcreteType<ConcreteSomething>();
then you can get an instance of the configured type using this sort of code:
//The concrete type will be ConcreteSomething
ISomething instance = ObjectFactory.GetInstance<ISomething>();
You can do it in a config file:
<StructureMap MementoStyle="Attribute">
<DefaultInstance PluginType="Blah.ISomething, Blah.SomethingDLL" PluggedType="Blah.Concrete.ConcreteSomething,Blah.ConcreteDLL"/>
</StructureMap>
and in the main method or Global.asax you can set this config by saying:
StructureMap.ObjectFactory.Initialize(x => { x.PullConfigurationFromAppConfig = true; });
and use it the same way as above:
ISomething instance = ObjectFactory.GetInstance<ISomething>();
If the concrete class has a constructor that needs instances injected in it, and you have those configured, the concrete types will get injected by the framework.
There are ways of passing parameters to constructors, dealing with Gereric types, creating named instances that are configured with specific constructor/property values. I use this framework and like it very much.

Resources