Can someone tell me if this is the correct way to check the current environment when using JHipster ?
public DwollaService(ApplicationProperties applicationProperties, Environment env) throws Exception {
this.dwolla = applicationProperties.dwolla;
this.env = env;
Collection<String> activeProfiles = Arrays.asList(env.getActiveProfiles());
Boolean isProd = activeProfiles.contains(JHipsterConstants.SPRING_PROFILE_PRODUCTION);
this.dwollaClient = new Dwolla(
this.dwolla.getKey(),
this.dwolla.getSecret(),
(isProd) ? DwollaEnvironment.PRODUCTION : DwollaEnvironment.SANDBOX
);
}
Your code works but following is the better way to identify prod environment(In fact spring deprecated checking active profiles based on strings).
isProd = env.acceptsProfiles(Profiles.of(JHipsterConstants.SPRING_PROFILE_PRODUCTION));
Related
I have a TypeConverter in which I'm using context.Mapper.Map() to map two subproperties.
The properties are the same Type and use the same (another) TypeConverter. However in one the properties I need to pass some IMappingOperationsOptions.
It looks like this (simplified):
public class MyTypeConverter : ITypeConverter<A, B>
{
public B Convert(A, B, ResolutionContext context)
{
var subProp1 = context.Mapper.Map<C>(B.SomeProp);
var subProp2 = context.Mapper.Map<C>(B.SomeOtherProp, ops => ops.Items["someOption"] = "someValue");
return new B
{
SubProp1 = subProp1,
SubProp2 = subProp2
};
}
}
This was working fine in AutoMapper 8.0.0 but I'm upgrading to AutoMapper 10.1.1 (last version with .NET framework support).
In this newer version of AutoMapper the overload method to pass IMappingOperationsOptions does not exist anymore.
I could (theoretically) solve this by injecting IMapper in the constructor of the TypeResolver and use that instead of the ResolutionContext's Mapper but that doesn't feel right.
At the moment I solved the issue by temporarily updating the ResolutionContext options, but that also doesn't really feel right.
var subProp1 = context.Mapper.Map<C>(B.SomeProp);
context.Options.Items["someOption"] = "someValue";
var subProp2 = context.Mapper.Map<C>(B.SomeOtherProp);
context.Options.Remove("someOption");
Casting ((IMapper)context.Mapper).Map() crashes so that's not an option either. Is there a more elegant way to achieve this?
Im using automapper to object conversion where source is table class and destination is property class.
I'm using .dml to connect database.
App type - Window
Using platform - VS-12 framework 4.5 , automapper version 4.2.1
Issues :- when convert single class object automapper successfully converted but when im using list then it return zero.
In Config class-
public static Initialize();
Mapper.CreateMap<Source, destination>().ReverseMap();
Mapper.CreateMap<List<Source>, List<destination>>().ReverseMap();
In code-
//It run successfully
Mapper.map(result, objdestination);
//It not run work and anot giving any exception
Mapper.map(listresult, listdestination);
Thanks in advance.
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap< Source, destination>().ReverseMap();
});
config.AssertConfigurationIsValid(); // check if configuration valid.
IMapper mapper = config.CreateMapper();
var appProduct = mapper.Map<List<destination>>(sourceObj);
I'm trying to get the hung of catel but have a problem.
Trying "Messaging via attribute" gets an compile error.
'Catel.MVVM.ViewModelBase.GetService(object)' is obsolete: 'GetService is no longer >recommended. It is better to inject all dependencies (which the TypeFactory fully supports) >Will be removed in version 4.0.0.'
private void OnCmdExecute()
{
var mediator = GetService<IMessageMediator>();
mediator.SendMessage("Test Value");
}
[MessageRecipient]
private void ShowMessage(string value)
{
var messageService = GetService<IMessageService>();
messageService.Show(value);
}
I'm using 3.9.
A hint and a code snippet whould be good help.
Thanks for your attention.
The GetService is marked obsolete. You have 2 options:
1) If you are using a view model, simply let the services be injected in the constructor:
private readonly IMessageMediator _messageMediator;
private readonly IMessageService _messageService;
public MyViewModel(IMessageMediator messageMediator, IMessageService messageService)
{
Argument.IsNotNull(() => messageMediator);
Argument.IsNotNull(() => messageService);
_messageMediator = messageMediator;
_messageService= messageService;
}
2) Use the GetDependencyResolver extension method:
var dependencyResolver = this.GetDependencyResolver();
var messageMediator = dependencyResolver.Resolve<IMessageMediator>();
Solution 1 is the recommended way.
Thanks for your answer.
I also found a good example in the "Catel.Examples" solution, link to download
I need to trace any complex (i.e. non-default) mappings in our project.
To achieve this, I'm using a custom value resolver, and publishing out a log event during resolution. As part of this message I'd like to know the destination member being mapped, which I was hoping to find in source.Context.MemberName - but this is always null.
ValueResolver:
public class Resolver : IValueResolver
{
public event MappingEventHandler MappingEvent;
public delegate void MappingEventHandler(MappingMessage m);
public ResolutionResult Resolve(ResolutionResult source)
{
var src = (SourceDTO)source.Context.SourceValue;
if (!String.IsNullOrWhiteSpace(src.Status) && src.Status == "Alert")
{
var newValue = source.Value + " - Fail";
var fieldName = source.Context.MemberName; //Always null
MappingEvent(new MappingMessage(fieldName , newValue));
return source.New(value, typeof(String));
}
return source;
}
}
... and its usage:
Resolver resolver = new Resolver();
//... subscribe to resolver events etc.
Mapper.CreateMap<SourceDTO, Common>()
.ForMember(dest => dest.ReferenceIdentifier
, opt => opt.ResolveUsing<Resolver>()
.FromMember(src => src.Reference)
.ConstructedBy(() => resolver)
I can see in the Automapper code that MemberName only returns if the PropertyMap is non-null, and since PropertyMap is null in this case, I'm not getting my MemberName back.
Is there a reason the PropertyMap isn't being defined in this here? There's a relevant candidate via source.Context.TypeMap.GetPropertyMaps(), but it's not being pushed into this context.
Any ideas? Perhaps there's a means of pulling the right PropertyMap out of the Context.TypeMap set?
Tried with the more recent Automapper build - looks like the problem has been resolved.
Version with issue: 2.1.266
Working version: 2.2.1
Also found it's a lot easier to use the following syntax to resolve from an existing instance:
Resolver resolver = new Resolver();
//... subscribe to resolver events etc.
Mapper.CreateMap<SourceDTO, Common>()
.ForMember(dest => dest.ReferenceIdentifier
, opt => opt.ResolveUsing(resolver)
.FromMember(src => src.Reference) )
I would like to externalize all my string constants in my groovy classes to a properies file, and then have them called from there. I have seen the example with the configslurper, want to know how can I make the design such that I just include/import/load the resource/property file in my class and call the property I want to on it..
something like this in my controller
include/load propertyfilename
if (propertyfilename.propertyname == mygroovyVar) {
....some code
}
i.e if possible I want to avoid using "(quotes) when retrieving properties anywhere
like the resource bundle setup for properties in spring mvc
Regards
Priyank
You can do something like this:
something.groovy:
def loadConfig(environment,settingFileName) {
levelConfig = new ConfigSlurper().parse(new File(settingFileName).toURI().toURL())."$environment"
}
To access it :
def configFile = loadConfig("alpha","c:\somewhere\something.properties"
assert configFile.currlevel = "alpha"
something.properties:
alpha {
currlevel = "alpha"
}
beta {
currlevel = "beta"
}
prod {
currlevel = "prod"
}
If you dont want the environment tweak it a bit. Hope this can help.