Automapper; mapping a string[] to Dictionary<MyEnum, string> - automapper

Here is what I am trying to do:
class Source
{
int MyProperty1 { get; set;}
....
string[] MyStrings { get; set;}
}
class Destination
{
int MyProperty1 { get; set;}
...
IDictionary<MyEnum, string> MyStrings {get;set;}
}
Here is the mapping:
Mapper.CreateMap<Source, Destination>();
Mapper.CreateMap<string[], IDictionary<MyEnum, string().
ConvertUsing<CustomArraytoDict>();
And the ITypeConverter implementation:
public class CustomArraytoDict : ITypeConverter<string[], IDictionary<MyEnum, string>>
{
public IDictionary<MyEnum, string> Convert(ResolutionContext context)
{
var periodNames = new Dictionary<MyEnum, string>();
//Create new Key Value pairs from
//context.SourceValue and add them to periodNames.
return periodNames;
}
}
My understanding is that "ConvertUsing()" should ensure that during mapping AutoMapper should use the reference for MyStrings dictionary as provided by CustomArraytoDict. However I do not see that happening. AutoMapper does seem to call correctly CustomArraytoDict during mappping of Source object to Destination object but the MyStrings reference in the Destination object remains the one created and initialized by the Destination constructor. The reference returned by CustomArraytoDict is not used.
What could I be doing wrong?

Related

Get Values from ReleaseData.Values

When executing the OpenScript method of my release script I want to store the indexfields, batchfields and variables to lists. I created a snippet for this
Dictionary<string, string> indexFields = new Dictionary<string, string>();
Dictionary<string, string> batchFields = new Dictionary<string, string>();
Dictionary<string, string> kofaxValues = new Dictionary<string, string>();
foreach (Value val in documentData.Values)
{
if (val.TableName.IsEmpty())
{
string sourceName = val.SourceName;
string sourceValue = val.Value;
switch (val.SourceType)
{
case KfxLinkSourceType.KFX_REL_INDEXFIELD:
indexFields.Add(sourceName, sourceValue);
break;
case KfxLinkSourceType.KFX_REL_VARIABLE:
kofaxValues.Add(sourceName, sourceValue);
break;
case KfxLinkSourceType.KFX_REL_BATCHFIELD:
batchFields.Add(sourceName, sourceValue);
break;
}
}
}
I want to do this because I need the field value. Each field name is unique so I can use it as a key.
When storing my custom properties to the ReleaseSetupData I can read them from the ReleaseData. Let's say two custom properties would return me the field name and the field type, so I know that the field is an IndexField and it's name is "MyIndexField".
I can use these information to access the Dictionary<string, string> indexFields and get the value from that Indexfield.
Currently I setup my ReleaseSetupData with this code
releaseSetupData.CustomProperties.RemoveAll();
// Save all custom properties here
releaseSetupData.CustomProperties.Add("myCustomProperty", "fooBar");
releaseSetupData.Links.RemoveAll();
foreach (IndexField indexField in releaseSetupData.IndexFields) // Save all IndexFields
{
releaseSetupData.Links.Add(indexField.Name, KfxLinkSourceType.KFX_REL_INDEXFIELD, indexField.Name);
}
foreach (BatchField batchField in releaseSetupData.BatchFields) // Save all BatchFields
{
releaseSetupData.Links.Add(batchField.Name, KfxLinkSourceType.KFX_REL_BATCHFIELD, batchField.Name);
}
foreach (dynamic batchVariable in releaseSetupData.BatchVariableNames) // Save all Variables
{
releaseSetupData.Links.Add(batchVariable, KfxLinkSourceType.KFX_REL_VARIABLE, batchVariable);
}
When the OpenScript method of my release script gets executed, the dictionaries (shown in the first snippet) stay empty. This is because documentData.Values is empty.
How can I fill documentData.Values?
You can't. The order of events is as follows:
OpenScript() is called - once per batch.
ReleaseDoc() is called - once per document
CloseScript() is called - once per batch.
The Values collection holds information specific to an individual document and as such will be empty during OpenScript(). Sometimes this isn't what you want - you may want to access another document's values, or exporting them all at once - e.g. in a single web service call.
Here's what I would recommend:
Create a wrapper class for Kofax' Document object. Here's my approach (only properties are shown). This class has a constructor that accepts a ReleaseData object as the single parameter, and all respective properties are populated in said contructor.
public class Document
{
public Dictionary<string, string> BatchFields { get; private set; }
public Dictionary<string, string> IndexFields { get; private set; }
public Dictionary<string,string> KofaxValues { get; set; }
public Dictionary<string, string> TextConstants { get; set; }
public Dictionary<string, string> CustomProperties { get; private set; }
public Dictionary<string,string> ConfigurationSettings { get; set; }
public List<Table> Tables { get; private set; }
private List<Column> Columns;
public List<string> ImageFileNames { get; private set; }
public string KofaxPDFFileName { get; private set; }
public string XdcFilePath { get; private set; }
public XDocument XDocument { get; private set; }
public string ImageFilePath { get; private set; }
public string KofaxPDFPath { get; private set; }
public string TextFilePath { get; private set; }
public byte[] BinaryImage { get; private set; }
public char CellSeparator { get; set; }
}
Then, during ReleaseDoc(), I would just add all of my Documents to a collection. Note that the connection documents is a defined as private in your ReleaseScript:
public KfxReturnValue ReleaseDoc()
{
documents.Add(new Document(DocumentData));
}
You can then decide when and where to export your data. It could be during the CloseScript() event as well, but keep in mind that sanity checks and potential exceptions related to document data (invalid index field values, et cetera) must be thrown during ReleaseDoc(). Using a custom wrapper class and a collection adds a lot of flexibility and features native to .NET to your Export Connector, such as LINQ - here's an example (this is impossible with Kofax' COM objects):
var noPdfs = documents.Where(x => x.KofaxPDFPath.Length == 0);

