Codeeffects Linq Error: E106 The given ruleset does not contain any rules with type System.Xml.Linq.XElement - rule-engine

When I try to the following LINQ Filter
var productsInfo = from product in productsElement.Descendants("product").Filter(rule)
from photo in product.Descendants("photo")
from parameter in product.Descendants("parameter")
let id = product.Attribute("id")
let addr = photo.Attribute("addr")
let name = parameter.Attribute("name")
select new { ID = id.Value, Addr = addr.Value, Name = name.Value };
I get the following error:
The given ruleset does not contain any rules with type
System.Xml.Linq.XElement, System.Xml.Linq, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089 (Error E106)
My rule:
<?xml version="1.0" encoding="utf-8"?><codeeffects xmlns="http://codeeffects.com/schemas/rule/41" xmlns:ui="http://codeeffects.com/schemas/ui/4"><rule id="09973a56-3d6a-4616-ae1c-40d0d17e95b9" webrule="4.3.6.7" utc="2017-07-24T10:07:08.6346" type="testSlimWebRule.products, testSlimWebRule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" eval="true"><definition><condition type="equal"><property name="AllProducts.product.id" /><value type="numeric">1</value></condition></definition><format><lines /></format></rule></codeeffects>
The XML:
XDocument productsElement = XDocument.Parse(#"<products>
<AllProducts>
<product id='1'>
<photo addr='1.jpg'/>
<parameter name='name'/>
</product>
<product id='2'>
<photo addr='2.jpg'/>
<parameter name='Kenneth'/>
</product>
</AllProducts>
</products> ");
The products class generated using Visual Studio "Insert as XML class":
namespace testSlimWebRule
{
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class products
{
private productsAllProducts allProductsField;
/// <remarks/>
public productsAllProducts AllProducts
{
get
{
return this.allProductsField;
}
set
{
this.allProductsField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class productsAllProducts
{
private productsAllProductsProduct productField;
/// <remarks/>
public productsAllProductsProduct product
{
get
{
return this.productField;
}
set
{
this.productField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class productsAllProductsProduct
{
private productsAllProductsProductPhoto photoField;
private productsAllProductsProductParameter parameterField;
private byte idField;
/// <remarks/>
public productsAllProductsProductPhoto photo
{
get
{
return this.photoField;
}
set
{
this.photoField = value;
}
}
/// <remarks/>
public productsAllProductsProductParameter parameter
{
get
{
return this.parameterField;
}
set
{
this.parameterField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public byte id
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class productsAllProductsProductPhoto
{
private string addrField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string addr
{
get
{
return this.addrField;
}
set
{
this.addrField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class productsAllProductsProductParameter
{
private string nameField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
}
}
The ASP.net part:
<rule:RuleEditor ID="ruleEditor" runat="server"
Mode="Filter"
ShowToolBar="false"
SourceAssembly="testSlimWebRule"
SourceType="testSlimWebRule.products" />
I have tried several combinations of setting the filter, but just can't find a solution.
What am I missing?

The Filter() extension takes a collection of objects and evaluates them against a given rule individually. The rule engine takes a type of an element of the collection and uses it to compile a lambda with a parameter of that type.
List<MyClass> list = new List<MyClass>();
list.Filter(rule);
In this example the rule is of type MyClass and it gets applied to each object in the list similar to:
Evaluator<MyClass> ev = new Evaluator<MyClass>(rule);
foreach (var item in list)
{
ev.Evaluate(item);
}
You may read more on CodeEffects documentation page: Rule-Based Data Filtering Using LINQ to Object Provider.
In your example you made few mistakes:
You generate classes for XML elements, but don't use them.
The auto-generated classes incorrectly map the AllProduct field. It is supposed to be an array of productsProduct.
You apply a filter to a collection of XElement objects but the rule has type testSlimWebRule.products. It should be XElement in this case.
The example below will demonstrate four possible options, with the first one being the best.
Main
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using System.Xml.Serialization;
using CodeEffects.Rule.Core;
namespace testSlimWebRule
{
class Program
{
/* Output:
* Option A: 1, Bob, 1.jpg
* Option B: 1, Bob, 1.jpg
* Option C: 1, Bob, 1.jpg
* Option D: 2, Kenneth, 2.jpg
*/
static void Main(string[] args)
{
string xml =
#"<products>
<AllProducts>
<product id='1'>
<photo addr='1.jpg'/>
<parameter name='Bob'/>
</product>
<product id='2'>
<photo addr='2.jpg'/>
<parameter name='Kenneth'/>
</product>
</AllProducts>
</products>";
XDocument dom = XDocument.Parse(xml);
products products;
//You need to load auto-generated classes. I prefer serialization. You may parse XML elements individually.
using (var xmlReader = dom.CreateReader())
{
var serializer = new XmlSerializer(typeof(products));
products = (products)serializer.Deserialize(xmlReader);
}
string productsProductRule = File.ReadAllText("rule1.xml");
//A: Filter productsProduct[]; result is IEnumerable<productsProduct>.
//This rule evaluates objects of the productsProduct type.
var filteredProducts = products.AllProducts.Filter(productsProductRule);
foreach (var product in filteredProducts)
Console.WriteLine("Option A: {0}, {1}, {2}", product.id, product.parameter.name, product.photo.addr);
string xElementRule = File.ReadAllText("rule2.xml");
//B: Filter IEnumerable<XElement>; result is IEnumerable<XElement>.
//This rule evaluates objects of the XElement type.
var filteredElements = dom.Descendants("product").Filter(xElementRule);
foreach (var element in filteredElements)
Console.WriteLine("Option B: {0}, {1}, {2}", element.Attribute("id").Value, element.Element("parameter").Attribute("name").Value, element.Element("photo").Attribute("addr").Value);
//C: Filter IEnumerable<XElement>; result is IEnumerable<'a> (anonymous)
//This rule also evaluates objects of the XElement type.
var productsInfo = from product in dom.Descendants("product").Filter(xElementRule)
from photo in product.Descendants("photo")
from parameter in product.Descendants("parameter")
let id = product.Attribute("id")
let addr = photo.Attribute("addr")
let name = parameter.Attribute("name")
select new
{
ID = id.Value,
Addr = addr.Value,
Name = name.Value
};
foreach (var info in productsInfo)
Console.WriteLine("Option C: {0}, {1}, {2}", info.ID, info.Name, info.Addr);
string anonymousRule = File.ReadAllText("rule3.xml");
//D: Filter IEnumerable<'a>; result is IEnumerable<'a>
//This rule evaluates objects of the anonymous type 'a with properties ID, Addr, and Name.
var productsInfo2 = (from product in dom.Descendants("product")
from photo in product.Descendants("photo")
from parameter in product.Descendants("parameter")
let id = product.Attribute("id")
let addr = photo.Attribute("addr")
let name = parameter.Attribute("name")
select new
{
ID = id.Value,
Addr = addr.Value,
Name = name.Value
})
.Filter(anonymousRule);
foreach (var info in productsInfo2)
Console.WriteLine("Option D: {0}, {1}, {2}", info.ID, info.Name, info.Addr);
}
}
}
Auto-generated classes
You need re-paste your XML example to generate proper array fields. The one you have was generated with XML sample that only had one record. However to filter you need a collection.
using System;
namespace testSlimWebRule
{
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class products
{
private productsProduct[] allProductsField;
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("product", IsNullable = false)]
public productsProduct[] AllProducts
{
get
{
return this.allProductsField;
}
set
{
this.allProductsField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class productsProduct
{
private productsProductPhoto photoField;
private productsProductParameter parameterField;
private byte idField;
/// <remarks/>
public productsProductPhoto photo
{
get
{
return this.photoField;
}
set
{
this.photoField = value;
}
}
/// <remarks/>
public productsProductParameter parameter
{
get
{
return this.parameterField;
}
set
{
this.parameterField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public byte id
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class productsProductPhoto
{
private string addrField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string addr
{
get
{
return this.addrField;
}
set
{
this.addrField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class productsProductParameter
{
private string nameField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
}
}
rule1.xml
This rule has proper type testSlimWebRule.productsProduct. It gets evaluated against each element in the auto-generated array of testSlimWebRule.productsProduct.
<?xml version="1.0" encoding="utf-8"?>
<codeeffects xmlns="http://codeeffects.com/schemas/rule/41" xmlns:ui="http://codeeffects.com/schemas/ui/4">
<rule id="09973a56-3d6a-4616-ae1c-40d0d17e95b9" webrule="4.3.6.7" utc="2017-07-24T10:07:08.6346" type="testSlimWebRule.productsProduct, testSlimWebRule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" eval="true">
<definition>
<condition type="equal">
<property name="id" />
<value type="numeric">1</value>
</condition>
</definition>
</rule>
</codeeffects>
rule2.xml
This rule is the way you would have. It is applied to objects of type System.Xml.Linq.XElement. As such it can only operate on properties and methods of that type, i.e. you don't get your custom fields id, addr, name, etc.
<?xml version="1.0" encoding="utf-8"?>
<codeeffects xmlns="http://codeeffects.com/schemas/rule/41" xmlns:ui="http://codeeffects.com/schemas/ui/4">
<rule id="e38da351-1190-47fb-b99b-d06787c9a459" webrule="4.3.6.7" utc="2017-07-24T10:07:08.6346" type="System.Xml.Linq.XElement, System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" eval="true">
<definition>
<condition type="equal">
<property name="FirstAttribute.Value" />
<value>1</value>
</condition>
</definition>
</rule>
</codeeffects>
rule3.xml
This rule does not have any type. Instead it receives whatever type it is evaluated against. In the example it gets anonymous type 'a, so it can operate on properties ID, Name, and Addr.
<?xml version="1.0" encoding="utf-8"?>
<codeeffects xmlns="http://codeeffects.com/schemas/rule/41" xmlns:ui="http://codeeffects.com/schemas/ui/4">
<rule id="7d72463f-5ae2-4617-a2bf-fd605fcb4f54" webrule="4.3.6.7" utc="2017-07-24T10:07:08.6346" type="" eval="true">
<definition>
<condition type="startsWith">
<property name="Addr" />
<value>2</value>
</condition>
</definition>
</rule>
</codeeffects>

Related

None of mybatis typehandler getResult is entered

I think I'm seeking for trouble for myself using complex structure but I need to get over this problem.
I have object nested in object, then the object list
Here's the base Entity
#Data
public class BaseEntity {
private Long id;
private String name;
private String description;
}
Here's my impactEntity built on the top of base Entity
#Data
public class ImpactEntity implements Structable{
private String ref;
private String ASOF;
private Long deliverTime;
private String SLAMissed;
private BaseEntity client;
private BaseEntity application;
private BaseEntity deliverable;
}
Here's my real final object:
#Data
public class IncidentEntity {
private String PID;
private String ID;
private String NID;
private String REF;
private String RREF;
private String ASOF;
private Long StartTime;
private Long endTime;
private String description;
private String active;
private Long createAt;
private List<ImpactEntity> impacts;
}
Here's my incidentMapper:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.well.valley.mapper.IncidentMapper" >
<resultMap id="IncidentEntity" type="org.well.valley.entity.IncidentEntity">
<result column="PID" jdbcType="VARCHAR" property="PID" />
<result column="ID" jdbcType="VARCHAR" property="ID" />
<result column="NID" jdbcType="VARCHAR" property="NID" />
<result column="REF" jdbcType="VARCHAR" property="REF" />
<result column="RREF" jdbcType="VARCHAR" property="RREF" />
<result column="ASOF" jdbcType="VARCHAR" property="ASOF" />
<result column="STARTTIME" jdbcType="INTEGER" property="startTime" />
<result column="ENDTIME" jdbcType="INTEGER" property="endTime" />
<result column="DESCRIPTION" jdbcType="VARCHAR" property="description" />
<result column="ACTIVE" jdbcType="VARCHAR" property="active" />
<result column="CREATEAt" jdbcType="INTEGER" property="createAt" />
<result property="impacts" column="IMPACTS" typeHandler="org.well.valley.utils.typehandlers.OracleTypeHandler" />
</resultMap>
<select id="get" statementType="CALLABLE" parameterType="org.well.valley.entity.IncidentEntity">
call get_incidents(
#{ASOF, jdbcType=VARCHAR},
#{ref, jdbcType=VARCHAR},
#{active, jdbcType=VARCHAR},
#{outParam.c1, mode=OUT, jdbcType=CURSOR, javaType=java.sql.ResultSet, resultMap=IncidentEntity}
)
</select>
</mapper>
I'm using a customized typeHandler to deal with
<result property="impacts" column="IMPACTS" typeHandler="org.well.valley.utils.typehandlers.OracleTypeHandler" />
Here's my full body of the OracleTypeHandler. I want to make it very generic and be able to deal with anything, including something like List although I haven't started the code to deal with the List in the resultset yet.
public class OracleTypeHandler<T> extends BaseTypeHandler<T> {
private Logger logger = LoggerFactory.getLogger(getClass());
private ObjectMapper objectMapper = new ObjectMapper();
private DBMapperBean getDBMapperBean() {
return SpringContext.getBean(DBMapperBean.class);
}
private Class<T> type;
protected int getIndex(ResultSetMetaData data, String columnName) throws SQLException {
logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>11111111111111111111111111");
for (int i = 1; i <= data.getColumnCount(); i++) {
String c = data.getColumnName(i);
if(columnName.equals(c))
{
return i;
}
}
return -1;
}
public OracleTypeHandler(Class<T> type) {
if (type == null) {
throw new IllegalArgumentException("Type argument cannot be null");
}
this.type = type;
logger.info(">>>>>>>>>>>>>>>>>>>>Type is:{}", type);
}
#Override
public void setNonNullParameter(PreparedStatement ps, int index, T parameter, JdbcType jdbcType)
throws SQLException {
logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>333333333333333333333333");
Connection conn = ps.getConnection();
if(parameter.getClass().isArray()) {
if(parameter instanceof String[]) {
Array array = ((OracleConnection) conn).createOracleArray("STRINGARRAY", parameter);
ps.setArray(index, array);
array.free();
}
}
}
#Override
public T getNullableResult(ResultSet resultSet, String columnName) throws SQLException {
ResultSetMetaData metaData = resultSet.getMetaData();
logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>444444444444444444444444444444");
int index = getIndex(metaData, columnName);
String columnClassName = metaData.getColumnClassName(index);
if(columnClassName.equals("oracle.jdbc.OracleStruct"))
{
java.sql.Struct o = (java.sql.Struct)resultSet.getObject(index);
return extractObject(o);
}else if(columnClassName.equals("oracle.jdbc.OracleArray"))
{
Array array = resultSet.getArray(columnName);
Object r = array.getArray();
array.free();
return (T) r;
}
return null;
}
private String typeNameConvert(String type, String target)
{
if(target.equals("DBTYPE"))
{
return type.concat("TYPE");
}else
{
return type.substring(0, type.length()-4);
}
}
private T extractObject(Struct o) throws SQLException {
String typeName = o.getSQLTypeName();
typeName = typeName.substring(typeName.lastIndexOf(".")+1);DBMapperBean dbMapperBean = getDBMapperBean();
typeName = typeNameConvert(typeName, "JAVATYPE");
if(typeName.endsWith("ARRAY"))
{
switch (typeName){
case "STRINGARRAY":
break;
}
}else {
HashMap <String, Class> map = dbMapperBean.getMap();
try {
Class<?> cls = (Class<?>) map.get(typeName);
Structable obj = (Structable) cls.getDeclaredConstructor().newInstance();
Object[] attrs = o.getAttributes();
List<Field> fields = obj.getOrderedField(obj.getClass().getDeclaredFields());
int i = 0;
for (Field f : fields) {
f.setAccessible(true);
Object source = attrs[i];
if(source == null)
{
f.set(obj, null);
}
else {
Class targetType = f.getType();
Class sourceType = source.getClass();
if (Number.class.isAssignableFrom(targetType) && Number.class.isAssignableFrom(sourceType)) {
if (targetType == Long.class) {
source = Long.parseLong(source.toString());
}
}
try {
f.set(obj, source);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
i++;
}
return (T) obj;
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
return null;
}
#Override
public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>66666666666666666");
Struct struct =(java.sql.Struct) rs.getObject(columnIndex);
return extractObject(struct);
}
#Override
public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>7777777777777777777");
Struct struct =(java.sql.Struct) cs.getObject(columnIndex);
return extractObject(struct);
}
}
So even before the DB stored procedure is called, the constructor of the OracleTypeHandler is called, which is expected, as it's to build up a List of the object of objects
19:58:28.439 INFO o.w.v.u.t.OracleTypeHandler - >>>>>>>>>>>>>>>>>>>>Type is:class org.well.valley.entity.BaseEntity
19:58:33.692 INFO o.w.v.u.t.OracleTypeHandler - >>>>>>>>>>>>>>>>>>>>Type is:class org.well.valley.entity.BaseEntity
20:02:43.477 INFO o.w.v.u.t.OracleTypeHandler - >>>>>>>>>>>>>>>>>>>>Type is:class org.well.valley.entity.BaseEntity
20:02:47.161 INFO o.w.v.u.t.OracleTypeHandler - >>>>>>>>>>>>>>>>>>>>Type is:class org.well.valley.entity.BaseEntity
20:03:00.370 INFO o.w.v.u.t.OracleTypeHandler - >>>>>>>>>>>>>>>>>>>>Type is:class [Ljava.lang.String;
20:03:16.080 INFO o.w.v.u.t.OracleTypeHandler - >>>>>>>>>>>>>>>>>>>>Type is:interface java.util.List
However, after my DB stored procedure is called, none of the 3 getNullableResult methods in the typehandler is entered, while I would expect for whatever result set is retrieved one of them should be entered. To be more specific I think this one should be entered, as this is the one to retrieve the result using column name. I was expecting to do some job inside this method. However, it's not entered, and returned an empty List(but if I call the procedure in DB directly I got the result)
public T getNullableResult(ResultSet resultSet, String columnName) throws SQLException
So - can someone please advise what's the reason the getNullableResult methods were not entered? Which typehandler Mybatis us in this case to deal with my impactList?
Thanks a million

Lost attribute after serialization while building XML request

I created the request object from the class of my web service reference and assigned the enum value to the property of object.
SWS.QueueAccessLLSRQ.QueueAccessRQ request = new SWS.QueueAccessLLSRQ.QueueAccessRQ();
request.Version = "2.0.9";
request.Navigation = new SWS.QueueAccessLLSRQ.QueueAccessRQNavigation() { Action = SWS.QueueAccessLLSRQ.QueueAccessRQNavigationAction.I };
I'm expecting to have the XML request with attribute "Action" inside Navigation node like below:
<QueueAccessRQ xmlns="http://webservices.sabre.com/sabreXML/2011/10" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Version="2.0.9"> <Navigation Action="QR"/></QueueAccessRQ>
But after serialization I'm getting the next XML request without "Action" attribute.
<?xml version="1.0"?><QueueAccessRQ Version="2.0.9" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Navigation xmlns="http://webservices.sabre.com/sabreXML/2011/10"/></QueueAccessRQ>
Also, below are classes from web service reference that I'm using in my request:
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.7.2117.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://webservices.sabre.com/sabreXML/2011/10")]
public partial class QueueAccessRQ {
private QueueAccessRQNavigation navigationField;
private QueueAccessRQQueueIdentifier queueIdentifierField;
private QueueAccessRQSelection[] selectionField;
private bool returnHostCommandField;
private bool returnHostCommandFieldSpecified;
private System.DateTime timeStampField;
private bool timeStampFieldSpecified;
private string versionField;
public QueueAccessRQ() {
this.versionField = "2.0.9";
}
/// <remarks/>
public QueueAccessRQNavigation Navigation {
get {
return this.navigationField;
}
set {
this.navigationField = value;
}
}
/// <remarks/>
public QueueAccessRQQueueIdentifier QueueIdentifier {
get {
return this.queueIdentifierField;
}
set {
this.queueIdentifierField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Selection")]
public QueueAccessRQSelection[] Selection {
get {
return this.selectionField;
}
set {
this.selectionField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public bool ReturnHostCommand {
get {
return this.returnHostCommandField;
}
set {
this.returnHostCommandField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool ReturnHostCommandSpecified {
get {
return this.returnHostCommandFieldSpecified;
}
set {
this.returnHostCommandFieldSpecified = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public System.DateTime TimeStamp {
get {
return this.timeStampField;
}
set {
this.timeStampField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool TimeStampSpecified {
get {
return this.timeStampFieldSpecified;
}
set {
this.timeStampFieldSpecified = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Version {
get {
return this.versionField;
}
set {
this.versionField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute( "System.Xml", "4.7.2117.0" )]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute( "code" )]
[System.Xml.Serialization.XmlTypeAttribute( AnonymousType = true, Namespace = "http://webservices.sabre.com/sabreXML/2011/10" )]
public partial class QueueAccessRQNavigation {
private QueueAccessRQNavigationAction actionField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public QueueAccessRQNavigationAction Action
{
get
{
return this.actionField;
}
set
{
this.actionField = value;
}
}
}
Any idea how to not lost the attribute "Action" after serialization?
I will appreciate any help.
You are missing the attribute ActionSpecified, I have added a snippet of the payload construction.
QueueAccessRQ request = new QueueAccessRQ()
{
Version = "2.0.9",
Navigation = new QueueAccessRQNavigation()
{
Action = QueueAccessRQNavigationAction.I,
ActionSpecified = true
}
};

UWP Binding to AutoSuggestBox in MVVM

i am invoking the QuerySubmitted command of the AutoSuggestBox control in UWP.
the command binds to ICommand in the view model.
the problem is it requires to accept AutoSuggestBoxQuerySubmittedEventArgs which is pure UI and it's not acceptable in MVVM.
my code looks like that:
<AutoSuggestBox Name="SearchAutoSuggestBox"
PlaceholderText="Search by keywords"
QueryIcon="Find"
>
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="QuerySubmitted">
<core:InvokeCommandAction Command="{x:Bind ViewModel.SearchCommand}" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</AutoSuggestBox>
and my view model looks like that:
public DelegateCommand<AutoSuggestBoxQuerySubmittedEventArgs> SearchCommand { get; }
public MainPageViewModel()
{
SearchCommand = new DelegateCommand<AutoSuggestBoxQuerySubmittedEventArgs>(ExecuteMethod);
}
private void ExecuteMethod(AutoSuggestBoxQuerySubmittedEventArgs o)
{
// CODE HERE
}
ofcours AutoSuggestBoxQuerySubmittedEventArgs is not acceptable in the view model.
looking for alternatives...
same goes to SuggestionChosen...
InvokeCommandAction has a parameter named InputConverter which you can use to convert the event args to some other parameter that can be passed to your ViewModel.
First create a IValueConverter class to extract what you need from your event args like this:-
public class AutoSuggestQueryParameterConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
// cast value to whatever EventArgs class you are expecting here
var args = (AutoSuggestBoxQuerySubmittedEventArgs)value;
// return what you need from the args
return (string)args.ChosenSuggestion;
}
}
Then use that converter in your XAML like this:
<Page.Resources>
<converters:AutoSuggestQueryParameterConverter x:Key="ArgsConverter" />
</Page.Resources>
<AutoSuggestBox Name="SearchAutoSuggestBox"
PlaceholderText="Search by keywords"
QueryIcon="Find">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="QuerySubmitted">
<core:InvokeCommandAction
Command="{x:Bind ViewModel.SearchCommand}"
InputConverter="{StaticResource ArgsConverter}" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</AutoSuggestBox>
Finally in your viewmodel change your command to accept a string as parameter. So you would have the following in your vm:
public DelegateCommand<string> SearchCommand { get; }
public MainPageViewModel()
{
SearchCommand = new DelegateCommand<string>(ExecuteMethod);
}
private void ExecuteMethod(string o)
{
// CODE HERE
}
You can bind the search string (Text property) to a view model property and the events to parameter-less methods. This way the view model wont have to deal with UI objects:
XAML:
<AutoSuggestBox Header="What's your name?"
TextChanged="{x:Bind ViewModel.FilterUsuals}"
QuerySubmitted="{x:Bind ViewModel.ProcessQuery}"
SuggestionChosen="{x:Bind ViewModel.ProcessChoice}"
ItemsSource="{x:Bind ViewModel.Usuals, Mode=OneWay}"
Text="{x:Bind ViewModel.SearchText, Mode=TwoWay}"
QueryIcon="Find" />
Code behind:
public class MainPageViewModel : SomeViewModelBaseClass
{
/* Boilerplate code and constructor not included */
private string _SearchText;
public string SearchText {/* getter and setter INotyfyPropertyChange compliant */ }
private List<string> _Usuals; // Initialized on constructor
public string Usuals {/* getter and setter INotyfyPropertyChange compliant */ }
public void FilterUsuals()
{
// the search string is in SearchText Example:
Usuals = _UsualsStore.Where(u => u.Contains(_SearchText.ToLower())).ToList();
}
public void ProcessQuery() { /* TODO - search string is in SearchText */ }
public void ProcessChoice() { /* TODO - search string is in SearchText */ }
}
If you don't mind doing non pure MVVM way.
MainPage.xaml :
<AutoSuggestBox Name="SearchAutoSuggestBox"
PlaceholderText="Search by keywords"
QueryIcon="Find" QuerySubmitted="{x:Bind ViewModel.SearchQuerySubmitted}" IsEnabled="{x:Bind ViewModel.CanExecuteSearchCommand, Mode=TwoWay}"
>
</AutoSuggestBox>
MainPageViewModel.cs :
public class MainPageViewModel : INotifyPropertyChanged
{
private bool _canExecuteSearchCommand;
public MainPageViewModel()
{
this.CanExecuteSearchCommand = true;
}
public bool CanExecuteSearchCommand
{
get { return _canExecuteSearchCommand; }
set
{
bool changed = _canExecuteSearchCommand != value;
_canExecuteSearchCommand = value;
if(changed)
this.OnPropertyChanged();
}
}
public void SearchQuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
{
// Just example disabling SearchBox
this.CanExecuteSearchCommand = false;
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
MainPage.cs :
MainPageViewModel ViewModel = new MainPageViewModel();
UWP Binding Command/Delegate to AutoSuggestBox in MVVM
For UWP Mobile Application
Make a DelegateCommand class
public class DelegateCommand<T> : ICommand
{
private readonly Action<T> executeAction;
Func<object, bool> canExecute;
public event EventHandler CanExecuteChanged;
public DelegateCommand(Action<T> executeAction)
: this(executeAction, null)
{
//var a = ((Page)(((Func<object, bool>)(executeAction.Target)).Target)).Name;
//((ViewModel.VMBranchSelection)(executeAction.Target)).;
}
public DelegateCommand(Action<T> executeAction, Func<object, bool> canExecute)
{
this.executeAction = executeAction;
this.canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return canExecute == null ? true : canExecute(parameter);
}
public void Execute(object parameter)
{
executeAction((T)parameter);
}
public void RaiseCanExecuteChanged()
{
EventHandler handler = this.CanExecuteChanged;
if (handler != null)
{
handler(this, new EventArgs());
}
}
}
In View Model
public ICommand SuggessionSelectCity_QuerySubmitted
{
get { return new DelegateCommand<AutoSuggestBoxQuerySubmittedEventArgs>(this.SuggessionSelectCityQuerySubmitted); }
}
private void SuggessionSelectCityQuerySubmitted(AutoSuggestBoxQuerySubmittedEventArgs obj)
{
if (obj.ChosenSuggestion != null)
{
AutosuggestionTextBoxName.Text = ((ModelName) (obj.ChosenSuggestion)).Model's Property name;
//or
AutosuggestionTextBoxName.Text =(obj.ChosenSuggestion).property name
}
else
{
}
}
In XAML Code
<AutoSuggestBox Grid.Column="1" x:Name="SuggessionSelectCity"
PlaceholderText="Search by keywords" QueryIcon="Find"
ItemsSource="{Binding PApplicantCityList}"
HorizontalAlignment="Center" VerticalAlignment="Center" DisplayMemberPath="Description" Width="250" Height="45">
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="TextChanged">
<Core:EventTriggerBehavior.Actions>
<Core:InvokeCommandAction Command="{Binding SuggessionSelectCityTextChange}"/>
</Core:EventTriggerBehavior.Actions>
</Core:EventTriggerBehavior>
<Core:EventTriggerBehavior EventName="QuerySubmitted">
<Core:EventTriggerBehavior.Actions>
<Core:InvokeCommandAction Command="{Binding SuggessionSelectCity_QuerySubmitted}"/>
</Core:EventTriggerBehavior.Actions>
</Core:EventTriggerBehavior>
<Core:EventTriggerBehavior EventName="SuggestionChosen">
<Core:EventTriggerBehavior.Actions>
<Core:InvokeCommandAction Command="{Binding SuggessionSelectCitySuggestionChosen}"/>
</Core:EventTriggerBehavior.Actions>
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</AutoSuggestBox>
</Grid>
Create a list in View Model for Autosuggestion TextBox Itemssource
private ObservableCollection<ResultMasterModel> ApplicantCityList;
public ObservableCollection<ResultMasterModel> PApplicantCityList
{
get { return ApplicantCityList; }
set { this.SetProperty(ref this.ApplicantCityList, value); }
}
add some hard code value in above list
Create a Model In Model Folder
public class ResultMasterModel
{
public string Code { get; set; }
public string Description { get; set; }
}

Log method parameters and return type using Enterprise library logging application block

Is there any way to log method parameter name , its value and return type value using Enterprise library logging application block.
I have provided a code sample below. The requirement is to log it's methods input parameters value and its return type value
// Complex Types
public class UserDetails
{
public string UserName { get; set; }
public int UserAge { get; set; }
public string UserAddress { get; set; }
}
public class User
{
public string UserId { get; set; }
public string Pwd { get; set; }
}
//Interface
public interface IService
{
UserDetails GetUserDetails(User ReqUser);
}
//Imp
public class Service : IService
{
[LogCallHandler(Categories = new string[] { "General" }, LogBeforeCall = true, LogAfterCall = true ,
BeforeMessage = "This occurs before the call to the target object",AfterMessage="This occured after method call",IncludeParameters=true)]
public UserDetails GetUserDetails(User ReqUser)
{
UserDetails oUD = new UserDetails();
oUD.UserName = "hhh" + ReqUser.UserId;
oUD.UserAge = 100;
oUD.UserAddress = "HHHHHHHHHHHHHHHHHHHHHHH";
return oUD;
}
#endregion
}
//Usage
private void button2_Click(object sender, EventArgs e)
{
IUnityContainer container = new UnityContainer().LoadConfiguration();
container.AddNewExtension<EnterpriseLibraryCoreExtension>();
IService service = container.Resolve<IService>();
User nUser = new User();
nUser.UserId = "TTTTT";
nUser.Pwd = "XXXXX";
UserDetails mm = service.GetUserDetails(nUser);
}
Could anyone please explain how to implement this using Enterprise library logging application block?
You can write an OnMethodBoundaryAspect to intercept your method calls using PostSharp API.
OnMethodBoundaryAspect.OnEntry method includes MethodExecutionArgs parameter which provides all the information you need about the method and its arguments.
See this post for a sample logging aspect implementation very close to your requirements.
// This method is executed before the execution of target methods of this aspect.
public override void OnEntry( MethodExecutionArgs args )
{
// Build method information to log.
string methodInfo = BuildMethodInformation(args.Arguments);
// continue with your logging...
}
You can get method parameters via Arguments member of MethodExecutionArgs parameter like this:
private string BuildMethodInformation(Arguments arguments)
{
var sb = new StringBuilder();
sb.Append(_methodName);
foreach (var argument in arguments.ToArray())
{
sb.Append(arguments.GetArgument( i ) ?? "null");
}
return sb.ToString();
}
For method parameters, check this or this samples. They are built for caching but BuildCacheKey/GetCacheKey methods include all the information you need to get argument information of a method.
You can use EntLib LogCallHandler by code:
container.AddNewExtension<EnterpriseLibraryCoreExtension>();
container.RegisterType<IService, Service>(
new InterceptionBehavior<PolicyInjectionBehavior>(),
new Interceptor<TransparentProxyInterceptor>());
Or by config file:
<unity>
<sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" />
<namespace name="LoggingCallHandler" />
<assembly name="LoggingCallHandler" />
<container>
<extension type="Interception" />
<extension type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity.EnterpriseLibraryCoreExtension, Microsoft.Practices.EnterpriseLibrary.Common" />
<register type="IService" mapTo="Service">
<interceptor type="TransparentProxyInterceptor" />
<policyInjection />
</register>
</container>
</unity>
Here, LoggingCallHandler is namespace/assembly for your service class. Alternatively, you can define your type alias like this:
<alias alias="Service" type="LoggingCallHandler.Service, LoggingCallHandler"/>
<alias alias="IService" type="LoggingCallHandler.IService, LoggingCallHandler"/>
See this or this discussion for full configuration including logging block configuration.

BindingMode.TwoWay does not work with UserControl (not update source property)

I have created Custom User Control which contain TextBox and PasswordBox. it is binding completely work but when i changed any value inside TextBox or PasswordBox of user control then my source property does not getting refreshed.
Following are the code for my Custom User Control
RestrictedBox.xaml
<UserControl.Resources>
<Converters:EnumToVisibilityConverter x:Key="enumToVisibilityConverter" />
<Converters:EnumToVisibilityConverterReverse x:Key="enumToVisibilityConverterReverse" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="Transparent" >
<StackPanel>
<TextBox x:Name="txtTextBox" Width="50" Height="25" />
<PasswordBox x:Name="txtPasswordBox" Width="50" Height="25" />
</StackPanel>
</Grid>
RestrictedBox.xaml.cs
public partial class RestrictedBox : UserControl
{
public RestrictedBox()
{
InitializeComponent();
txtTextBox.SetBinding(TextBox.TextProperty, new Binding { Source = this, Path = new PropertyPath("Value"), Mode = BindingMode.TwoWay });
txtTextBox.SetBinding(TextBox.VisibilityProperty, new Binding("Type")
{
Source = this,
Converter = new EnumToVisibilityConverter()
});
txtPasswordBox.SetBinding(PasswordBox.PasswordProperty, new Binding { Source = this, Path = new PropertyPath("Value"), Mode = BindingMode.TwoWay });
txtPasswordBox.SetBinding(TextBox.VisibilityProperty, new Binding("Type")
{
Source = this,
Converter = new EnumToVisibilityConverterReverse()
});
}
public string Value
{
get { return (string)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(string), typeof(RestrictedBox), new PropertyMetadata("", ValueChanged));
private static void ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
public Mode Type
{
get { return (Mode)GetValue(TypeProperty); }
set { SetValue(TypeProperty, value); }
}
public static readonly DependencyProperty TypeProperty = DependencyProperty.Register("Type", typeof(Mode), typeof(RestrictedBox), new PropertyMetadata(Mode.Text, TypeChanged));
private static void TypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
}
Following are the code for LoginView where i have used my custom User Control (RestrictedBox).
LoginView.xaml
<control:RestrictedBox Type="Text" Value="{Binding Path=UserName}" />
LoginView.xaml.cs
[Export(typeof(LoginView))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public partial class LoginView : UserControl, IPageTitle
{
#region Constuctors
public LoginView()
{
InitializeComponent();
}
[Import]
public LoginViewModel ViewModel
{
get
{
return this.DataContext as LoginViewModel;
}
set
{
DataContext = value;
}
}
#endregion
}
LoginViewModel.cs
[Export]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class LoginViewModel : INotifyPropertyChanged, IRegionMemberLifetime
{
private string _UserName = "";
public string UserName
{
get { return _UserName; }
set
{
_UserName = value;
OnPropertyChanged("UserName");
}
}
[ImportingConstructor]
public LoginViewModel(IEventAggregator eventAggregator, IRegionManager regionManager)
{
}
}
Please help me to resolved this because i am trying to resolve since last 1.5 days without any luck.
Your comments and suggestions would be highly appreciated.
Note:- I am able to bind value of the UserName to TextBox but i update TextBox and click on submit i couldn't getting updated value from TextBox.
Thanks,
Imdadhusen
You are missing Mode=TwoWay in you LoginView.xaml:
<control:RestrictedBox Type="Text" Value="{Binding Path=UserName,Mode=TwoWay}" />