How Can I Use Interfaces to resolve circular dependency to access dbcontext database across libraries asp.net mvc core

The x class library contains the models needed to create the database table so x added to the y class library (contains database). The problem arises when the database in class library y need to be used in class library x controllers. How can I use Interfaces to resolve this issue. Please Help

dependency injection – Simple constructor DI implementation in Rust

From my experience in C# programming I think that DI is important. But it’s not possible to do it same way in Rust. There are some DI frameworks but I’ve come with an idea on how it can be made and decided to implement it my way. My implementation uses only constructor injection, no container needed. I’m only learning Rust now so can you review my code, find possible pitfalls, suggest improvements or better approaches?

My goals in DI:

  1. Make stubs possible for testing.
  2. Systems shouldn’t know “sub-dependencies” of their dependencies. Separate construction and providing dependencies from actual usage. All dependencies are “wired” in one place.
  3. No global shared state.
  4. The design principle that higher level systems declare what they need and lower level systems implement it, not other way.
  5. Avoid God objects.
  6. All dependencies can be clearly determined by a constructor signature, no looking inside code needed. Therefore I don’t use Resource Locator pattern.
#(cfg(not(feature = "test")))
mod Bindings {
    // real implementations binding

    // I assume only one implementor per each interface
    // this is not always the case but al least it's good for a simple scenario
    pub type Door = crate::Autocar::DoorMod::DoorImpl;
    pub type Engine = crate::Autocar::EngineMod::EngineImpl;
}

// how can I move these into actual tests files?
#(cfg(all(feature = "test", feature = "test1")))
mod Bindings {
    // stubs for test1 can be binded here
    pub type Door = crate::Autocar::DoorMod::DoorImpl;
    pub type Engine = crate::Autocar::EngineMod::EngineImpl;
}

#(cfg(all(feature = "test", feature = "test2")))
mod Bindings {
    // stubs for test2 can be binded here
    pub type Door = crate::Autocar::DoorMod::DoorImpl;
    pub type Engine = crate::Autocar::EngineMod::EngineImpl;
}

// prelude for internal use
mod Usings {
    pub use crate::Bindings::*;
    pub use std::cell::RefCell;
    pub use std::rc::Rc;
    pub type Mrc<T> = Rc<RefCell<T>>; // Mutable Reference Counter
    pub fn Mrc<T>(v: T) -> Mrc<T> {
        Rc::new(RefCell::new(v))
    }
}

fn main() {
    // this code performs constructor injection itself
    // all constructors are called here

    use Autocar::*;
    use Usings::*;

    let engine = Mrc(Engine::new());

    // also we can make factory methods
    let make_door = || -> Door { Door::new(engine.clone()) };

    let doors = vec!(make_door(), make_door());
    let mut car = Car::new(engine, doors);

    // all constructed, now run something
    car.doors(0).open();
}

// now application code
mod Autocar {
    use crate::Usings::*;

    // top-level struct so no interface
    pub struct Car {
        // Since same Engine is used also by a Door too, I have to use Mrc.
        // This may become an issue as once a dependency becomes
        // used by multiple structs I have to change it everywhere to Mrc
        // and insert borrow_mut() everywhere.
        // Which doesn't look like a good design. But no choice. Or?
        pub engine: Mrc<Engine>,

        pub doors: Vec<Door>,
    }

    impl Car {
        pub fn new(engine: Mrc<Engine>, doors: Vec<Door>) -> Car {
            Car { engine, doors }
        }
    }

    // declare Car dependencies:

    // we actually need IDoor so stubs can inherit it and reflect signature changes when refactoring
    pub trait IDoor {
        fn is_opened(&self) -> bool;
        fn open(&mut self);
    }

    pub trait IEngine {
        fn is_running(&self) -> bool;
        fn start(&mut self);
        fn stop(&mut self);
    }

    pub(crate) mod DoorMod {
        use super::*;
        use crate::Usings::*;

        pub struct DoorImpl {
            // I tried to design the code in a way so that DI doesn't prevent optimizations.
            // So I don't use IEngine here or otherwise it becomes dyn implicitly and then
            // no inlining and can't be placed on the stack.
            // But one issue with this approach is that IntelliSense can see
            // all EngineImpl functions even if it implements multiple traits, not just IEngine.
            // But a stub will contain only interface-declared functions
            // so it will be at least checked by the compiler.
            engine: Mrc<Engine>,
        }

        impl IDoor for DoorImpl {
            fn is_opened(&self) -> bool {
                unimplemented!()
            }

            fn open(&mut self) {
                if self.engine.borrow().is_running() {
                    self.engine.borrow_mut().stop();
                }
                println!("opening")
            }
        }

        impl DoorImpl {
            pub fn new(engine: Mrc<Engine>) -> Self {
                DoorImpl { engine }
            }
        }
    }

    pub(crate) mod EngineMod {
        use super::*;
        use crate::Usings::*;

        pub struct EngineImpl;

        impl IEngine for EngineImpl {
            fn is_running(&self) -> bool {
                true
            }

            fn start(&mut self) {
                println!("starting");
            }

            fn stop(&mut self) {
                println!("stopping");
            }
        }