Avoiding flattening of property "Value" in AutoMapper

We are working with AutoMapper 4.1.1 (will soon update to 6, but we need some time to change the project) and have this issue: in the source class we have a property "int? MyProp" while in the destination we have string "MyPropValue", which should not be mapped from MyProp. AutoMapper however does map it when the value of MyProp is not null (using MyProp.Value, flattened). We could use Ignore() to ignore the mapping but as we have dozens of such properties, we are looking for a way to do it with one configuration, without breaking the rest of the mapping.
Here is some example code:
class Program
{
static void Main(string[] args)
{
var map = Mapper.CreateMap<Dto, Model>();
var dto = new Dto() {MyProp = 10};
var model = Mapper.Map<Dto, Model>(dto);
Console.WriteLine(model.MyPropValue); // MyPropValue should be empty but it gets mapped
}
}
public class Dto
{
public int? MyProp { get; set; }
}
public class Model
{
public string MyPropValue { get; set; }
}
Mapper.CreateMap<SourceType, DestinationType>()
.ForAllMembers(opt => opt.Condition(srs => !srs.IsSourceValueNull));
Allows you to skip all nulls.
Automapper skip null values with custom resolver

inherited class AutoMapper.AutoMapperMappingException

I am new at automapper and it is a very good stuff easy to use, but now I have a problem with it. Trying to convert my derived class to base and it gives me
AutoMapper.AutoMapperMappingException
Missing type map configuration or unsupported mapping.
Mapping types: ClientEventDb -> EventId
Database.ClientEventDb -> EventId
Destination path: ClientEvent
Source value:
Event:Login
Automapper wants to convert ClientEventDb to EventId? I don't understand why. EventId is an enum...
Please help me I have run out of ideas.
Here is the code which I run:
ClientEventDb[] edbl;
using (var context = new DbEntities())
{
edbl=context.Events.Take(1000).ToArray();
}
Mapper.CreateMap<ClientEventDb, ClientEvent>();
Console.WriteLine("hello");
return edbl.Select(edb => Mapper.Map<ClientEvent>(edb)).ToArray();
Here are my classes
[Table("events", Schema = "public")]
public class ClientEventDb : ClientEvent
{
public ClientEventDb(string userName, EventId happening, object userObject = null)
: base(userName, happening, userObject)
{
}
public ClientEventDb()
{
}
}
[ProtoContract]
[Table("events", Schema = "public")]
public class ClientEvent : ClientEventBase
{
[ProtoMember(1)]
[Column("username")]
public string UserName { get; private set; }
[ProtoMember(2)]
[Column("time")]
public DateTime DateTime { get; private set; }
[ProtoMember(3)]
[Key]
[Column("id")]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string Id { get; private set; }
[ProtoMember(4)]
[Column("data")]
public byte[] UserObject { get; set; }
public ClientEvent(string userName,EventId happening, object userObject=null) : base(happening)
{
UserName = userName;
DateTime = DateTime.Now;
//UserObject = null;
if (userObject!=null) throw new NotImplementedException();
}
public ClientEvent()
{
}
protected ClientEvent Clone()
{
return (ClientEvent)MemberwiseClone();
}
}
[ProtoContract]
[ProtoInclude(10, typeof(ClientEvent))]
public class ClientEventBase
{
[Column("eventid")]
[ProtoMember(1)]
public int EventIdValue { get; set; } //must be public because of entity framework
[NotMapped]
public EventId EventId
{
get { return (EventId) EventIdValue; }
set { EventIdValue = (int) value; }
}
public ClientEventBase(EventId eventId)
{
EventId = eventId;
}
public ClientEventBase()
{
}
public override string ToString()
{
return String.Format("Event:{0}",EventId);
}
}
public enum EventId
{
Login = 1,
Logout,
ExitApplication,
}
UPDATE
bugfix: ClientEvent [Key] attribute moved to id property
Solution was this (thx to stuartd):
ClientEventDb[] edbl;
using (var context = new DbEntities())
{
edbl=context.Events.ToArray();
}
Mapper.CreateMap<ClientEventDb, ClientEvent>().ConstructUsing((ClientEventDb src) => new ClientEvent());
return edbl.Select(Mapper.Map<ClientEvent>).ToArray();
AutoMapper is confused as its made to map between similar properties in different classes, you are using it incorrectly - you just need to go from the derived class to the base which does not require AutoMapper. You could use this to do what you need....
ClientEventDb[] edbl;
using (var context = new DbEntities())
{
edbl=context.Events.Take(1000).ToArray();
}
return edbl.Cast<ClientEvent>().ToList();
I'd be looking at why you even feel you need a derived ClientEventDb though - understand we dont have the whole picture here but it seems to do nothing in addition to what the base class already does.
The issue is that ClientEvent has two constructors but you have not told AutoMapper which to use.
If you want it to use your constructor with parameters, change your mapping code to this and it will work:
Mapper.CreateMap<ClientEventDb, ClientEvent>()
.ConstructUsing(src => new ClientEvent(src.UserName, src.EventId));
Or to make AutoMapper use the default constructor:
Mapper.CreateMap<ClientEventDb, ClientEvent>()
.ConstructUsing((ClientEventDb src) => new ClientEvent());

