Solve the problem that ObjectDataSource cannot be injected with NHibernate entities

Source: Internet
Author: User

Using server controls such as ObjectDataSource and GridView can indeed achieve very oop (however, I prefer to bind with Repeater, which is a little troublesome, and this is very fast)

However, if dependency injection is required for applications in a multi-layer framework, ObjectDataSource needs to be bound to the interface.

ObjectDataSource only supports classes and cannot instantiate interfaces. This will lead to the dependency of the multi-layer structure.

 

Google found it for half a day. You only need to use the following code.
 

 

Protected IBLLTestUser bll = BLLFactory. Create ("IBLLTestUser") as IBLLTestUser;
 

Protected void odsTestUser_ObjectCreating (object sender, objectperformanceeventargs e)
{
E. ObjectInstance = bll;
}

In this way, the instance can be passed in. However, interface binding still requires calling the original class to automatically generate interface elements, which is a bit uncomfortable, but this has little impact, because the lower layer changes, as long as it is injected, and it has nothing to do with the original, only facilitates page binding and generation. The interface fields of the gridview can be automatically generated together with the NHibernate object such as ilist <t>. All functions of the GridView can be generated, including settings of ObjectDataSource during paging, modification, deletion, and sorting.
 


 

 

The GridView can only be manually bound before ObjectDataSource is used. It is quite troublesome. After ObjectDataSource is used, the NHibernate object attribute corresponding to the GridView will be automatically brought out, as shown in comparison.
 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.