Two times same code in methode except one little line In Service.Insert(), I call DeviceRepo.Insert() and in Service.Update(), I call DeviceRepo.Update(). All my other code is exactly the same.

I'm thinking I can make one method that do the same thing update. I want to update and insert when I will insert a device.

The methods respectively insert a new or update an existing (IoT) device into my database with entity framework 6.

How could I refactor the code? You can see I have the same methods twice where only another method is called.

public class DeviceService : IDeviceService
{
    internal IGenericRepo&lt;Device&gt; _deviceRepo;

    public DeviceService()
    {
        _deviceRepo = new DeviceRepo(); // P.S.: DeviceRepo overrides GenericRepo&lt;Device&gt; who implements IGenericRepo&lt;Device&gt;
    }

    public void Insert(Device device, IEnumerable&lt;int&gt; selectedFrameworks, IEnumerable&lt;int&gt; selectedOSs)
    {
        device.Framework = GiveObjects&lt;Framework&gt;(selectedFrameworks, _frameworkRepo);
        device.OS = GiveObjects&lt;OS&gt;(selectedOSs, _osRepo);

        SetStateUnchanged&lt;Framework&gt;(device.Framework);
        SetStateUnchanged&lt;OS&gt;(device.OS);

        _deviceRepo.Insert(device);
        _deviceRepo.SaveChanges();
    }

    public void Update(Device device, IEnumerable&lt;int&gt; selectedFrameworks, IEnumerable&lt;int&gt; selectedOSs)
    {
        device.Framework = GiveObjects&lt;Framework&gt;(selectedFrameworks, _frameworkRepo); 
        device.OS = GiveObjects&lt;OS&gt;(selectedOSs, _osRepo);

        SetStateUnchanged&lt;Framework&gt;(device.Framework);
        SetStateUnchanged&lt;OS&gt;(device.OS);

        _deviceRepo.Update(device);
        _deviceRepo.SaveChanges();
    }
}


GenericRepo&lt;Device&gt;:

public class GenericRepo&lt;TEntity&gt; : IGenericRepo&lt;TEntity&gt; where TEntity : class
{
    internal ApplicationDbContext context;
    internal DbSet&lt;TEntity&gt; dbSet;

    public GenericRepo()
    {
        context = new ApplicationDbContext();
        dbSet = context.Set&lt;TEntity&gt;();
    }

    public virtual TEntity Insert(TEntity entity)
    {
        return dbSet.Add(entity);
    }

    public virtual void Update(TEntity entityToUpdate)
    {
        context.Entry(entityToUpdate).State = EntityState.Modified;
        dbSet.Attach(entityToUpdate);
    }

    public virtual void SaveChanges()
    {
        context.SaveChanges();
    }
}

public class DeviceRepo : GenericRepo&lt;Device&gt;
{ /* not important methods I override for this question */ }
I think in this case I would simply make a private method to capture the duplicated code.  I probably wouldn't mind about the extra call to the SaveChanges() as it's only one line and removes any need for further complexity on the code.

public void Insert(Device device, IEnumerable&lt;int&gt; selectedFrameworks, IEnumerable&lt;int&gt; selectedOSs)
{
    UpdateDeviceProperties(device, selectedFrameworks, selectedOSs);

    _deviceRepo.Insert(device);
    _deviceRepo.SaveChanges();
}

public void Update(Device device, IEnumerable&lt;int&gt; selectedFrameworks, IEnumerable&lt;int&gt; selectedOSs)
{
    UpdateDeviceProperties(device, selectedFrameworks, selectedOSs);

    _deviceRepo.Update(device);
    _deviceRepo.SaveChanges();
}

private void UpdateDeviceProperties(Device device, IEnumerable&lt;int&gt; selectedFrameworks, IEnumerable&lt;int&gt; selectedOSs) 
{
    device.Framework = GiveObjects&lt;Framework&gt;(selectedFrameworks, _frameworkRepo); 
    device.OS = GiveObjects&lt;OS&gt;(selectedOSs, _osRepo);

    SetStateUnchanged&lt;Framework&gt;(device.Framework);
    SetStateUnchanged&lt;OS&gt;(device.OS);
}


I might also consider returning either a boolean (or some other error result) from the methods to raise awareness of whether the db action actually succeeded to help with raising failures back to the caller of the method.

In the case of Insert it could be as simple as (assuming device has an Id of course) return device.Id &gt; 0;