Veselin Vasilev
Veselin Vasilev

Reputation: 3793

Sharing data between User Controls without access to the Page class

I have two user controls and each one of them needs to call the same service to get some data. Since these two user controls are on the same page - this would result in calling the service twice, which is not ideal from performance perspective.

These controls are part of a CMS, so I don't have access to the aspx page - I mean, I cannot add a property to the Page class and use that.

One way I am thinking this can be done is storing the data in the user Session.

So, each of the user controls will check the Session and if it is null - it will call the service and populate the Session.

What are my options apart from using the Session?

EDIT: Each of the user controls needs to call the following service, which is part of the CMS:

 var personalizationService = new PersonalizationService();
 var userSegments = personalizationService.GetUserSegments();
 //get data based on userSegments

The thing is - these two user controls can be on the same page, but at some point the user may decide to remove one of the user controls, or add a third one to the page.

Thanks

Upvotes: 1

Views: 988

Answers (1)

mason
mason

Reputation: 32703

That's a bad idea. User controls should be self contained and not know about their environment. Think of a grid view.

Aspx

<asp:GridView runat="server" id="GridView1" />
<asp:GridView runat="server" id="GridView2" />

Code Behind

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        DataTable dt = Database.GetDataTable();
        GridView1.DataSource = dt;
        GridView2.DataSoure = dt;
        GridView1.DataBind();
        GridView2.DataBind();
    }
}

GridView doesn't pull data from Session, and neither should you! Instead, set a property in your user control to pass the data in.

If the data has relevance outside of just the current page then you might store it in session, but the user control shouldn't pull the data from Session, your page should pull the data from Session and pass it into the user control

if(Session["ImportantData"] == null)
    {
    Session[ImportantData] = Database.GetImportantData();
    }
MyUserControl1.Data = Session["ImportantData"] as ImportantDataClass;
MyUserControl2.Data = Session["ImportantData"] as ImportantDataClass;

In response to your edit...

var personalizationService = new PersonalizationService();
MyUserControl1.PersonalizationService = personalizationService;
MyUserControl2.PersonalizationService = personalizationService;

By the way, you shouldn't do new PersonalizationService(); Ideally, you should be getting that from dependency injection, so you can swap out the implementation from a centralized location. I tend to use Ninject, which can do attribute injection.

[Ninject]
public IPersonalizationService personalizationService {get; set;}

protected void Page_Load(object sender, EventArgs e)
{
    MyUserControl1.PersonalizationService = personalizationService;
}

Upvotes: 1

Related Questions