Refactoring-simpler and more elegant code II: Practical Experience (How can code be reduced by project layering)

Source: Internet
Author: User

Preface:

I haven't written any text for several days, because in the legendary 8-country language blog, the actual definition of the 10 + 1 custom language is now predefined, the code is still slowly writing ~~~~

At present, the latest progress preview URL: http://cyq.tupianshop.com/, its strength and CYQ. Data framework V3.N series after the introduction.

Writing articles sometimes requires inspiration or impulse ~ For example, I was just changing the Blog Code. After some thinking and some inspiration, I got this article.

 

Long Ago, I wrote an article: refactoring-making the code more concise and elegant: Practical Experience (providing a skill that allows you to save N more code)

If you have not read it, you can check it first. At the end of the article, I mentioned this:

Of course, you can also load it in turn. IHttpCustom is added to the constructor in LogicBase.

The final implementation page is called as follows:

 

I just mentioned it, but I didn't mention how to implement it. In fact, it's not difficult. It's just adding a constructor to the base class and passing in the IHttpCustom interface.

In fact, this method is also used in my blog recently developed by refactoring, which saves a lot of parameter code and looks quite concise ~

However, this is not the focus of this section. What is it about?

 

I. Let's see what the reality is and how the problem arises.

 

1: view the project hierarchy from the picture

Note:

In order to save a lot of parameter code for Logic projects, we extracted the common interface IHttpCustom from the HttpCustom page base class.

At the same time, use the abstract base class LogicBase to implement the interface, so as to omit N multi-parameter code in the above section.

 

2: What is the current situation?

Finally, there is a solution: Web. Title. It is a solution for managing all page titles in a unified manner. Let's take a look at the methods in the Action:

// Author: passing by autumn blog: http://www.cnblogs.com/cyq1162/public class Action
{
Public static TitleInfo GetTile (string urlPara, string urlType, MDataRow domainUser, MutilLanguage language)
{
//... Omitted...
}
}

Let's take a look at the calls in the base class:

Web. Title. TitleInfo info = Web. Title. Action. GetTile (UrlPara, UrlType, DomainUser, Language );
If (info! = Null)
{
//... Modify the title of the current page...
}

Simply put, it is quite common to call the Web. Title code in the base class.

What's the problem? The problem is that because of the "Project dependency", Web. Title cannot be implemented in the same way as Logic.

 

3: Shenma! Project dependency, what?

To put it simply, project A adds a dll that references Project B. In turn, Project B cannot reference the DLL of Project.

 

What is the actual situation?

A: If you want to implement IHttpCustom for Web. Title, you must reference the Module project.
B: If HttpCustom needs to call Web. Title, the Web. Title project must be referenced.
As a result, the circular dependency is generated, so we cannot solve the problem in a way like Logic [the Module does not call the Logic code, so it will not reference each other]

 

2: Just click. That's it?

1: Okay, I can't do it anymore. Pass the parameters, write N methods, and let them pass in one by one. So I went back to liberation overnight ~~~~~

In fact, it was good before liberation. There were just a few more parameters, and there was nothing to do with mock. It was possible to pass a few more parameters to mock, if this is not the case, you can comfort yourself ~~~

 

2: To increase blog access statistics and article access statistics, I plan to process it for a new project ~~~

The method is similar to Web. Title, which is called Web. Visit? Well, that's the call! Implement the same as Web. Title! It also writes several lines of code in the base class for processing, and the branches are handed over to Web. Visit for processing.

Oh ~~~ Continue before liberation? No worries. In this case, you can write mock or mock ~~~

 

3: I want to add the XXX feature again. I am planning to process it for a new project ~~~ The processing method is similar to Web. Title !!

Look at the parameters of a heap, a heap, and a heap ~~ Confused ~ I am in a bad mood ~ How many times are there !!!~~ Oh ~~ FucX: What is mock ~ Once or twice, you can still comfort yourself. Then, self-comfort is neither neural nor God banned ~~~

 

Only when the problem is repeated too many times will we think about how to reconstruct and optimize it...

The more people grow, the smaller the requirements for this repeated value ~~~

 

3. layered, clear dependencies, and return to post-liberation

 

1: For layering, there is such a phenomenon

A: Many people do not know how to divide the layer, so there will be three layers at will.
B: Some people think they have a lot of theoretical ink, so they write articles to clearly understand the three layers. How do people misunderstand the three layers and how do they score the three layers?
C: There are some old birds. When I saw the three layers, I ran and said, You have all poisoned Petshop.

 

2: stratified, reasonable, and clear dependencies [3 is only a number, not a truth]

A: Clear the dependency and exit the exit layer. IHttpCustom leaves home.

For the base class HttpCustom, there are too many things to call ~~ Simply put, it is very difficult to depend on others too much and want to be depended on by others,
For the IHttpCustom interface, there are too many dependencies and are often referenced by others ~~ It is not suitable to mix in a layer that relies too much on others.
So: IHttpCustom and HttpCustom are about to say goodbye, and they should always go ~~

B: IHttpCustom has arrived at the new home of the new project.

The name of the new home is a headache. I finally started a Web. Core, because many others will be forced to come in later.

C: LogicBase also leaves home and comes to the IHttpCustom address.

D: The two decided to change their names together.

 

3: The effect has come out, and the loop dependency has disappeared.

A: The previous Logic remains unchanged. The reference Module is changed to Web. Core, and the base class is changed to a new name.
B: Web. Title can finally be the same as Logic. It references Web. Core and inherits CoreBase.
C: whatever new to it, it references Web. Core and inherits CoreBase.

 

Let's take a look at the call method after the last change:

A: Title implementation. A bunch of parameters are omitted.

Public class Action: CoreBase
{
Public Action (ICore custom): base (custom ){}
Public TitleInfo GetTitle ()
{
TitleInfo info = new TitleInfo ("passing by autumn", "http://cyq1162.cnblogs.com", "8 languages blog ");
Switch (UrlType)
{
Case "error ":
Case "home ":
Case "sys ":
Info = new HomeTitle (this). Get ();
Break;
Case "index ":
Case "article ":
Case "photo ":
Case "admin ":
Case "guestbook ":
Info = new UserTitle (this). Get ();
Break;
}
Info. Title + = info. Split + Language. Get (IDLang. sitename );
Info. ClearHtml ();
Return info;
}
}

 

B: HttpCustom call

Web. Extend. TitleInfo info = new Web. Extend. Action (this). GetTitle ();

 

 

Statement:

At the beginning of the project, I was always satisfied with my own code. [The requirement was generated, so I will write the code that is not beautiful at, 1.1] the pipeline is so cyclical. One day, I finally found myself writing a bunch of junk code ~~~ So I resigned and left the mess to others. I took over the mess and called my aunt again ~~~~ Simple code, everyone's responsibility ~~ Make the code simpler and easier to understand ~~~

~ Everything just wanted to make the code more concise and elegant, so I thought a lot about it ~~~

 

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.