1

I am creating some custom API. What I need is pass some object to the api that have "static" required propreties and dynamic optional properties.

I have created a dynamic custom object like this:

public class MyDynamicEntity : DynamicObject, IMyDynamicEntity
{
    public string Key { get; set; }
    public string Type { get; set; }
}

And my API has this signature:

void DoWork(MyDynamicEntity myEntity);

Everything "seems" work, but the problem is that the intellisense does not help programmers (that are not me) how to use my API.... Infact to use the api they must write something like this:

dynamic myEntity = new MyDynamicEntity();
myEntity.DynamicPropA = ...;
myEntity.DynamicPropB = ...;
...
myEntity.Key = ...;
myEntity.Type = ...;

They must declare an object of type dynamic, so it is not visible to who use the API the exists two required props: Key andType..

Can anyone have a suggestion how to solve my problem? thanx

PS.: Due to some reasons, I cannot simply create some classes that implements my IMyDynamicEntity interface...

Exactly, I must create a mapping between my MyDynamicEntity class and a strictly "Azure-bounded" class: Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity (or Microsoft.WindowsAzure.Storage.Table.TableEntity). But my API must be generic and not specific to a use case. Furthermore my API cannot expose a DynamicTableEntity (or a TableEntity nor a custom class that inherits one of them) otherwise the client that use my API would import Azure DLLs.

I am looking for a solution easy to remember in some moths to me and easy to understand for who must use my API, so I do not need to explain everyone what they have to do with the API.

3
  • Due to some reasons What are those reasons? Commented Apr 9, 2018 at 9:29
  • This seems like a job for generics. If it's an interface and not a superclass, though, you still need to implement it in your actual class. What exactly is that DoWork requirement from? Commented Apr 9, 2018 at 9:29
  • I extended my "PS" that explain what are reasons that let me choose dynamic objects as possible solution Commented Apr 9, 2018 at 9:54

1 Answer 1

1

You can split dynamic and non-dynamic properties like this:

interface IMyDynamicEntity {
    string Key { get; set; }
    string Type { get; set; }
}

public class MyEntity : IMyDynamicEntity
{        
    public string Key { get; set; }
    public string Type { get; set; }
    public dynamic Properties { get; } = new MyDynamicProperties();

    private class MyDynamicProperties : DynamicObject {

    }
}

Then usage becomes:

var myEntity = new MyEntity();
myEntity.Properties.DynamicPropA = ...;
myEntity.Properties.DynamicPropB = ...;                
myEntity.Key = ...;
myEntity.Type = ...;

Alternatively, require "required" properties in constructor:

public class MyDynamicEntity : DynamicObject, IMyDynamicEntity
{
    public MyDynamicEntity(string key, string type) {
        Key = key;
        Type = type;
    }
    public string Key { get; }
    public string Type { get; }
}

Then usage becomes:

dynamic myEntity = new MyDynamicEntity(key, type);
myEntity.DynamicPropA = ...;
myEntity.DynamicPropB = ...;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.