如何使用相同的方法过滤来自不同对象的数据? [英] How can I filter data from different object using same method?

查看:62
本文介绍了如何使用相同的方法过滤来自不同对象的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我使用的是N层架构。对于每个用户界面(UI页面: .aspx 扩展名),只有一个类。这些类继承了不同的Entity类。

在我的数据过滤图层中,我想传递UI页面的对象并想要检查。

所以对于不同的页面我将不同的对象传递给我的过滤方法。如何在同一过滤方法中过滤不同页面的不同对象?

In my Project I use N-tier architecture. For each User Interface (UI page: .aspx extension )there is a single class. These class inherit different Entity class.
In my Data Filtering Layer I want to pass the object of the UI page and want to check.
So for different page I pass different object to my filtering method. How can I filter different object for different page in same filtering method?

推荐答案

您好,



As我可以理解,这是一对紧张夫妇的问题。



你可以做什么....



Hi,

As far I can understand that's a problem of tight couple.

What you can do....

public interface IFilterable
{
object Filter();
}





在您的实体类中实现相同的功能,然后从DataFilter层调用过滤器功能



[更新]





implement the same in you Entity Class and then Call Filter Function from your DataFilter Layer

[Update]

public interface IFilterable
    {        
        object Filter();
    }
    
    public abstract class EntityRepo<T> : IFilterable
    {        
        public virtual object Filter()
        {
            return null;
        }
    }
    public class EmpRepo : EntityRepo<EntityEmp>
    {
        public object Filter()
        {            
            // write your logic
            return new List<EntityEmp>();
        }
    }
    public class EntityEmp : IFilterable
    {
        public int ID { get; set; }
        public string Name { get; set; }
       
    }
    // Data Filter Layer 

    public class FilterData
    {
        public IEnumerable< T> GetFilteredData<T>(IFilterable Fltr)
        {
            return (IEnumerable<T>)Fltr.Filter();
        }
    }





我认为它会帮助你......



I think it will help you...


好吧,



由于C#是一种面向对象的语言,你应该能够像解析一种变量一样解析整个对象。例如:

Well,

Since C# is an object orientated language, you should be able to parse an entire object as you would parse an type of variable. for example :
void ParseSomething(Object obj)
{
   Form a = null;
   if (obj.GetType() == typeof(Form))
      a = (Form)obj;
   if (a != null)
   {
      Label lbl = a.lblName.Text; //Assuming for a second that lblName is public
   }
}



我希望这就是你的意思,否则请提供一个例子。


I hope this is what you meant, or else please provide an example.


这篇关于如何使用相同的方法过滤来自不同对象的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