CSLA .NET

Vibrant discussion about CSLA .NET and using the framework to build great business applications.

How to Create MEF Container in Server Side?

rated by 0 users
Answered (Verified) This post has 2 verified answers | 5 Replies | 0 Followers

Top 150 Contributor
57 Posts
simon posted on Tue, Mar 13 2012 10:24 AM

In my solution, use WCF communicate between client and server.  and host in IIS.

my problem is how to create MEF Container in server side and import  repository instance to ObjectFactory?

  • Filed under:

Answered (Verified) Verified Answer

Top 10 Contributor
1,769 Posts
Answered (Verified) JonnyBee replied on Tue, Mar 13 2012 11:43 AM
Verified by simon

Follow this link http://cslacontrib.codeplex.com/SourceControl/list/changesets  and download the latest source.

In the folder $\samples\MEFSamples\ObjectFactory you will find a sample application with ObjectFactory that use MEF.
And in the $\trunk\CslaContrib.MEF you will find baseclasses and an ObjectFactoryLoader that use MEF.

Basically:

  1. Create interfaces for your ObjectFactories in the business assembly or separate assembly.rom
  2. Use the MEFFactoryLoader from CslaContrib.MEF (configured in servers web.config and maybe applications app.config)
  3. Make sure the object factory assemblies make into the servers bin folder.
  4. Make your "root" object factories export the actual interface and import child factory interfaces

Jonny Bekkum, Norway CslaContrib Coordinator

Top 10 Contributor
1,769 Posts
Verified by simon

Look at MefFactoryLoader.

You want to use MEF to have a separate DAL layer for unit testing and another for production - right?

That would typically require that you have the implementation in separate asseblies to be loaded into the container.

CSLA - when using ObjectFactory requires an ObjectFactoryLoader to return

  • the type (so CSLA can check for RunLocal attribute on the method to call)
  • an instance of the actual factory for a given object.  ,

The ObjectFactoryLoader gets the text string from ObjectFactoryAttribute - and the ObjectFactoryAttribute constructor that accepts typeof(interface) will actually use the fully qualified name of the interface. So this name is sent to the ObjectFactoryLoader to return an instance of the actual class that the DataPortal will call.

This is implemented in the MefFactoryLoader - that may be altered to use another container - my implementation just loads the available types from the bin folder - assuming that only the relevant assemblies is present at the given time.

So - anything that needs MEF container must be handled inside the implementation of ObjectFactoryLoader and must also "build" the imports of the actual class before it is returned to the data portal.

Jonny Bekkum, Norway CslaContrib Coordinator

All Replies

Top 10 Contributor
1,769 Posts
Answered (Verified) JonnyBee replied on Tue, Mar 13 2012 11:43 AM
Verified by simon

Follow this link http://cslacontrib.codeplex.com/SourceControl/list/changesets  and download the latest source.

In the folder $\samples\MEFSamples\ObjectFactory you will find a sample application with ObjectFactory that use MEF.
And in the $\trunk\CslaContrib.MEF you will find baseclasses and an ObjectFactoryLoader that use MEF.

Basically:

  1. Create interfaces for your ObjectFactories in the business assembly or separate assembly.rom
  2. Use the MEFFactoryLoader from CslaContrib.MEF (configured in servers web.config and maybe applications app.config)
  3. Make sure the object factory assemblies make into the servers bin folder.
  4. Make your "root" object factories export the actual interface and import child factory interfaces

Jonny Bekkum, Norway CslaContrib Coordinator

Top 150 Contributor
57 Posts
simon replied on Tue, Mar 13 2012 8:24 PM

Thanks.

I will have to combine diffgrah、customField  MEF MefBusinessBase into one Bussinessbase

Top 10 Contributor
1,769 Posts

When you use ObjectFactory you have no need for MefBusinessBase and the other Mef intermediate base classes.

MefFactoryLoader is the key component and the usage of interfaces for the "root" and "child" object factories.

They MefXYX base classes are only required for repository pattern and just my laziness to use the same business library with a minimum of changes for the 2 samples.

Jonny Bekkum, Norway CslaContrib Coordinator

Top 150 Contributor
57 Posts
simon replied on Wed, Mar 14 2012 4:04 AM

According to what I understand, if need to sepreate BO and DataAccess,such as Fetch,Create, into two assembly, will use ObjectFactory. If I no sepeate them. How to Create Container?

for example in ProjectTracker, if i need to create MEF, should create static class  and  examine it in DALFactory

 public static IDALManager GetManager()
        {

           _container = Container.Current;
            if (dalManager == null)
            {


                var dalTypeName = ConfigurationManager.AppSettings["DalManagerType"];
                if (!string.IsNullOrEmpty(dalTypeName))
                    // _dalType = Type.GetType(dalTypeName);
                    dalManager = ServiceLocator.Current.GetInstance<IDALManager>(dalTypeName);
                else
                    throw new NullReferenceException("DalManagerType");
                if (dalManager == null)
                    throw new ArgumentException(string.Format("Type {0} could not be found", dalTypeName));
            }
            return dalManager;
        }

Top 10 Contributor
1,769 Posts
Verified by simon

Look at MefFactoryLoader.

You want to use MEF to have a separate DAL layer for unit testing and another for production - right?

That would typically require that you have the implementation in separate asseblies to be loaded into the container.

CSLA - when using ObjectFactory requires an ObjectFactoryLoader to return

  • the type (so CSLA can check for RunLocal attribute on the method to call)
  • an instance of the actual factory for a given object.  ,

The ObjectFactoryLoader gets the text string from ObjectFactoryAttribute - and the ObjectFactoryAttribute constructor that accepts typeof(interface) will actually use the fully qualified name of the interface. So this name is sent to the ObjectFactoryLoader to return an instance of the actual class that the DataPortal will call.

This is implemented in the MefFactoryLoader - that may be altered to use another container - my implementation just loads the available types from the bin folder - assuming that only the relevant assemblies is present at the given time.

So - anything that needs MEF container must be handled inside the implementation of ObjectFactoryLoader and must also "build" the imports of the actual class before it is returned to the data portal.

Jonny Bekkum, Norway CslaContrib Coordinator

Page 1 of 1 (6 items) | RSS

Copyright (c) 2006-2010 Marimer LLC. All rights reserved.
Email admin@lhotka.net for support.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems