简体   繁体   中英

Mapping classes c#

尝试使用xustom标头进行一些映射

If template is a class, and each template potentially has a different transform, then why not just include the transform function inside of your template class?

public static myentrypoint( ITemplate t);
{
   t.transform();
}

The way that I do these types of situations is through the use of Generics . ( Shameless self-promotion of a blog post )

Basically, you'll have your base class set up like this:

public abstract class Transformer<T>
    where T : Template
{
    public abstract void Transform(T item);
}

Then you derive for each of your types like this:

public class Transformer1 : Tansformer<Template1>
{
    public void Transform(Template1 item) 
    {

    }
}

public class Transformer2 : Transformer<Template2>
{
    public void Transform(Template2 item)
    {

    }
}

Then you'll just need a factory to give you the correct Transformer .

public class TransformFactory
{
    public Transformer<T> GetTransformer<T>(T item)
    {
        if (item is Template1)
            return new Transformer1();
        else if (item is Template2)
            return new Transformer2();
        // ...
    }
}

The benefit of this approach is that you'll be able to encapsulate all behavior on that specific type in the concrete implementations. If there is any common behavior on them all, you can do that in the abstract base.

Invoking methods based on a parameter without switch-case statements in C#

In OOP , based on the [ open / close principle ] which says that software entities such as classes and functions should be open for extension, but closed for modification.

Methods which use switch-case statement would call this principle into question. In order to implement this principle inside the codes without causing changes in their functionality.

We use a pattern named "Delegate Dictionary Pattern" .

For example, we have an entity named Template that keep input values as well as some of Transform classes for processing this Template.


Template class for keeping input value

public class Template
{
    public int TransformNo { get; set; }
    public string Title { get; set; }
}

ITransform interface for transform abstract

public interface ITransform
{
    void Do(Template template);
}

Transform1 as a concrete class of ITransform

public class Transform1 : ITransform
{
    public void Do(Template template)
    {
        Console.WriteLine($"Transform : {template.TransformNo}, TemplateTitle : { template.Title}");
    }
}

Transform2 as a concrete class of ITransform

public class Transform2 : ITransform
{
    public void Do(Template template)
    {
        Console.WriteLine($"Transform : {template.TransformNo}, TemplateTitle : { template.Title}");
    }
}

TransformCordinator class for coordinating template of *ITransformer**

public class TransformCordinator
{
    Dictionary<int, Action<Template>> transformMap = new Dictionary<int, Action<Template>>();
    public TransformCordinator()
    {
        transformMap.Add(1, x => new Transform1().Do(x));
        transformMap.Add(2, x => new Transform2().Do(x));
    }

    public void Do(Template template)
    {
        transformMap[template.TransformNo](template);
    }
}

// example

 class Program
  {
    static void Main(string[] args)
    {

        var transformCordinator = new TransformCordinator();
        transformCordinator.Do(new Template() { TransformNo = 1, Title = "Hi!" });

        Console.ReadLine();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM