Today I will continue to introduce the core library IOC Use , In my frame ,IOC It's easy to use , It is mainly used to decouple modules and instantiate interfaces .

1、 Interface specification ,IocContainer The interface is relatively simple, only 3 A way , But it's the most used in the system .

IocContainerpublic interface IocContainer:IDisposable
{
/// <summary>
/// Get a set of instances of an interface
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns>IEnumerable{``0}.</returns>
IEnumerable<T> GetInstances<T>();
/// <summary>
/// Get an instance of the interface
/// </summary>
/// <typeparam name="T"> Type of interface </typeparam>
/// <returns>, If the interface is not registered , Then return to null</returns>
T GetInstance<T>(); /// <summary>
/// Get objects based on keywords
/// </summary>
/// <param name="key">The key.</param>
/// <returns>System.Object.</returns>
object GetInstance(string key);
}

2、 Interface implementation . I use it AutoFac, He was chosen on the basis of 2 All aspects of consideration , The first is right MEf And generics , The second is efficiency

In the garden Leepy I've compared the mainstream IOC frame ,http://www.cnblogs.com/liping13599168/archive/2011/07/17/2108734.html,

It's early , Interested can use the latest class library test .

AutoFac Yes. MEF Extensions for , Very convenient . The advantage of this is , I can only refer to... In the core module AutoFac , There is no need to refer to , use Mef Just mark it , For team development , He doesn't need to know AutoFac How to use .

Not much said , Code up

 ContainerBuilder builder;
IContainer _container = null;
public AutofacContainer(params ComposablePartCatalog[] catalogs)
{
builder = new ContainerBuilder(); bool isRegisterOrm = false;
foreach (var item in catalogs)
{
builder.RegisterComposablePartCatalog(item); if (item is DirectoryCatalog && !isRegisterOrm)
{
DirectoryCatalog dir=item as DirectoryCatalog;
}
} }

among ComposablePartCatalog yes MEF The east east ,ContainerBuilder yes AutoFac Things that are , This step is mainly about Mef The component of is registered to AutoFac in . And then through IContainer Container=builder.Build(Autofac.Builder.ContainerBuildOptions.IgnoreStartableComponents); This method is initialized , I found this method when I tested it , It can only be executed once , The second time I reported an error . Corresponding IocContainer.GetInstance The implementation of this method is simple , call Autofac The corresponding method is OK bool bl = Container.TryResolve<T>(out t); return t Just OK 了 ,

 
 

It should be noted that ,Mef Support for generics is not very good , So it needs to be dealt with separately .

about IRepository<T> and IRepository<T,Tid> Interface , The ideal situation is , Which entity class do I need to deal with , Use directly var rep=  IocContainer.GetInstance<IRepository<T>>() This way, , You can get to IRepository<T> Example , Then the following operations are carried out .

First of all, get all the Assembly, And then judge if his name is "Repository`1" ending , And then call AutoFac Generic registration method for , register ,

Assembly ass = Assembly.LoadFile(filepath);
Type[] types = ass.GetTypes().Where(t => t.Name.EndsWith("Repository`1")).ToArray();
if (types.Length > 0 && types[0].Name != "IRepository`1")
{
builder.RegisterGeneric(types[0]).As(typeof(IRepository<>));
isload = true;
}

Because I am right Autofac It's also a little knowledge , Only in stupid ways , There must be a good way , It's just that I don't know . I hope the experts in the garden can give me some advice .

adopt IocContainer Interface , We can create and use all the interfaces .
3、 Use
   stay Core I designed a global static class in ApplicationEx, hold IocContainer The interface is exposed as a read-only property , Interface instantiation operations are all in ApplicationEx In the initialization of , At other startup times , initialization ApplicationEx once , Guarantee IOC Is initialized once , The direct use of all the following modules ApplicationEx Of Container attribute . That's what we want .
4、 Expand
I am a lazy person , I don't want to write every time var rep=  IocContainer.GetInstance<IRepository<T>>() , I think it's a little too much
And then in Core Li Gei IocContainer Add an extension method ,
 /// <summary>
/// Get data operation warehouse interface
/// </summary>
/// <typeparam name="T"> The entity class of the operation </typeparam>
/// <returns>IRepository{``0}.</returns>
public static IRepository<T> GetRepository<T>(this IocContainer ioc) where T : EntityBase, new()
{
return ioc.GetInstance<IRepository<T>>();
}

In the future, all module calls will be written directly var rep=  IocContainer.GetRepository<T>(), I've written less 10 A few characters , Ha ha ha .

Come here today , I'll introduce you later .

