简体   繁体   中英

C# Avoid Repetitive code and use Generic

The subject might look like that this question is already been posted, but my code snippet and problem is different. Please have a look at it. I have some 20 to 22 Sql tables of which I have created my POCO class in my Application. Each of these tables have some 4-5 common columns to which I need to fill some records in the application,rest columns of the table are already filled by XML data sent by some other application. The only 4-5 common columns of each table I am trying to fill in the application as below.

private List<Software> GetSoftwareEntityData(Information request, DateTime scanDateTime)
{
    List<Software> Data = new List<Software>();
    int SrNo = 0;
    foreach (var item in request.Software)
    {
        Software d = new Software();
        d = item;
        d.DCDTime = DateTime.Now;
        d.LastModDTime = scanDateTime;
        d.ID = request.Id;
        d.ScanDateTime = scanDateTime;
        d.SrNo = ++SrNo;
        Data.Add(d);
    }
    return Data;
}

private List<Users> GetUsersEntityData(Information request, DateTime scanDateTime)
{
    List<Users> Data = new List<Users>();
    int SrNo = 0;
    foreach (var item in request.Users)
    {
        Users d = new Users();
        d = item;
        d.DCDTime = DateTime.Now;
        d.LastModDTime = scanDateTime;
        d.ID = request.Id;
        d.ScanDateTime = scanDateTime;
        d.SrNo = ++SrNo;
        Data.Add(d);
    }
    return Data;
}

Similarly I have written code for 22 tables each and these functions are being called when I am dealing with that specific set of object.

How can I create a Generic method, so that I can get rid of 22 functions which have repetitive code?

I am posting my Information class also here below.

public class Information
{
 public List<Software> Software{get;set;}
 public List<Users> Users{get;set;}
}

So now my Software and Users class looks like below with an interface implementation called ICommonColumns.

public interface ICommonColumns
{
DateTime DCDTime{get;set;}
DateTime LastModDTime{get;set}
.....
and others common fields
}

public class Software : ICommonColumns
{
....
}

public class Users : ICommonColumns
{
....
}

Why not have the POCO's implement an interface and create a method that accepts implementations of that interface?

Edit:

public interface ISomething
{
    public DateTime DCDTime { get; set; }
    public DateTime LastModDTime { get; set; }
    public int ID { get; set; }
    public DateTime ScanDateTime { get; set; }
    public int SrNo { get; set; }
}
    private IEnumerable<T> GetSoftwareEntityData<T>(Information request, DateTime scanDateTime, Func<Information, T> someCollection)
    where T : ISomething
   {
    int SrNo = 0;

    foreach (var item in someCollection(request))
    {
        item.DCDTime = DateTime.Now;
        item.LastModDTime = scanDateTime;
        item.ID = request.Id;
        item.ScanDateTime = scanDateTime;
        item.SrNo = ++SrNo;

        yield return item;
    }
}

Both Software and Users will need to have a constraint to ensure that they have something in common , such as an interface or base class.

public class BaseEntity
{
    public DateTime DCDTime { get; set; }
    public DateTime LastModDTime { get; set; }
    public DateTime ScanDateTime { get; set; }
    public int ID { get; set; }
    public int SrNo { get; set; }
}

For example, your Software class would look like this:

public class Software : BaseEntity
{
    //Some more properties
}

Then you can create a generic method like this:

private IEnumerable<TEntity> GetEntityData<TEntity>(int requestId, IEnumerable<TEntity> requestEntities, DateTime scanDateTime)
    where TEntity : BaseEntity, new()
{
    int SrNo = 0;

    foreach (var item in requestEntities)
    {
        TEntity d = new TEntity();
        d = item;
        d.DCDTime = DateTime.Now;
        d.LastModDTime = scanDateTime;
        d.ID = requestId;
        d.ScanDateTime = scanDateTime;
        d.SrNo = ++SrNo;

        yield return item;
    }
}

And call it like this:

IEnumerable<Software> softwares = GetEntityData<Software>(request.Id, request.Software, DateTime.Now);
IEnumerable<Users> users = GetEntityData<Users>(request.Id, request.Users, DateTime.Now);

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