PM> Install-Package Unity
Web.config
<configSections> <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration" /> </configSections> <unity xmlns="http://schemas.microsoft.com/practices/2010/unity"> <container> <register type="Common.Interfaces.IGetTestBLL,Common" mapTo="R365.Common.BusinessLogic.GetDummyTestBLL,Common" /> </container> </unity>
UnityHelper.cs
using Microsoft.Practices.Unity; namespace VML.Employee.Common.Utils { public static class UnityHelper { private static IUnityContainer unityContainer = new UnityContainer(); static UnityHelper() { } public static IUnityContainer UnityContainer { get { return unityContainer; } set { unityContainer = value; } } public static T Resolve<T>(params ResolverOverride[] overrides) { return unityContainer.Resolve<T>(overrides); } public static T Resolve<T>(string name, params ResolverOverride[] overrides) { return unityContainer.Resolve<T>(name, overrides); } } }
IGetTestBLL.cs
public interface IGetTestBLL { List<Test> GetTestById(int id); }
GetTestBLL.cs
// get dummpy data public class GetDummyTestBLL : IGetTestBLL { public List<Test> GetTestById(int id) { ... } } // get data from WebService public class GetWCFTestBLL : IGetTestBLL { public List<Test> GetTestById(int id) { ... } }
UnityHelper.UnityContainer.LoadConfiguration();
IGetTestBLL getTest = UnityHelper.Resolve<IGetTestBLL>();
getTest.GetTestById(1);
时间: 2024-10-15 03:40:07