Is this circular addiction bad? Any potential problems?

In this class, I ended up creating some level of data:

public class DataRequest { public class DataResponse { public DataResponse(DataRequest req) { Request = req; } public DataRequest Request {get; set;} // ... here go some other fields ... } public Response { get; set; } public DataRequest() { Response = new DataResponse(this); } public void Execute() { ... Get some data and fill Response object ... } } 

I need a request to keep abreast of the answer because it fills it with data; I need an answer to know about the request, because when I pass the response to some other methods, I want to have access to the original request.

Do you see any potential problems with this architecture, such as memory leaks, etc., or is it just a bad design idea?

+4
source share
2 answers

It doesn't look too evil, but you can also create a "DataContext" (due to the lack of a better word) that provides both a request and an answer, and only everyone knows about this context? that is, ask about the request, to which the answer will answer:

 this.Context.Request.SomeProperty; 

This is not a memory issue; .NET does not do reference counting, so you don't have to worry about the circular link island. If they are all inaccessible to other objects, they will be collected. The scenario in question will not lead to the end of the world.

+6
source

I think it’s better to make a manager class that contains both request properties and response properties, this will give you more control than this

 public class DataManager { public DataRequest Request {get; set;} public DataResponse Response {get; set;} public void Excute () { //do you logic here } } 
+1
source

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


All Articles