开发者

Using generic types with an asp.net user control

The other day I built a user control that dynamically builds a data display from a given custom business object.

It works well but I'd like to use the control in other applications and the way it is currently encapsulated is sub par.

My custom user开发者_开发技巧 control just contains the basic html controls and a few internal properties so I can access them from my other class.

In the same assembly I have a class DataManager - It takes an IEnumerable and a reference to its associated user control. The problem is that I have to handle implementation details like redrawing on Page_Init from the page calling the control as the actual user control has no knowledge of T.

Is there a way to pass a generic type to the user control to simplify things, or a way to store a reference to the DataManager object without specifying T?

Thanks for your help!


Here are a couple ideas, thoughts:

You are trying to achieve decoupling between your presentation (user control) and your business object's specific type.

But are having challenges because you have tightly coupled the data access (DataManager) and the presentation (user control) of your application.

Maybe you should shift your goal to decoupling your presentation layer from your business objects and data layer.

Some ideas that might help you achieve this include: 1) Consider creating a property on the user control that holds the data and set this property in the page load or page init..if you find yourself fighting the page lifecyle because the control page load fires before the page's page load event simply create your own bind method on your custom user control and call it after you set the user controls property with data 2) Create an interface or view model for the data being passed into the user control and use this as the type of the property that holds your data-- you could keep the type generic but I'm struggling to think of a user control that will work with any business object I can dream up so this seems like a bad idea 3) Regardless of the implementation details your DataManager having a reference to the user control is what you need to change. An object called DataManager should have a single responsibility and that should be getting data and have no knowledge of the user control or presentation of your application

I hope this helps. Just one guys opinion, good luck.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