        impl EngineImpl {
            pub fn new() -> Self {
                EngineImpl {}
            }
        }
    }
}
```

How do we model Domain entity that has dependency on Config classes?

So for e.x I have an entity Payment and a config Payment settings.

class Payment 
{ 
    public Payment(PaymentSetting setting, ....other fields){}
    internal Payment() /*strictly for the ORM**/ 
    //Other properties and behaviour
} 
class PaymentSettings 
{ 
    public Timespan TransactionTimeout {get; private set;} 
    //Other properties and behaviour
} 
class Order /AggregateRoot*/ 
{ 
    List<Payment> payments {get; private set;} 
    //Other properties and behaviour
}

There is no problem when I am going to create Payment object as I can inject the setting to constructor. But How do we inject config when the Order is loaded from DB?

I understand in the current example, Config is needed while creating the appointment only, and we can store the exact value as part of Payment object and revive it when needed. But what happens, if we need to know the latest config? Should add Methods to inject it from outside? Is it considered an acceptable practice wrt. DDD

magento2 – RequireJS module dependency injection in Magento 2

I am searching for a way to extend a JS file which is used as dependency in other JS files for the whole project. The file in use is Magento_Ui/js/form/element/abstract and I need to replace it with new file Custom_Ui/js/form/element/abstract and then enforce that every other file to use my new file instead of the original one. Only other files, because my new file will extend the original abstract file. Is that possible? So in practice I need to replace abstract algorithms for the Magento form UI element and make it in the most portable and stable way.

asymptotics – Upper bound for reccurence relation with two variables, with linear dependency between them

Given the following reccurence relation:

$$T(M,k) = T(M-1,k)+T(M-2,k-1)$$

where $T(0,k)=0, T(1,k)=1, T(M,1)=1$

I have $M^k$ as a general upper bound for $T(M,k)$.

Now, suppose I want to give an upper bound only for cases where $M=n cdot k$, i.e. I have the following new definition:
$$R(n,k) = T(nk,k)$$
and $R(n,1) = n-1$, $R(2,k)=f(k)$ (where $f(k)$ is some function of $k$ I know how to bound for any $k$).

is there a way to give an upper bound for $R(n,k)$, expressed with $n$ and $k$, which will by tighter than just placing it in $M^k$? I suspect that for those cases where $M=nk$ the value of $T$ is much lower than $M^k$, so a tighter bound should exists.

Is there a way to do such thing? I tried to unfold $R(n,k)$ to get an explicit definition but I get stucked when I need to express $T(nk-1,k)$ and $T(nk-2,k-1)$, since they do not “fit” to the defenition of $R(n,k)$.

Is dependency inversion principle necessary?

I’ve read a lot about dependency inversion principle but still, I can’t apply it to my case. I just don’t know when I should apply it and when not. I write a simple application in Java to generate invoices. For now, I have basic classes: Client, Product, InvoiceLine and Invoice. Should these classes communicate through interfaces? For instance, I have a method in Product for getting name of the prduct:

public String getName() {
        return name;
    }

And I use this method in class Invoice

public void addLineToInvoice(Product product, int quantity) {
        rows.add(new InvoiceLine(rows.size(), product.getName(), quantity, product.getPrice()));
    }

Now, should I create an interface for Product? Or is it unnecessary?

domain driven design – DDD Validator Specification with dependency on repository

When you inject repositories or other dependencies into the domain, the domain becomes less pure. Impure domain models are harder to test and reason about. Therefore pure domain models are preferred.

So, how would it be possible to satisfy the two conditions below, without injecting a repository?

I can’t have the property Color with value “Red” if there are 10 or more entity Car with the Color = “Red”.

I can’t have the property Color with value “Red” if the entity “Warehouse” haven’t the entry with item code “FF0000”.

The Car entity could have a method or constructor that takes the required information as arguments, instead of fetching it from a repository. For example:

public static Car Create(Model model, Engine engine, Color desiredColor, IReadOnlyList<ColorCount> usedColors, IReadOnlyList<Color> availableColors)
{
   // do validation on usedColors and availableColors
   return new Car(model, engine, desiredColor);
}

Before calling this method from an application service, you fetch the information from the repository. The usedColors collection contains the number of times each color is used and the availableColors collection contains all the colors in the warehouse.

This way, all decisions can be made inside the domain, without any out-of-process dependencies.

database design – What axiom is for this functional dependency?

I have recently come across this example that is not in my textbook. I can find the type of axiom to infer in most of the functional dependency except this. Can anyone point me what is the axiom to infer this functional dependency?

A->C

B->C

I am thinking if union rule is applicable here where AB->C? However, my textbook example wrote for union rule, X -> Y and X -> Z then X->Y,Z. Does union apply to the LHS too?

c# – How to avoid giant main function because of dependency injection

I try to use dependency injection as often as I can but every time I end up with the same problem : I always have a giant main function.

If I use it without any framework and send the classes in the constructors, I have to initialize every class in the main and it can take quite a lot of lines.

If I use a framework with a inversion of control / a service locator the result is nearly the same because I have to initialize the classes and send them to the IOC.

Is there any way to use dependency injection without having a big giant function initializing everything ?

reactjs – How to run a react project when react is added as peer dependency

I’m trying to run a repo from GI ithub locally. The folder structure for react is somewhat different what what I usually see in a react project(I’m new to react). the folder structure is like

-index.js
-css folder
-jsx folder – components
-jsx folder – index.html
-jsx folder – index.js

Also react is added as a peer dependency. I’m not sure how to run this repository. Any help is much appreciated

React in package.json

"peerDependencies": {
    "react": "^16.3.0",
    "react-dom": "^16.3.0",
    "react-router-dom": "^5.1.2"
}

enter image description here