Is it possible to draw a graph of objects?

I have an object graph that is set this way

Clients
    string name
    List [Address] Addresses

I would like to convey this

MyClients: Clients
    string name
    List [MyAddress] Addresses

MyAddress: Address
    String city

I know that I can do this by going through the entire object graph, and in this example it will not be bad. But if you have a large graph of objects, it becomes very hairy, and secondly, it just seems that you just need to go around the entire schedule of objects.

I am looking for a solution that

Does not change objects. Clients or Address. Object graph does not pass. It can be implemented with very small code changes.
+3
source share
8 answers

:

, A B C. B C . B C B C . B C

:

. .

, , , .

.

Client Address, :

public static class MyExtensions
{
    public static void SendLetter(this Address address, string messageBody)
    {
        // blah
    }
}

:

someClient.Addresses[0].SendLetter("Dear Sir, K THX BAI");

Address. , Address DependencyObject, , DependencyProperty:

public static class MyExtensionsWithData
{
    // declare one of these for each "data slot" you'll be using
    public static readonly DependencyProperty PhoneProperty = 
        RegisterAttached("PhoneNumber", 
                         typeof(string), 
                         typeof(MyExtensionsWithData));

    public static void SetPhoneNumber(this Address address, string phone)
    {
        address.SetValue(PhoneProperty, phone);
    }

    public static string GetPhoneNumber(this Address address)
    {
        return (string)address.GetValue(PhoneProperty);
    }
}

, :

// set
someClient.Addresses[0].SetPhoneNumber("5550945793847");

// get
string phoneNum = someClient.Addresses[0].GetPhoneNumber();
+1

, AFAIK , .

, , Automapper ( .NET, , , )

+3

- , (, ).

, , , , :

public class InitialGraph : IGraph
{
     IEnumerable<Client> GetClients()
     {
         ...
     }
}

:

public class MyGraph : IGraph
{
     private readonly IGraph _initial;
     public MyGraph(IGraph initial)
     {
         _initial = initial;
     }

     IEnumerable<Client> GetClients()
     {
          foreach (Client c in _initial.GetClients())
              yield return new MyClient(c);
     }
}

MyClient ( init).

[]

, ( ), GetClients() IEnumerable<Client>, :

// note that this class doesn't implement IGraph anymore
public class MyGraph
{
     private readonly IGraph _initial;
     public MyGraph(IGraph initial)
     {
         _initial = initial;
     }

     IEnumerable<MyClient> GetClients()
     {
          foreach (Client c in _initial.GetClients())
              yield return new MyClient(c);
     }
}
+1

XSLT , (, ).

+1
var myclients = clients.OfType(typeof(MyClients))

LINQ : -)

0
source

Given your point points, the answer is: No.

Why don't you just create the necessary objects when creating the chart?

0
source

Can't you just drop them when using objects instead?

0
source

Bringing a more general object to a more specific one will not work directly.

You cannot cast Clientin MyClient, but you can apply MyClientto Client.

0
source

Source: https://habr.com/ru/post/1719955/


All Articles