qkhanhpro
qkhanhpro

Reputation: 5220

How to mimic Entity Framework type-based configuration

I do not know the term to set a proper question title but I have some classes

class MyClass
{
    public string Prop1;
    public string Prop2;
    public string Prop3;
}

class MyClassOther
{
    public string PropOther1;
    public string PropOther2;
    public string PropOther3;
}

I would like to make a Configuration class that can map a property to an excel file for example

Configuration<MyClass>

where I can do

configuration.Property(p => p.Prop1).HasCell("A1")

Like Entity Framework

And later from that configuration, I can get the property value from expression (p => p.Prop1) to map to cell "A1"

Without that kind of configuration I can just store the property and the cell in any text form {"Prop1","A1"} and do something like this

Cell["A1"] = myClassInstance.GetType().GetProperty("Prop1").GetValue(myClassInstance, null);

Upvotes: 0

Views: 97

Answers (1)

Karel Křesťan
Karel Křesťan

Reputation: 469

The code to achieve this won't be hard, but you will have to write everything from scratch as I don't believe there is anything like that already implemented.

To start, please rewrite your classes so that they are using properties:

public class MyClass
{
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
    public string Prop3 { get; set; }
}

Now start creating your configuration class (take it more like a direction, there will be a lot of code in the end):

// Config class
// In real life, you don't want to use static class and method, instead, use injection
 public static class Configuration<TEntity>
 {
     public static PropertyConfigurationBuilder<TEntity, TProperty> Property<TProperty>(Expression<Func<TEntity, TProperty>> propertyExpression)
     {
         return new PropertyConfigurationBuilder<TEntity, TProperty>(propertyExpression);
     }
 }

And finally a builder class for syntax chaining:

public class PropertyConfigurationBuilder<TEntity, TProperty> 
{
    private Expression<Func<TEntity, TProperty>> _propertyExpression;

    public PropertyConfigurationBuilder(Expression<Func<TEntity, TProperty>> propertyExpression)
    {
        _propertyExpression = propertyExpression;
    }

    public void HasCell(string cell)
    {
        // Do actuall logic with the Excel sheet here
        // To do this, you actually need to access it, so you probably need to inject reference to the actuall sheet
        // You also probably don't want to return 'void' but some other builder so you chain your syntax same way Entity Framework does it
    }

}

With these simple code snippets, you should be able to use the syntax you want:

public class Usage
{
    public void Map()
    {
        Configuration<MyClass>.Property(e => e.Prop1).HasCell("A2");
    }
}

This should be a good starting point for you. I would like to help you further but I am not sure what parts of the implementation you find challenging and there is quite a lot of code and topics to cover.

Feel free to ask further!

Upvotes: 1

Related Questions