开发者

c# DAL - Interface for business objects

I have a Report Interface which has a Run method.

There are different types of reports which implement this interface and each run their own kind of report, getting data from different tables.

Each report, using its own data context, gets data which then populates Business Objects with and at the moment they are returned as an array (I would like to be able to at least return something like a list but because you have to define the list type it makes it a bit more difficult).

Reflection is then used to find out the properties of the returned data.

I hope I have explained this well 开发者_开发知识库enough!

Is there a better way of doing this?

By request:

public interface IReport
{
    int CustomerID { get; set; }

    Array Run();
}

public class BasicReport : IReport
{
    public int CustomerID { get; set; }

    public virtual Array Run()
    {
        Array result = null;
        using (BasicReportsDataContext brdc = new BasicReportsDataContext())
        {
            var queryResult = from j in brdc.Jobs
                              where j.CustomerID == CustomerID
                              select new JobRecord
                              {
                                  JobNumber = j.JobNumber,
                                  CustomerName = c.CustomerName
                              };

            result = queryResult.ToArray();
        }
    }
}

The other class then does a foreach over the data, and uses reflection to find out the field names and values and puts that in an xml file.

As it stands everything works - I just can't help thinking there is a better way of doing it - that perhaps my limited understanding of C# doesn't allow me to see yet.


Personnally I would first ask myself if I Really need an interface. It would be the case if the classes implementing it are Really different by nature (not only by report kind). If not, i.e all the implementing classes are basically "Reporters", then yes, there is a more convenient way to do this which is :

  • Writing a parent abstract Report
  • Having a virtual Run method and the CustomerID accessor
  • inheriting your "Reporter" classes from it
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