mardi 1 juin 2021

A good design-pattern approach to aggregate JSON-Data in Java

I need to extract (aggregate) particular information about satellites (this should be done in external Modules which will be loaded by e.g. reflection). Furthermore, the result of such aggregation, which can be hierarchically indefinitely deep, should be output by modules too (e.g. sysout, write to JSON)

An Aggregation of this JSON-data could be for instance:

- Sattelite A
   - Transponder B
      - 20 radio programmes
      - 10 tv programmes
   - Transponder C
      - 1 radio programme
      - 100 tv programmes

or

Sattelite a
   - english TV-Programme a
   - english TV-Programme b
Sattelite b
   - english TV-Programme a
   - english TV-Programme b
   - english TV-Programme c

Module-Overview

Any idea, which approach (design patterns, classes) would be appropriate to tackle this? I thought about generic composite.





Using reflection to generate the header of a table

Help! I have this task and i don't know how to implement this. The task is: Use reflection techniques to create a method that receives a list of objects and generates the header of the table by extracting through reflection the object properties and then populates the table with the values of the elements from the list.





How can I run a WPF project using a Console project within same process in C#?

I have two simple Hello world projects. One is Console_App and the second is WPF_App.

Using C# Process.Start() will run project is separate process. In my case I have to run the WPF_App with in the Console_App process. The WPF_App should run only in Console_App process.

Sample Console and WPF Projects





How these values are accessed in the code at the runtime?

I am java beginner, and the question may seem too basic. I have this code, class named ur it contains some code. below is the code:

public class ur {
       //some code here and object initializations    

     public static {
        new String("AES/CBC/PKCS5Padding");
        new String("someString");
        new String("otherString");
        new String("12");
        new String("16");
        new String("uniqueString");
      }

//some code here

}

What I am asking here is that how the above values is accessed when needed since it was not assigned to a variable. how is this works? or is it useless code?





Modifying a dictionary contained in a dynamic object by string at runtime

I have constructed the following dynamic object:

public class DynamicModuleBase : DynamicObject, IModuleBase
{      
    // The inner dictionary.
    Dictionary<string, object> DynamicProperties = new Dictionary<string, object>();

    public DynamicModuleBase()
    {
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        result = DynamicProperties[binder.Name];

        return true;
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        DynamicProperties[binder.Name] = value;

        return true;
    }
}

Now I am trying to access my dictionary by string (ie. read existing or add values).
When I try to use typical reflection mechanism that works with non-dictionary types, it returns a null:

PropertyInfo info = ((dynamic)myObjectInstance).GetType().GetProperty(myPropertyNameAsString);    // Returns null

Why is this not working in the conventional way?
What is the right way to modify the dictionary by string?





C# Pass a type to a DataTable function at runtime

I am using Entity Framework in C#. My database contains the following helper function, which basically converts an IList to a DataTable with whatever entity Type is passed at runtime:

        public static DataTable ToDataTable<T>(this IList<T> data)
        {
            PropertyDescriptorCollection properties =
                TypeDescriptor.GetProperties(typeof(T));
            DataTable table = new DataTable();
            foreach (PropertyDescriptor prop in properties)
                table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);

            foreach (T item in data)
            {
                DataRow row = table.NewRow();
                foreach (PropertyDescriptor prop in properties)
                    row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                table.Rows.Add(row);
            }
            return table;
        }

Here is a crude of example of how I need to call this function, lots of times, because there are many combo boxes:

        public bool Custom_EvtManualComboInputAllowed(object combo, ICustomControlType cct, string keyPressed)
        {
            DataTable dt = null;
            ComboBox cmb = (ComboBox)combo;

            if (cct.Name == "IngredientDirection" &&
                (cmb.Name == "CmbIngredientDirectionText" || cmb.Name == "CmbIngredientDirectionType" || cmb.Name == "CmbProteinType"))
            {

                if (cmb.Name == "CmbIngredientDirectionText")
                {
                    _CMainController.DataAccessLayer.SetContext(typeof(IngredientDirectionText));
                    dt = Functions.ToDataTable<IngredientDirectionText>(_CMainController.DataAccessLayer.GetAll());
                }
                else if (cmb.Name == "CmbIngredientDirectionType")
                {
                    _CMainController.DataAccessLayer.SetContext(typeof(IngredientDirectionType));
                    dt = Functions.ToDataTable<IngredientDirectionType>(_CMainController.DataAccessLayer.GetAll());
                }
                else if (cmb.Name == "CmbProteinType")
                {
                    _CMainController.DataAccessLayer.SetContext(typeof(ProteinType));
                    dt = Functions.ToDataTable<ProteinType>(_CMainController.DataAccessLayer.GetAll());
                }

                //Do something with "dt"
            }

            dt = null;
            cmb = null;

            return false; 
        }

I always know that my entity type will be the name of the combox box without "Cmb". Is it possible to use Reflection to extract the type eg. "IngredientDirectionText" or "ProteinType" at runtime and then pass is to the DataTable function? This would greatly reduce the code.

I have searched other questions, but the answers I found seem to all show how to make a Class instance using Reflection, when I simply want to pass the underlying Type. Many thanks in advance.

UPDATED: For clarity

I want to be able to do something like this, but of course this illegal syntax:

        public bool Custom_EvtManualComboInputAllowed(object combo, ICustomControlType cct, string keyPressed)
        {
            DataTable dt = null;
            ComboBox cmb = (ComboBox)combo;

            if (cct.Name == "IngredientDirection" &&
                (cmb.Name == "CmbIngredientDirectionText" || cmb.Name == "CmbIngredientDirectionType" || cmb.Name == "CmbProteinType"))
            {

                string entityName = cmb.Name.Replace("Cmb", "");
                _CMainController.DataAccessLayer.SetContext(typeof(entityName));
                dt = Functions.ToDataTable<entityName>(_CMainController.DataAccessLayer.GetAll());

                //Do something with "dt"
            }

            dt = null;
            cmb = null;

            return false; 
        }




Selenium Keyword driven framework - Performance Issue

I am using Keyword driven framework and its taking longer time for execution. We have used the reflections in this framework. And rewriting the results for every action in the excel sheet.

how can we improve the performance?