lundi 3 avril 2017

Evaluating dynamic information for constructing objects in .NET

I am having a problem of generating expressions of all kinds which at the end will be evaluated and on the basis of results of those expressions or their direct results will be apply to destination column/property while creation of that type.

For example :

Product

While creating Package object ( left hand side of equal op is destination col )

PackageId = dbContext.Products.SingleOrDefault(pr=>pr.Code== "name" ).ProductId

PackageName = dbContext.Packages.SingleOrDefault( pr=>pr.Name == model.Name ) != null && 
!string.IsNullOrEmpty(model.CodeName) ?
dbContext.Packages.SingleOrDefault( pr=>pr.Name == model.Name ).PackageId : null

and so on.....almost 100 fields I need to initialize in this way, where expressions.

Now the problem is : all these decisions are being made on the GUI ( i.e mapping of the source column to destination column, mapping on the basis of certain expression evaluation, or mapping a result of an expression to destination column ). Hence I am preparing an XML for including this whole info at field level in such a way..

<Mapping>
<Field TargetField="CountryGroup" >
    <Rule>
        <Compare pram1="Versandart" param2="24" >
            <True Result="2"/>
            <False Result="1"/>
        </Compare>
    </Rule>
</Field>    
<Field TargetField="UnLoadingCountryId" >
    <Rule>
        <Compare param1="Versendart" param="24">
            <True ResultType  ="CallToDB" TableName="Country" SourceCol="Iso" TargetCol="ELKZ" TargetValueColumn="CountryId"/>
            <False ResultType ="ScalerValue" value="94"/> 
        </Compare>  <!-- targetting expression was :  bell.Versandart == "24" ? Countries.SingleOrDefault(pr => pr.Iso == bell.ELKZ.Left(2))?.CountryId : 94,-->
    </Rule>
</Field>
<Field TargetField="CostCenter" >
    <Rule>
        <IsNullOrEmpty param1="Kundengruppe">
            <True ResultType  ="ScalerValue" value="S"/>
            <False ResultType ="SelfAssignment" value="Kundengruppe"/>
        </Compare>
    </Rule>
</Field>

<Field TargetField="TODId" >
    <Rule>
        <IsNullOrEmpty param1="Versandstelle" param2="1031">
            <True ResultType ="CallToDB" TableName="Tod" search="Border" TargetValueColumn="TodId"/>
            <True ResultType ="CallToDB" TableName="Tod" search="Delivered" TargetValueColumn="TodId"/>

            <!-- targetting expression was : Versandstelle == "1031" ? 1028 : 1027 -->
        </Compare>
    </Rule>
</Field>

I don't know at this moment, how good this xml structure would be and how this would all the way store each and every information which would dynamically be read and applied. Further this xml might get complicated more and more because of nested expressions or because of multiple kinds of operations on the basis of results of expressions.

For this , I am considering following options:

  1. Creating dynamic expressions using DLINQ ( later on calling compile )

  2. Having seperate class for each and every operation ( i.e Compare, Add, Substract, Searching into the database ( getting DbSets dynamically and searching for perticular property using reflection)

First option is more easily construct able than later one. Because creating dynamic expressions could be much easy. But that would be much slower because of dynamic nature.

Second option where we should have separate commands( classes ) for doing respective operations might be faster but it would create much complexity in the application. Because, each operation is dynamically invoked on the basis of Xml Tags.

Can I have better recommendations on this? What should be follow? Xml structure needs to be refine?





Aucun commentaire:

Enregistrer un commentaire