My development framework (WinForm)3 More articles about

  1. Development framework (WinForm)3

    My development framework (WinForm)3 Today I will continue to introduce the core library IOC Use , In my frame ,IOC It's easy to use , It is mainly used to decouple modules and instantiate interfaces . 1. Interface specification ,IocContainer The interface is relatively simple ...

  2. RDIFramework.NET Agile development framework WinForm New file center - Realize centralized attachment management

    1. introduction File center like attachment management is a very practical function , You can file the documents you need , You can also share files with others , It's more like a knowledge center . The main interface of the file center is shown in the figure below , left " Attachment classification " Shows the user's understanding of the file ...

  3. My development framework (WinForm)2

    The last article briefly introduced , One of my development frameworks . There are still a lot of people watching , Thank you for your attention , Let me continue with , How modules are organized . Data modular : The module mainly completes the operation of data , Adopt warehousing mode to realize , In the core module (C ...

  4. My development framework (WinForm)

    Recently, , Look in the garden , Many students , Show me the development framework in my work , Some are powerful , Some are short and tough , I'd like to join you , I hope you can give me some advice . One . guiding ideology 1. Minimize the programmer's dependence on the database , Or try to write as little as possible sql sentence . according to ...

  5. My development framework (WinForm)4

    Log module For a system , The log module is essential , It can provide maintenance and bug Repair of , Bring great convenience ..net There are many log modules in , The more popular ones are Log4Net,NLog, And the log module of Microsoft Enterprise Library , I used ...

  6. WinForm Development of general attachment management control design and development reference

    1. introduction stay WinForm In development , File attachment management exists in almost any application , It's a very general and centralized public module . Our daily records are accompanied by pictures . Documents and other attachments to show , If you do an attachment management for each business object , Or every time ...

  7. RDIFramework.NET ━ .NET New release of agile development framework - Best used .NET Development framework 100% Source code authorization

    RDIFramework.NET, be based on .NET Agile development framework of rapid information system based on .10 Precipitation in . Thousands of tests , Committed to intelligent development of enterprises , Help improve software development efficiency . Best used .NET Development framework ,100% Source code authorization . 1. ...

  8. RDIFramework.NET Agile development framework ━ Introduction to workflow components

    RDIFramework.NET, be based on .NET Rapid information system agile development . Integration Framework , Best for users and developers .Net Framework deployment plan . 1.RDIFramework.NET Introduction to agile development framework RDIFramewo ...

  9. RDIFramework.NET Agile development framework Web A new mail center is added to realize convenient mail sending and receiving

    1. introduction E-mail has such a demand in many business systems , Is a more formal and commonly used function . In our framework, we provide the mail center function module , Collection of internal mail . Mail classification . The asterisk of the mail . The deletion and thorough deletion of email , Mail center function module interface ...

Random recommendation

  1. TweenMax Learning one

    TweenMaxjs It's a high performance js Animation frame , It is associated with css3 Animation has the concept of a timeline . You can easily add animations to a timeline queue and execute them in the order you need . Official website address : http://greensock ...

  2. C++ Realize the golden section series ( Fibonacci sequence )( Non recursive )

    Fibonacci sequence : Defined as F0=0,F1=1,......Fn=F(n-1)+F(n-2) 1,1,2,3,5,8,13,21,34,55....... void FibonacciOdd(int n) ...

  3. USACO GCD Extreme(II)

    The main idea of the topic : seek gcd(1,2)+gcd(1,3)+gcd(2,3)+...+gcd(n-1,n) ---------------------------------------------------- ...

  4. 17.3--- How many tails of factorial 0(CC150)

    Ideas , In fact, this question easy, It's just how many 5. answer : public static int getFactorSuffixZero(int n) { // write code here int res = ...

  5. JavaScript encapsulation Ajax( class JQuery in $.ajax() Method )

    ajax.js (function(exports, document, undefined){ "use strict"; function Ajax(){ if(!(this ...

  6. SPOJ4206Fast Maximum Matching(hopcroft-karp)

    Please stamp the title here The main idea of the topic : Naked binary matching . Topic analysis : The data is strong , Used to measure the template . This question uses hungry It's hard to run , So use hopcroft-karp Algorithm . This algorithm is better than hungry Efficient because every time bfs Find a way to ...

  7. grok Regular capture ( Namely perl Regular capture of )

    2.3.2 grok Regular capture : \s+ and [\n\t\r\f]+ equally 1. The named grouping format is (?<grp name>), Reverse reference with \k<grp name> 2. Name the group of matches ...

  8. FZOJ2110: Star

    Problem Description Overpower often go to the playground with classmates. They play and chat on the ...

  9. Detailed explanation java Responsibility chain model of design pattern

    See :http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt175 From drumming to spreading flowers Drumming is a lively and nervous drinking game . At the banquet, the guests in turn ...

  10. JAVA Override and Overload Meaning to distinguish

    java Inheritance in , Methods cover ( rewrite )override Overloads with methods overload The difference between 1. Method rewrite (Overriding) And overloading (Overloading) yes Java Different manifestations of polymorphism , rewrite (Overr ...