C # Generics / Interfaces - returning different types depending on class

For work, we have specific types of records that are included, but each project has its own implementation. Columns etc. They differ from each other, but in general the process is the same (records are divided into batches, batches are assigned, batches are completed, batches are returned, batches are sent, etc.). Many of the columns are also common, but name changes sometimes occur (BatchId in one vs Id in another. [Column ("name") takes care of this problem).

This is currently what I have for implementing batch assignment functionality with the common components specified in the interface:

public interface IAssignment
{
   // properties
   ...
   // methods
   T GetAssignmentRecord<T>(int UserId, int BatchId) where T : IAssignment;
   List<T> GetAssignmentRecords<T>(int UserId) where T : IAssignment;
}

Now I have two projects that have a batch assignment. Due to the fact that they are executed in EntityFramework, assignment in namespace 1 and assignment in namespace2 are completely different things, but are connected by certain common components (identifier, assigned user, registered, etc.), which control all methods of their return .

I think my main question is that I am doing it wrong, and if there is a better way to achieve this, so that I can transfer data to my controllers, and so that the controllers look a bit like a project, having as much method work is processed automatically (first of all, for the “fix, fix everything” scenario to occur when I need to make updates).

Here is an example of how I am implementing the implementation for namespace1:

public class Assignment
{
    ...
    public T GetAssignmentRecord<T>(int UserId, int BatchId) where T : IAssignment
    {
        var db = new Database1Context();
        return (T) Convert.ChangeType(db.Assignment.Where(c => c.UserId == UserId && c.BatchId == BatchId && c.Assigned).First(), typeof(T));
    }
}

In the controller:

Assignment assignment = new Assignment();
var record = assignment.GetAssignmentRecord<Assignment>(userid, batchid);
// do stuff

, , . Assignment, , . , , , , : " , framework, , " , , ".

? , ?

+2
3

, , , . ...

interface IAssignment
{
}

interface IRepo<out T> where T : IAssignment
{
    T GetAssignmentRecord(int UserId, int BatchId);
    IEnumerable<T> GetAssignmentRecords(int UserId);
}

class AssignmentRecord : IAssignment
{
}
class AssignmentWeb : IAssignment
{
}

class RepoDb : IRepo<AssignmentRecord>
{
    public AssignmentRecord GetAssignmentRecord(int UserId, int BatchId)
    {
        //using(var db = new MyDbContext())
        //{
        //    return db.Assignment.Where(c => c.UserId == UserId && c.BatchId == BatchId && c.Assigned).First();
        //}
        return new AssignmentRecord();
    }

    public IEnumerable<AssignmentRecord> GetAssignmentRecords(int UserId)
    {
        //using(var db = new MyDbContext())
        //{
        //    return db.Assignment.Where(c => c.UserId == UserId && c.BatchId == BatchId && c.Assigned);
        //}
        return new List<AssignmentRecord> 
            {
                new AssignmentRecord(),
                new AssignmentRecord(),
                new AssignmentRecord(),
                new AssignmentRecord(),
                new AssignmentRecord(),
                new AssignmentRecord(),
                new AssignmentRecord(),
                new AssignmentRecord(),
            };
    }
}

class RepoWeb : IRepo<AssignmentWeb>
{
    public AssignmentWeb GetAssignmentRecord(int UserId, int BatchId)
    {
        // fetch it from some web service...
        return new AssignmentWeb();
    }

    public IEnumerable<AssignmentWeb> GetAssignmentRecords(int UserId)
    {
        //using(var db = new MyDbContext())
        //{
        //    return db.Assignment.Where(c => c.UserId == UserId && c.BatchId == BatchId && c.Assigned);
        //}
        return new List<AssignmentWeb> 
            {
                new AssignmentWeb(),
                new AssignmentWeb(),
                new AssignmentWeb(),
            };
    }
}

class MYController
{
    public IRepo<IAssignment> Repository { get; set; } // you can inject this e.g. DI

    public IAssignment GetAssignment(int userid, int batchid)
    {
        return Repository.GetAssignmentRecord(userid, batchid);
    }

    public IEnumerable<IAssignment> GetAllAssignments(int userid)
    {
        return Repository.GetAssignmentRecords(userid);
    }
}

class ProgramAssignment
{
    static void Main(string[] args)
    {
        try
        {
            var controller = new MYController();
            controller.Repository = new RepoDb();

            IAssignment assignment = controller.GetAssignment(0, 0);
            IEnumerable<IAssignment> all = controller.GetAllAssignments(0);

            controller.Repository = new RepoWeb();

            assignment = controller.GetAssignment(0, 0);
            all = controller.GetAllAssignments(0);
        }
        catch
        {
            Console.WriteLine("");
        }
    }
}

out - - ...

, "" ?

+3

, 2 Assignment (, ), , , . "" ( ) - .

() Assignment "". Assignment, POCO.

() /// . :

public AssignmentRepository: IAssignmentRepository{
  public Assignment GetAssignmentRecord(int userId, int batchId){

  }
}

public BatchAssignmentRepository: IAssignmentRepository{
  public Assignment GetAssignmentRecord(int userId, int batchId){

  }
}

, 2 1, ? , , . - BatchAssignment (, , ..), Assignment "if batchAssignment else" .

:

IAssignmentService service = new AssignmentService();
IAssignmentRepository repository = new AssignmentRepository();
Assignment a = repository.GetAssignmentRecord(userId, batchId);
service.DoSomething(a);
+1

. /, , , . , "" , . , undefined , .

, . , . ( ).

Unity.

+1

All Articles