. Net mef entry-level example,. netmef entry-level example

Source: Internet
Author: User

. Net mef entry-level example,. netmef entry-level example

 

I like to start with simple examples to learn new things. I feel that understanding and getting started will be faster. This article will record a simple mef example. Baidu, google has gone.

 

Mef does not need to re-code when you adjust some functions, but only needs to replace the corresponding dll.

 


 

MefDemo. Core: returns the current date in the format of yyyy-MM-dd.

MefDemo. Core2: returns the current date in the format of yyyy/MM/dd.

MefDemo. InterFace: InterFace, intermediate InterFace between the caller and the implementer

MefDemo. Web: An MVC program used for testing

 

I. Code in MefDemo. Core


 


 


 

Ii. Code in MefDemo. Core2

 



 

Iii. MefDemo. InterFace code



 

Iv. MefDemo. Web code

The code in Global. asax is written in red box, and the rest is automatically generated by.


 

The code of the MefDependencySolver class is as follows:


 
public class MefDependencySolver : IDependencyResolver
    {
        private readonly ComposablePartCatalog _catalog;
        private const string MefContainerKey = "MefContainerKey";

        public MefDependencySolver(ComposablePartCatalog catalog)
        {
            _catalog = catalog;
        }

        public CompositionContainer Container
        {
            get
            {
                if (!HttpContext.Current.Items.Contains(MefContainerKey))
                {
                    HttpContext.Current.Items.Add(MefContainerKey, new CompositionContainer(_catalog));
                }
                CompositionContainer container = (CompositionContainer)HttpContext.Current.Items[MefContainerKey];
                HttpContext.Current.Application["Container"] = container;
                return container;
            }
        }

        #region IDependencyResolver Members

        public object GetService(Type serviceType)
        {
            string contractName = AttributedModelServices.GetContractName(serviceType);
            return Container.GetExportedValueOrDefault<object>(contractName);
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return Container.GetExportedValues<object>(serviceType.FullName);
        }

        #endregion
    }



 
In the last step, we can test it by finding a controller.

 


After testing, we will find that when we are in MefDemo. the binfile of the Web Project is stored in MefDemo. core. in dll, We ViewBag. the value obtained by date is the year-month-number. When we put the MefDemo in the binfile. core. replace dll with MefDemo. when Core2.dll is used, ViewBag. the value of date is the year, month, and number. Now, the Getting Started demo is here.



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.