开发者

Best solution: Extension Methods or a New Class?

开发者 https://www.devze.com 2023-04-12 21:00 出处:网络
So I\'ll try and play devil\'s advocate on this one... Hypothetically there is a Framework which services 2 or 3 different web sites. 1 basic function of the framework is to deal with all calls to a

So I'll try and play devil's advocate on this one...

Hypothetically there is a Framework which services 2 or 3 different web sites. 1 basic function of the framework is to deal with all calls to a certain DB. When making a DB call the websites call a Framework DataSource object and get a generic Framework data object back.

Now for the websites to retrieve properties/methods that are specific to it's needs we've got 2 solution options.

  1. Create a new Class, extending or wrapping the generic data object, exposing more domain friendly properties & keeping any domain specific functionality inside of this new class.

  2. Instead of creating a new class, create extension methods inside the Framework to serv开发者_Python百科ice each of these websites. So everything is contained inside the Framework and can be shared between web sites if 1 day needed.

Just to clarify here would be examples: Properties:

  1. NewObject.GetSiteSpecificProperty
  2. GenericObject.GetProperty("GetSiteSpecificProperty") or GenericObject.GetSiteSpecificProperty()

Methods

  1. NewObject.DoSomethingSpecificToThisWebsite()
  2. GenericObject.DoSomethingSpecificToThisWebsite()

So what solution would you opt for? 1 or 2?

Cheers.


In my opinion when designing a Framework you want to keep as much solution specific aspects out of the Framework and have the calling entities handle that if possible.

Now I'm not sure quite how your framework will be used or by how many different websites\projects but going with option (2) means that now whenever a new website is added the framework now needs to go do work in order to complete this functionality. The work of using the framework in a custom way should be handled by the websites not by the framework. If this framework ever grows to use 10 or even 100 websites, this becomes an absolute nightmare to maintain and your framework ends up looking much less like a framework and more like a solution specific product. Going with (1) is a much cleaner solution. Basically keep your framework reusable and solution-agnostic as possible.

If you are designing a framework that will be used by many different projects and is designed to be around for a while I'd recommend reading Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries (2nd Edition)


Generally if you control the source of the class you're extending, I would extend it by inheritance instead of extension methods. Extension methods are great for adding custom functionality to classes you don't control, like .NET built-ins (String, Enum, IEnumerable) and other third-party objects. However, they can be hard to organize, and they're technically static methods, which you usually want to minimize in the interest of startup performance and memory footprint.

You may also find yourself in namespace and method resolution trouble by going with extensions; let's say you put the extension methods into site-specific libraries. If one site ever has to do the same site-specific thing as another, you must either include one site's library containing the extension method in the other site (exposing other things you may not want your code to know about, possibly containing duplicates of objects or extensions), or clone the code (violating DRY).


In my opinion, it's a better design to create a base class and use overrides for your site specific code. Although they could do it, it just doesn't seem like extension methods were meant for this type of operation.

Now if you're looking for a way to get different values using a shared framework on different websites, it seems like the web.config would suit that need. Each site will have it's own Web.Config, can you populate the specific property values you need in there, and have a single function to retrieve that value?


I would go for 1 because it keeps the framework general (and reusable) and specific functionality where it's used and where I would look if I were a maintenance programmer.

To share functionality I'd create a base wrapper class that the specific wrappers derive from.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号