my first question that I can't find the exact answer elsewhere.
Using Automapper 4.2.1, Unity 4.01
I'm trying to have an Automapper profile call my service interface again to get a nested object to map to my destination property. Ultimately I would prefer to not use ResolveUsing, but if that works that is fine with me.
public class HeadlineMap : Profile
{
private readonly IDeserializeModels _deserialize;
public HeadlineMap(IDeserializeModels argDeserializeModels)
{
_deserialize = argDeserializeModels;
}
protected override void Configure()
{
CreateMap<LiveSiteHeadline, Headline>().
...
ForMember(t => t.VideoTitle, options => options.MapFrom(f => f.videoTitle)).
ForMember(t => t.Disclaimer, options => options.ResolveUsing<ResolveDisclaimerReference>().ConstructedBy(()=> new ResolveDisclaimerReference(_deserialize)));
}
}
public class ResolveDisclaimerReference : ValueResolver<LiveSiteHeadline, Object>
{
private readonly IDeserializeModels _deserialize;
public ResolveDisclaimerReference(IDeserializeModels argDeserializeModels)
{
_deserialize = argDeserializeModels;
}
protected override object ResolveCore(LiveSiteHeadline source)
{
return _deserialize.GetResponseObject(source.disclaimer.urlPattern, source.disclaimer.project, source.disclaimer.path);
}
}
I'm using the recommended 4.2.1 way of registering all of my maps within the Unity container:
var config = new MapperConfiguration(cfg =>
{
foreach (var profile in profiles)
{
cfg.AddProfile(profile);
}
});
container.RegisterType<MapperConfiguration>(new ContainerControlledLifetimeManager(),
new InjectionFactory(c => config))
.RegisterType<IMapper>(new InjectionFactory(c => c.Resolve<MapperConfiguration>().CreateMapper()));
But when I run the above I get -
"Missing type map configuration or unsupported mapping.".
Any ideas on what I'm missing? The other examples using ObjectFactory, Ninject don't seem to work as I must not be using those.
Related
I am using IStringLocalizer approach to localize my Blazor app as discussed here.
Injecting the IStringLocalizer on razor pages works great. I also need this to localize some services - whether scoped or even singleton services.
Using constructor injection to inject my IStringLocalizer service into the service works. However, when users change the language via UI, the service (whether singleton or scoped) keeps the initial IStringLocalizer - i.e. the one with the original language used when starting the app, not the updated language selected by the user.
What is the suggested approach to retrieve the updated IStringLocalizer from code?
EDIT
To prevent more details, here is some piece of code.
First, I add a Resources folder and create there a default LocaleResources.resx (with public modifiers) and a LocaleResources.fr.resx file, which contain the key-value pairs for each language.
Supported cultures are defined in the appsettings.json file as
"Cultures": {
"en-US": "English",
"fr": "Français (Suisse)",
...
}
In startup, I register the Resources folder and the supported cultures :
public void ConfigureServices(IServiceCollection services {
...
services.AddLocalization(options => options.ResourcesPath = "Resources");
...
services.AddSingleton<MySingletonService>();
services.AddScoped<MyScopedService>();
}
// --- helper method to retrieve the Cultures from appsettings.json
protected RequestLocalizationOptions GetLocalizationOptions() {
var cultures = Configuration.GetSection("Cultures")
.GetChildren().ToDictionary(x => x.Key, x => x.Value);
var supportedCultures = cultures.Keys.ToArray();
var localizationOptions = new RequestLocalizationOptions()
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures);
return localizationOptions;
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
...
app.UseRequestLocalization(GetLocalizationOptions());
...
app.UseEndpoints(endpoints => {
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
I created an empty LocaleResources.razor control at the root of the project (this is a trick used to inject a single resource file to all components).
I included a routing controller to change language :
[Route("[controller]/[action]")]
public class CultureController : Controller {
public IActionResult SetCulture(string culture, string redirectUri) {
if (culture != null) {
HttpContext.Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(
new RequestCulture(culture)));
}
return LocalRedirect(redirectUri);
}
}
And the language UI switcher looks like this (I use SyncFusion control here, but it could be any lookup actually, that shouldn't really matter)
#inject NavigationManager NavigationManager
#inject IConfiguration Configuration
<SfComboBox TValue="string" TItem="Tuple<string, string>" Placeholder="Select language" DataSource="#Cultures"
#bind-Value="selectedCulture" CssClass="lan-switch" Width="80%">
<ComboBoxFieldSettings Text="Item2" Value="Item1"></ComboBoxFieldSettings>
</SfComboBox>
<style>
.lan-switch {
margin-left: 5%;
}
</style>
#code {
string _activeCulture = System.Threading.Thread.CurrentThread.CurrentCulture.Name;
private string selectedCulture {
get => _activeCulture;
set {
_activeCulture = value;
SelectionChanged(value);
}
}
List<Tuple<string, string>> Cultures;
protected override void OnInitialized() {
var cultures = Configuration.GetSection("Cultures")
.GetChildren().ToDictionary(x => x.Key, x => x.Value);
Cultures = cultures.Select(p => Tuple.Create<string, string>(p.Key, p.Value)).ToList();
}
protected override void OnAfterRender(bool firstRender) {
if (firstRender && selectedCulture != AgendaSettings.SelectedLanguage) {
selectedCulture = AgendaSettings.SelectedLanguage;
}
}
private void SelectionChanged(string culture) {
if (string.IsNullOrWhiteSpace(culture)) {
return;
}
AgendaSettings.SelectedLanguage = culture;
var uri = new Uri(NavigationManager.Uri)
.GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped);
var query = $"?culture={Uri.EscapeDataString(culture)}&" +
$"redirectUri={Uri.EscapeDataString(uri)}";
NavigationManager.NavigateTo("/Culture/SetCulture" + query, forceLoad: true);
}
}
Finally, to the injection. I inject the IStringLocalizer to pages as follows and it works perfectly fine on razor controls:
#inject IStringLocalizer<LocaleResources> _loc
<h2>#_loc["hello world"]</h2>
Above, when I change language, the page displays the value in the corresponding resource file.
Now, to services: the MySingletonService and MyScopedService are registered at startup. They both have a constructor like
protected IStringLocalizer<LocaleResources> _loc;
public MySingletonService(IStringLocalizer<LocaleResources> loc) {
_loc = loc;
}
public void someMethod() {
Console.WriteLine(_loc["hello world"])
}
I run someMethod on a timer. Strangely, when I break on the above line, the result seems to oscillate : once it returns the default language's value, once the localized one...!
The answer to my question was: your code is correct!
The reason, I found out, is that I use a Scoped service that is started on the default App's start page:
protected async override Task OnAfterRenderAsync(bool firstRender) {
if (firstRender) {
MyScopedService.StartTimer();
}
await base.OnAfterRenderAsync(firstRender);
}
When users change language, the whole page is refreshed and a new instance of the scoped service is created and timer started. As my service did not implement IDisposable, the timer was not actually stopped.
So 2 solutions here:
use singleton services
make servcie disposable and ensure tasks are cancelled when service is disposed of.
I'm using MassTransit (5.5.5) with StructureMap (4.7) in a large code base, using Registry classes to keep my dependencies organized, following what's outlined here:
class BusRegistry : Registry
{
public BusRegistry()
{
For<IBusControl>(new SingletonLifecycle())
.Use(context => Bus.Factory.CreateUsingInMemory(x =>
{
x.ReceiveEndpoint("customer_update_queue", e => e.LoadFrom(context));
}));
Forward<IBusControl, IBus>();
}
}
// Snip...
public void CreateContainer()
{
_container = new Container(x => {
x.AddRegistry(new BusRegistry());
});
}
However, the LoadFrom extension method used in the call to ReceiveEndpoint is deprecated. So how is one supposed to use MassTransit with StructureMap Registry classes currently?
There probably isn't going to be a good answer, since StructureMap is on its way out. I ended up not using StructureMap for consumer configuration:
class BusRegistry : Registry
{
public BusRegistry()
{
For<IBusControl>(new SingletonLifecycle())
.Use(context => Bus.Factory.CreateUsingInMemory(x =>
{
x.ReceiveEndpoint("customer_update_queue", endpointCfg => {
endpointCfg.Consumer<FirstSubscriber>(
GetFirstSubscriberFactoryMethod()
);
endpointCfg.Consumer<SecondSubscriber>(
GetSecondSubscriberFactoryMethod()
);
// etc...
});
}));
Forward<IBusControl, IBus>();
}
}
...though I am using StructureMap to inject dependencies into the consumers.
documentation is very sparce and all i tried results in the deserializer injected but normal odata url's not working anymore.
https://github.com/OData/WebApi/issues/158 has solutions buut for 5.6.
The final relevant comment is:
#dbenzhuser - In that commit, look at ODataFormatterTests.cs for how
inject a custom deserializer/deserializer provider. You can still use
a custom DeserializerProvider but it's injected through DI instead of
injecting it through ODataMediaTypeFormatters.
which is quite meaningless. I tried the code there, but it breaks, as I said, the URL's.
Right now my Odata setup is simple:
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddOData();
\UnitTest\Microsoft.AspNet.OData.Test.Shared\Formatter\ODataFormatterTests.cs
has examples to inject them (like in lines 379-383)
config.MapODataServiceRoute("IgnoredRouteName", null, builder =>
builder.AddService(Microsoft.OData.ServiceLifetime.Singleton, sp => ODataTestUtil.GetEdmModel())
.AddService<ODataSerializerProvider>(ServiceLifetime.Singleton, sp => new CustomSerializerProvider())
.AddService<IEnumerable<IODataRoutingConvention>>(ServiceLifetime.Singleton, sp =>
ODataRoutingConventions.CreateDefaultWithAttributeRouting("IgnoredRouteName", config)));
but I seem unable to get this working without removing the core odata routing.
Anyone an idea how to use that for the current version without breaking the base functionality?
There are three steps if you want to maintain the base functionality:
Your DeserializerProvider implementation should default to the base implementation for all scenarios that your custom Deserializer can't manage. In the following example the custom deserializer only operates on Resources and not Sets:
public class EntityTypeDeserializerProvider : DefaultODataDeserializerProvider
{
private readonly DataContractDeserializer _dataContractDeserializer;
public EntityTypeDeserializerProvider(IServiceProvider rootContainer)
: base(rootContainer)
{
_dataContractDeserializer = new DataContractDeserializer(this);
}
public override ODataEdmTypeDeserializer GetEdmTypeDeserializer(IEdmTypeReference edmType)
{
if(edmType.Definition.TypeKind == EdmTypeKind.Complex || edmType.Definition.TypeKind == EdmTypeKind.Entity)
return _dataContractDeserializer;
else
return base.GetEdmTypeDeserializer(edmType);
}
}
As with the provider your custom _Deserializer should call through to the base implementation for everything that you do not need to customize. In the following implementation we are only trying to enforce the Order of the properties that are deserialized as well as calling the DataContract OnDeserializing and OnDeserialized methods, the rest of the deserialization is unaffected:
/// <summary>
/// OData serializer that oberys the DataMember Order Attribute and OnDeserializing and OnDeserialized attributes on the resource definition
/// </summary>
public class DataContractDeserializer : ODataResourceDeserializer
{
public DataContractDeserializer(ODataDeserializerProvider provider)
: base(provider) { }
public override object CreateResourceInstance(IEdmStructuredTypeReference structuredType, ODataDeserializerContext readContext)
{
var resource = base.CreateResourceInstance(structuredType, readContext);
var type = resource.GetType();
if(type.GetCustomAttributesData().Any(x => x.AttributeType == typeof(DataContractAttribute)))
{
// manually call OnDeserializing
var init = type.GetMethods(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic).FirstOrDefault(x => x.GetCustomAttributesData().Any(a => a.AttributeType == typeof(OnDeserializingAttribute)));
if(init != null)
{
init.Invoke(resource, new object[] { new StreamingContext(StreamingContextStates.Remoting, readContext ) });
}
}
return resource;
}
public override object ReadResource(ODataResourceWrapper resourceWrapper, IEdmStructuredTypeReference structuredType, ODataDeserializerContext readContext)
{
var resource = base.ReadResource(resourceWrapper, structuredType, readContext);
var type = resource.GetType();
if (type.GetCustomAttributesData().Any(x => x.AttributeType == typeof(DataContractAttribute)))
{
// manually call OnDeserialized
var final = type.GetMethods(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic).FirstOrDefault(x => x.GetCustomAttributesData().Any(a => a.AttributeType == typeof(OnDeserializedAttribute)));
if (final != null)
{
final.Invoke(resource, new object[] { new StreamingContext(StreamingContextStates.Remoting, readContext) });
}
}
return resource;
}
public override void ApplyStructuralProperties(object resource, ODataResourceWrapper resourceWrapper, IEdmStructuredTypeReference structuredType, ODataDeserializerContext readContext)
{
var type = resource.GetType();
var memberDescriptors = type.GetProperties().Where(x => x.HasAttribute<DataMemberAttribute>());
if (memberDescriptors.Any())
{
var orderedProperties = from p in resourceWrapper.Resource.Properties
let clsProperty = memberDescriptors.FirstOrDefault(m => m.Name == p.Name)
let memberAtt = (DataMemberAttribute)(clsProperty?.GetCustomAttributes(true).FirstOrDefault(a => a.GetType() == typeof(DataMemberAttribute)))
orderby (memberAtt?.Order).GetValueOrDefault()
select p;
foreach (var property in orderedProperties)
{
ApplyStructuralProperty(resource, property, structuredType, readContext);
}
}
else
base.ApplyStructuralProperties(resource, resourceWrapper, structuredType, readContext);
}
}
Finally, You need to replace the default DeserializerProvider registration with your own, the following is an example of an overload to MapODataServiceRoute that registers the DeserializerProvider from the previous 2 examples.
I have commented out an example of registering a specific SerializerProvider
private static ODataRoute MapODataServiceRoute(this HttpConfiguration configuration, string routeName,
string routePrefix, IEdmModel model, ODataBatchHandler batchHandler = null, ODataUriResolver uriResolver = null, IList<IODataRoutingConvention> routingConventions = null)
{
return configuration.MapODataServiceRoute(routeName, routePrefix, builder =>
builder
.AddService(ServiceLifetime.Singleton, sp => model)
//.AddService<ODataSerializerProvider>(ServiceLifetime.Singleton, sp => new DefaultODataSerializerProvider(sp))
.AddService<ODataDeserializerProvider>(ServiceLifetime.Singleton, sp => new EntityTypeDeserializerProvider(sp))
.AddService(ServiceLifetime.Singleton, sp => batchHandler ?? new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer))
.AddService(ServiceLifetime.Singleton, sp => uriResolver ?? new ODataUriResolver())
.AddService<IEnumerable<IODataRoutingConvention>>(ServiceLifetime.Singleton, sp =>
routingConventions ??
ODataRoutingConventions.CreateDefaultWithAttributeRouting(routeName, configuration)
)
);
}
Need some help here please ...
I'm looking at the example "graphql-dotnet/server" where the exposed objects contains just plain properties. But what if I need to resolve a property and get data from a repository, how can I get hold of the respository class in the Type-class?
Example:
The sample has a ChatQuery exposing "messages".
public ChatQuery(IChat chat)
{
Field<ListGraphType<MessageType>>("messages", resolve: context => chat.AllMessages.Take(100));
}
Instance "chat" is the repository here and is serving data (messages) via chat.AllMessages.
Let's say that a message has a list of viewers. Then I need to resolve that list from a repository. This is done in the other example "graphql-dotnet/examples" where the "StarWars/Types/StarWarsCharacter.cs" has a list of friends and the "StarWars/Types/HumanType" has the repository (StarWarsData) inserted in the constructor and can be used in the resolve method for "friends":
public class HumanType : ObjectGraphType<Human>
{
public HumanType(StarWarsData data)
{
Name = "Human";
Field(h => h.Id).Description("The id of the human.");
Field(h => h.Name, nullable: true).Description("The name of the human.");
Field<ListGraphType<CharacterInterface>>(
"friends",
resolve: context => data.GetFriends(context.Source)
);
Field<ListGraphType<EpisodeEnum>>("appearsIn", "Which movie they appear in.");
Field(h => h.HomePlanet, nullable: true).Description("The home planet of the human.");
Interface<CharacterInterface>();
}
}
BUT, doing the same thing in the server sample won't work.
public class MessageType : ObjectGraphType<Message>
{
public MessageType(IChat chat)
{
Field(o => o.Content);
Field(o => o.SentAt);
Field(o => o.From, false, typeof(MessageFromType)).Resolve(ResolveFrom);
Field<ListGraphType<Viewer>>(
"viewers",
resolve: context => chat.GetViewers(context.Source)
);
}
private MessageFrom ResolveFrom(ResolveFieldContext<Message> context)
{
var message = context.Source;
return message.From;
}
}
When I add the chat repository to the constructor in MessageType it fails.
I'm obviously missing something here; why isn't Dependency Injection injecting the chat instance into the MessageType class in the "graphql-dotnet/server" project?
But it works in the "graphql-dotnet/examples" project.
Best,
Magnus
To use DI you need to pass a dependency resolver in the constructor of your Schema class. The default resolver uses Activator.CreateInstance, so you have to teach it about the Container you are using.
services.AddSingleton<IDependencyResolver>(
s => new FuncDependencyResolver(s.GetRequiredService));
IDependecyResolver is an interface in the graphql-dotnet project.
public class StarWarsSchema : Schema
{
public StarWarsSchema(IDependencyResolver resolver)
: base(resolver)
{
Query = resolver.Resolve<StarWarsQuery>();
Mutation = resolver.Resolve<StarWarsMutation>();
}
}
https://github.com/graphql-dotnet/examples/blob/bcf46c5c502f8ce75022c50b9b23792e5146f6d2/src/AspNetCore/Example/Startup.cs#L20
https://github.com/graphql-dotnet/examples/blob/bcf46c5c502f8ce75022c50b9b23792e5146f6d2/src/StarWars/StarWarsSchema.cs#L6-L14
I am trying to leverage the Automapper for mapping the controller methods to the viewmodel properties in an asp.net mvc project.
I analyzed few articles about Automapper and found that it is wonderful object to object mapper between complex model object and viewmodel object.
I have code in a controller: CustomersController.cs
[Authorize(Roles = "Administrator")]
public ActionResult Index()
{
var user = _userService.GetUser(_profile.UserName);
if (!user.IsActive)
return RedirectToAction("");
var clientGroups = new List<ClientGroup>();
var model = new CustomerGroupsIndexViewModel()
{
CustomerGroupUsersUrl = Url.RouteUrl<CustomerGroupsController>(c => c.GetUsersForCustomerGroup(null, null, null, 0, 0)),
CustomerGroupByAreaUrl = Url.RouteUrl<CustomerGroupsController >(c => c.GetAreaDetailsForCustomerGroup(null, null, null, 0, 0)),
CheckLoginNameUrl = Url.RouteUrl<UsersController>(c => c.CheckLoginName(null)),
ResetUserUrl = Url.RouteUrl<UsersController>(c => c.ResetPassword(null)),
GetSelectOptionsForCustomerGroupUrl = Url.RouteUrl<ClientGroupsController>(c => c.GetSelectOptionsForCustomerGroup(null,null)),
FindUsersMatchingTermUrl = Url.RouteUrl<UsersController>(c => c.FindUsersMatchingWithLoginName(null)),
NumberOfTestTaken = _scanService.GetCustomerForUser(user).Count(),
RefreshCustomerGroupUrl = Url.RouteUrl<CustomerGroupsController >(c => c.RefreshClientGroup()),
};
Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
return View("CustomerGroupIndex", model);
}
I have such methods across the project code base.
Can anyone help me how can I use Automapper efficiently here?
Thanks & Regards,
Santosh Kumar Patro
I 'll explain it very briefly
Create configuration for each Model and related ViewModel like this
Interface for configuration:
interface IGlobalConfiguration
{
void Configure();
}
Class for configuration:
public class AutoMapperViewModelConfiguration : IGlobalConfiguration
{
public void Configure()
{
Mapper.CreateMap<Model1,ViewModel1>();
Mapper.CreateMap<ViewModel1,Model1>()
.ForMember(x => x.ModelMember1, y => y.Ignore());//Ignore if not required
}
}
Create a class and method like this:
public class GlobalConfigurationModule
{
private readonly Assembly assembly;
public GlobalConfigurationModule()
: this(Assembly.GetExecutingAssembly())
{
}
public GlobalConfigurationModule(Assembly assembly)
{
this.assembly = assembly;
}
public void Load()
{
var ins = from x in assembly.GetExportedTypes()
where x.GetInterfaces().Contains(typeof(IGlobalConfiguration))
select Activator.CreateInstance(x) as IGlobalConfiguration;
foreach (var config in ins)
{
config.Configure();
}
}
}
Call the Load method in Global.asax
protected void Application_Start()
{
new GlobalConfigurationModule().Load();
}
When you require to map a Model to ViewModel use this Mapper.Map() method like this:
Mapper.Map(model1Object, viewModel1Object);
Edit:
If you need the mapping should be done from a method you can create mapping like this.
Mapper.CreateMap<Source, Destination>()
.ForMember(dest => dest.SomeValue,
opt => opt.MapFrom(src => src.GetSomeValue()))
You cannot map a method returns void.
Cheers