Warehouse design mode and container

Source: Internet
Author: User
Tags ibase

I recently learned about the company's framework. It is quite simple to use a container and a warehouse. All service objects are placed in a container, and only one copy of the object is saved in the container, it also has the function of decoupling. The following is a simplified framework.

First, define a container interface.

Namespace mycontainer. factory {public interface icontainer {// <summary> // register an object in the container /// </Summary> /// <typeparam name = "trepository"> </ typeparam> // <Param name = "Factory"> </param> void register <trepository> (func <trepository> factory ); /// <summary> /// obtain the object from the container /// </Summary> /// <typeparam name = "trepository"> </typeparam> /// <returns> </returns> trepository resolve <trepository> ();}}

Define a container to implement this interface

Namespace mycontainer. factory {internal class container: icontainer {private readonly concurrentdictionary <type, Object> _ factories; public container () {_ factories = new concurrentdictionary <type, Object> ();} public void register <trepository> (func <trepository> factory) {type key = typeof (trepository); _ factories [Key] = factory;} public trepository resolve <trepository> () {object factory; If (_ factories. trygetvalue (typeof (trepository), out factory) {return (func <trepository>) Factory )();} throw new argumentexception ("************* this object is not registered ***********");}}}

The concurrentdictionary dictionary is thread-safe and has good performance in some specific cases. Therefore, we use this object as the service object container.

Namespace mycontainer. factory {public class containerlocator {Private Static containerlocator _ instance = new containerlocator (); /// <summary> /// container's interface // </Summary> private icontainer _ container; public static icontainer container {get {return _ instance. _ container ;}/// <summary> /// An Instance Object loaded by the container /// </Summary> Public static containerlocator instance {get {return _ instance ;}} private containerlocator () {_ Container = new container (); registerdefaults (_ container );} /// <summary> /// register an object /// </Summary> /// <Param name = "_ container"> </param> private void registerdefaults (icontainer _ container) {_ container. register <ienglish> () => New English ());}}}

The next step is the warehouse layer. In the warehouse layer, this is the code implementation of interfaces and some interfaces. You must remember which layer to do and which layer to do, otherwise, it will be difficult to maintain later

A simple interface

namespace mycontainer.Repositories{   public interface IBase   {       string ShowHi();    }}

This is a basic interface, and the general method definitions of all interfaces are written in this interface,

The following is the interface of each data access layer object. Here we provide a simple implementation of empty interfaces and interfaces.

namespace mycontainer.Repositories{    public interface IEnglish:IBase    {       }}
namespace mycontainer.Repositories{    public class English:IEnglish    {        public string ShowHi()        {            return "Hello !!!";        }    }}

Well, the storage layer has been written. The framework here is simple. It can be considered as a data access layer, followed by the final service layer, also called the business logic layer.

namespace mycontainer.service{    public class HelloService    {        private readonly IEnglish english;        public HelloService()        {            english = ContainerLocator.Container.Resolve<IEnglish>();        }        public string SayHi()        {            return english.ShowHi();        }    }}

Such a simple framework is complete. Although the writing on the storage layer is simple, the storage layer can be well expanded if the system is complicated in the future.

namespace service{    class Program    {        static void Main(string[] args)        {            HelloService service = new HelloService();            string str = service.SayHi();            Console.WriteLine(str);            Console.ReadKey();        }    }}


Download http://files.cnblogs.com/dongqinglove/MyContainer.rar

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.