Get property values from a string using Reflection

I am new to this concept of reflection and finding problem in retrieving property value from a string. E.g.
I have a class Employee with following properties :
public string Name {get;set;}
public int Age {get;set;}
public string EmployeeID {get;set;}
string s = "Name=ABCD;Age=25;EmployeeID=A12";
I want to retrieve the value of each property from this string and create a new object of Employee with those values retrieved from the string for each field.
Can anyone please suggest how it can be done using reflection ??
//may be..
string s = "Name=ABCD;Age=25;EmployeeID=A12";
string[] words = s.Split(';');
foreach (string word in words)
{
string[] data = word.Split('=');
string _data = data[1];
Console.WriteLine(Name);
}
Here an example of how you maybe could do it
it used Reflection like you wanted ^^
using System;
using System.Collections.Generic;
using System.Reflection;
namespace replace
{
public class Program
{
private static void Main(string[] args)
{
var s = "Name=ABCD;Age=25;EmployeeID=A12";
var list = s.Split(';');
var dic = new Dictionary<string, object>();
foreach (var item in list)
{
var probVal = item.Split('=');
dic.Add(probVal[0], probVal[1]);
}
var obj = new MyClass();
PropertyInfo[] properties = obj.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
Console.WriteLine(dic[property.Name]);
if (property.PropertyType == typeof(Int32))
property.SetValue(obj, Convert.ToInt32(dic[property.Name]));
//else if (property.PropertyType== typeof(yourtype))
// property.SetValue(obj, (yourtype)dic[property.Name]);
else
property.SetValue(obj, dic[property.Name]);
}
Console.WriteLine("------------");
Console.WriteLine(obj.Name);
Console.WriteLine(obj.Age);
Console.WriteLine(obj.EmployeeID);
Console.Read();
}
}
public class MyClass
{
public string Name { get; set; }
public int Age { get; set; }
public string EmployeeID { get; set; }
}
}

Assign value to workflow variable

I have one question about Windows Workflow Foundation 4. I have an activity named PositionArrayActivity. This activity has a Sequence activity inside it. I need that in Execute method (during the workflow execution) oneFund variable mapping his value to PORTFOLIO_NAME that is created in Create method.... What have I to do to mapping oneFund value to PORTFOLIO_NAME at runtime?
Thanks
public sealed class PositionArrayActivity : NativeActivity, IActivityTemplateFactory
{
[Browsable(false)]
public Dictionary<string, List<Entity>> dictionary = new Dictionary<string, List<Entity>>();
public ActivityAction<Entity[]> Body { get; set; }
public Entity[] PositionList { get; set; }
public SqlDataReader rdr;
public SqlDataReader sdr;
public Entity[] positionArray;
public List<String> fundList;
public String oneFund { get; set; }
public String date { get; set; }
public List<Entity> listToArrayPositions;
protected override void CacheMetadata(NativeActivityMetadata metadata)
{
metadata.AddDelegate(Body);
}
protected override void Execute(NativeActivityContext context)
{
// A lot of code....
}
public Activity Create(DependencyObject target)
{
Variable<string> var = new Variable<string>
{
Name = "PORTFOLIO_NAME"
};
var fef = new PositionArrayActivity();
var aa = new ActivityAction<Entity[]>();
var da = new DelegateInArgument<Entity[]>();
da.Name = "positions";
fef.Body = aa;
aa.Argument = da;
aa.Handler = new Sequence
{
Variables = { var }
};
return fef;
}
}
You need to have an ActivityContext to set a variable value so first move the declaration of the var (did that name actually compile?) to a higher scope.
Then in Execute
var.Set(activityContext, oneFund);
One thing though, the oneFund property will only be set once at application startup so you may have some surprising results. If you wanted that to be for each instance, you need an inargument.

Resources